mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-08 22:31:04 +00:00
cmd/strelaypoolsrv: Fix race condition in caching (#6496)
Successful LRU cache lookups modify the cache's recency list, so RWMutex.RLock isn't enough protection. Secondarily, multiple concurrent lookups with the same key should not create separate rate limiters, so release the lock only when presence of the key in the cache has been ascertained. Co-authored-by: greatroar <@>
This commit is contained in:
parent
7505ea79a0
commit
b7ba401c0b
@ -116,10 +116,10 @@ var (
|
|||||||
requestQueueLen = 10
|
requestQueueLen = 10
|
||||||
requestProcessors = 1
|
requestProcessors = 1
|
||||||
|
|
||||||
getMut = sync.NewRWMutex()
|
getMut = sync.NewMutex()
|
||||||
getLRUCache *lru.Cache
|
getLRUCache *lru.Cache
|
||||||
|
|
||||||
postMut = sync.NewRWMutex()
|
postMut = sync.NewMutex()
|
||||||
postLRUCache *lru.Cache
|
postLRUCache *lru.Cache
|
||||||
|
|
||||||
requests chan request
|
requests chan request
|
||||||
@ -583,22 +583,21 @@ func evict(relay *relay) func() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func limit(addr string, cache *lru.Cache, lock sync.RWMutex, intv time.Duration, burst int) bool {
|
func limit(addr string, cache *lru.Cache, lock sync.Mutex, intv time.Duration, burst int) bool {
|
||||||
if host, _, err := net.SplitHostPort(addr); err == nil {
|
if host, _, err := net.SplitHostPort(addr); err == nil {
|
||||||
addr = host
|
addr = host
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.RLock()
|
lock.Lock()
|
||||||
bkt, ok := cache.Get(addr)
|
bkt, ok := cache.Get(addr)
|
||||||
lock.RUnlock()
|
|
||||||
if ok {
|
if ok {
|
||||||
|
lock.Unlock()
|
||||||
bkt := bkt.(*rate.Limiter)
|
bkt := bkt.(*rate.Limiter)
|
||||||
if !bkt.Allow() {
|
if !bkt.Allow() {
|
||||||
// Rate limit
|
// Rate limit
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lock.Lock()
|
|
||||||
cache.Add(addr, rate.NewLimiter(rate.Every(intv), burst))
|
cache.Add(addr, rate.NewLimiter(rate.Every(intv), burst))
|
||||||
lock.Unlock()
|
lock.Unlock()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user