Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware): Fix Allow method of RateLimiterMemoryStore #2618

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions middleware/rate_limiter.go
Expand Up @@ -238,17 +238,17 @@ var DefaultRateLimiterMemoryStoreConfig = RateLimiterMemoryStoreConfig{
// Allow implements RateLimiterStore.Allow
func (store *RateLimiterMemoryStore) Allow(identifier string) (bool, error) {
store.mutex.Lock()
now := store.timeNow()
if now.Sub(store.lastCleanup) > store.expiresIn {
store.cleanupStaleVisitors()
}
limiter, exists := store.visitors[identifier]
if !exists {
limiter = new(Visitor)
limiter.Limiter = rate.NewLimiter(store.rate, store.burst)
store.visitors[identifier] = limiter
}
now := store.timeNow()
limiter.lastSeen = now
if now.Sub(store.lastCleanup) > store.expiresIn {
store.cleanupStaleVisitors()
}
store.mutex.Unlock()
return limiter.AllowN(store.timeNow(), 1), nil
}
Expand Down
35 changes: 33 additions & 2 deletions middleware/rate_limiter_test.go
Expand Up @@ -308,7 +308,12 @@ func TestRateLimiterWithConfig_beforeFunc(t *testing.T) {
}

func TestRateLimiterMemoryStore_Allow(t *testing.T) {
var inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3, ExpiresIn: 2 * time.Second})
var (
inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3, ExpiresIn: 2 * time.Second})
now = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
)
inMemoryStore.lastCleanup = now

testCases := []struct {
id string
allowed bool
Expand Down Expand Up @@ -342,7 +347,33 @@ func TestRateLimiterMemoryStore_Allow(t *testing.T) {
for i, tc := range testCases {
t.Logf("Running testcase #%d => %v", i, time.Duration(i)*220*time.Millisecond)
inMemoryStore.timeNow = func() time.Time {
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Add(time.Duration(i) * 220 * time.Millisecond)
return now.Add(time.Duration(i) * 220 * time.Millisecond)
}
allowed, _ := inMemoryStore.Allow(tc.id)
assert.Equal(t, tc.allowed, allowed)
}
}

func TestRateLimiterMemoryStore_Allow_ExpiresIn(t *testing.T) {
var (
inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 1, ExpiresIn: 220 * time.Millisecond})
now = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
)
inMemoryStore.lastCleanup = now

testCases := []struct {
id string
allowed bool
}{
{"127.0.0.1", true}, // 0 ms
{"127.0.0.1", true}, // 221 ms, interval exceeds expiresIn
{"127.0.0.1", true}, // 442 ms, interval exceeds expiresIn
}

for i, tc := range testCases {
t.Logf("Running testcase #%d => %v", i, time.Duration(i)*220*time.Millisecond)
inMemoryStore.timeNow = func() time.Time {
return now.Add(time.Duration(i) * 221 * time.Millisecond)
}
allowed, _ := inMemoryStore.Allow(tc.id)
assert.Equal(t, tc.allowed, allowed)
Expand Down