Compare commits
1 commit
master
...
fix/multip
Author | SHA1 | Date | |
---|---|---|---|
|
261bf48b97 |
2 changed files with 56 additions and 9 deletions
|
@ -51,6 +51,7 @@ Changelog for FrostFS Node
|
|||
- Pretty printer of basic ACL in the NeoFS CLI (#2259)
|
||||
- Adding of public key for nns group `group.frostfs` at init step (#130)
|
||||
- Iterating over just removed files by FSTree (#98)
|
||||
- Concurrent morph cache misses (#1248)
|
||||
|
||||
### Removed
|
||||
### Updated
|
||||
|
|
|
@ -24,9 +24,19 @@ type valueWithTime[V any] struct {
|
|||
e error
|
||||
}
|
||||
|
||||
// valueInProgress is a struct that contains
|
||||
// values that are being fetched/updated.
|
||||
type valueInProgress[V any] struct {
|
||||
m sync.RWMutex
|
||||
v V
|
||||
e error
|
||||
}
|
||||
|
||||
// entity that provides TTL cache interface.
|
||||
type ttlNetCache[K comparable, V any] struct {
|
||||
ttl time.Duration
|
||||
m sync.RWMutex // protects progMap
|
||||
progMap map[K]*valueInProgress[V] // contains fetch-in-progress keys
|
||||
ttl time.Duration
|
||||
|
||||
sz int
|
||||
|
||||
|
@ -41,32 +51,68 @@ func newNetworkTTLCache[K comparable, V any](sz int, ttl time.Duration, netRdr n
|
|||
fatalOnErr(err)
|
||||
|
||||
return &ttlNetCache[K, V]{
|
||||
ttl: ttl,
|
||||
sz: sz,
|
||||
cache: cache,
|
||||
netRdr: netRdr,
|
||||
ttl: ttl,
|
||||
sz: sz,
|
||||
cache: cache,
|
||||
netRdr: netRdr,
|
||||
progMap: make(map[K]*valueInProgress[V]),
|
||||
}
|
||||
}
|
||||
|
||||
func waitForUpdate[V any](vip *valueInProgress[V]) (V, error) {
|
||||
vip.m.RLock()
|
||||
defer vip.m.RUnlock()
|
||||
|
||||
return vip.v, vip.e
|
||||
}
|
||||
|
||||
// reads value by the key.
|
||||
//
|
||||
// updates the value from the network on cache miss or by TTL.
|
||||
//
|
||||
// returned value should not be modified.
|
||||
func (c *ttlNetCache[K, V]) get(key K) (V, error) {
|
||||
val, ok := c.cache.Peek(key)
|
||||
valWithTime, ok := c.cache.Peek(key)
|
||||
if ok {
|
||||
if time.Since(val.t) < c.ttl {
|
||||
return val.v, val.e
|
||||
if time.Since(valWithTime.t) < c.ttl {
|
||||
return valWithTime.v, valWithTime.e
|
||||
}
|
||||
|
||||
c.cache.Remove(key)
|
||||
}
|
||||
|
||||
v, err := c.netRdr(key)
|
||||
c.m.RLock()
|
||||
valInProg, ok := c.progMap[key]
|
||||
c.m.RUnlock()
|
||||
|
||||
if ok {
|
||||
return waitForUpdate(valInProg)
|
||||
}
|
||||
|
||||
c.m.Lock()
|
||||
valInProg, ok = c.progMap[key]
|
||||
if ok {
|
||||
c.m.Unlock()
|
||||
return waitForUpdate(valInProg)
|
||||
}
|
||||
|
||||
valInProg = &valueInProgress[V]{}
|
||||
valInProg.m.Lock()
|
||||
c.progMap[key] = valInProg
|
||||
|
||||
c.m.Unlock()
|
||||
|
||||
v, err := c.netRdr(key)
|
||||
c.set(key, v, err)
|
||||
|
||||
valInProg.v = v
|
||||
valInProg.e = err
|
||||
valInProg.m.Unlock()
|
||||
|
||||
c.m.Lock()
|
||||
delete(c.progMap, key)
|
||||
c.m.Unlock()
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue