Skip to content

Commit

Permalink
Fix[performance]: Don't call DeleteExpired unnecessarily often to avo…
Browse files Browse the repository at this point in the history
…id superfluous mutex locks
  • Loading branch information
yunginnanet committed Jan 8, 2024
1 parent ec76a57 commit fb8ddac
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fb8ddac

Please sign in to comment.