[#1632] node: Cache Sidechain errors along with the values

In previous implementation failed requests to the Sidechain weren't
cached. It makes sense to cache errors along with the values in order to
decrease potential load spikes onto Sidechain nodes.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-08-11 18:44:48 +04:00 committed by LeL
parent 7c1babb7d6
commit a529149e6f

View file

@ -20,6 +20,8 @@ type netValueReader func(interface{}) (interface{}, error)
type valueWithTime struct {
v interface{}
t time.Time
// cached error in order to not repeat failed request for some time
e error
}
// entity that provides TTL cache interface.
@ -57,23 +59,25 @@ func (c *ttlNetCache) get(key interface{}) (interface{}, error) {
valWithTime := val.(*valueWithTime)
if time.Since(valWithTime.t) < c.ttl {
return valWithTime.v, nil
return valWithTime.v, valWithTime.e
}
c.cache.Remove(key)
}
val, err := c.netRdr(key)
if err != nil {
return nil, err
}
c.cache.Add(key, &valueWithTime{
v: val,
c.set(key, val, err)
return val, err
}
func (c *ttlNetCache) set(k, v interface{}, e error) {
c.cache.Add(k, &valueWithTime{
v: v,
t: time.Now(),
e: e,
})
return val, nil
}
func (c *ttlNetCache) remove(key interface{}) {