cmd/strelaypoolsrv: Simplify LRU usage (#6507)

This commit is contained in:
greatroar 2020-04-06 12:43:56 +02:00 committed by GitHub
parent 88cabb9e0a
commit 674a99e9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 12 deletions

View File

@ -589,19 +589,15 @@ func limit(addr string, cache *lru.Cache, lock sync.Mutex, intv time.Duration, b
}
lock.Lock()
bkt, ok := cache.Get(addr)
if ok {
lock.Unlock()
bkt := bkt.(*rate.Limiter)
if !bkt.Allow() {
// Rate limit
return true
}
} else {
cache.Add(addr, rate.NewLimiter(rate.Every(intv), burst))
lock.Unlock()
v, _ := cache.Get(addr)
bkt, ok := v.(*rate.Limiter)
if !ok {
bkt = rate.NewLimiter(rate.Every(intv), burst)
cache.Add(addr, bkt)
}
return false
lock.Unlock()
return !bkt.Allow()
}
func loadRelays(file string) []*relay {