Fix[performance]: Don't call DeleteExpired unnecessarily often to avoid superfluous mutex locks

This commit is contained in:
kayos@tcp.direct 2024-01-08 00:25:47 -08:00
parent ec76a571d0
commit fb8ddac1b2
Signed by: kayos
GPG Key ID: 4B841471B4BEE979

View File

@ -79,7 +79,7 @@ func newLimiter(policy Policy) *Limiter {
window := time.Duration(policy.Window) * time.Second
return &Limiter{
Ruleset: policy,
Patrons: cache.New(window, 1*time.Second),
Patrons: cache.New(window, time.Duration(policy.Window)*time.Second),
known: make(map[interface{}]*int64),
RWMutex: &sync.RWMutex{},
debugMutex: &sync.RWMutex{},
@ -135,7 +135,6 @@ func (q *Limiter) Check(from Identity) (limited bool) {
var count int64
var err error
src := from.UniqueKey()
q.Patrons.DeleteExpired()
count, err = q.Patrons.IncrementInt64(src, 1)
if err != nil {
// IncrementInt64 should only error if the value is not an int64, so we can assume it's a new key.