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:
commit
4413fdb0c4
@ -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.
|
||||
|
@ -87,9 +87,9 @@ func peekCheckLimited(t *testing.T, limiter *Limiter, shouldbe, stringer bool) {
|
||||
t.Fatalf("dummyTicker does not exist in ratelimiter at all!")
|
||||
}
|
||||
case limited && shouldbe:
|
||||
t.Logf("dummyTicker is limited as expected.")
|
||||
t.Logf("dummyTicker is limited (pass).")
|
||||
case !limited && !shouldbe:
|
||||
t.Logf("dummyTicker is not limited as expected.")
|
||||
t.Logf("dummyTicker is not limited (pass).")
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user