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