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

View File

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