1
2
mirror of https://github.com/yunginnanet/Rate5 synced 2024-06-29 18:40:48 +00:00

Merge pull request #10 from yunginnanet/janitor-adjust

Fix: reduce mutex lock ops
This commit is contained in:
kayos 2024-01-08 00:33:25 -08:00 committed by GitHub
commit 4413fdb0c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 4 deletions

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

@ -87,9 +87,9 @@ func peekCheckLimited(t *testing.T, limiter *Limiter, shouldbe, stringer bool) {
t.Fatalf("dummyTicker does not exist in ratelimiter at all!") t.Fatalf("dummyTicker does not exist in ratelimiter at all!")
} }
case limited && shouldbe: case limited && shouldbe:
t.Logf("dummyTicker is limited as expected.") t.Logf("dummyTicker is limited (pass).")
case !limited && !shouldbe: case !limited && !shouldbe:
t.Logf("dummyTicker is not limited as expected.") t.Logf("dummyTicker is not limited (pass).")
} }
} }