Skip to content

Commit 1ee8e22

Browse files
authored
do not use global timeNow variables (#2477)
1 parent 44ead54 commit 1ee8e22

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

middleware/rate_limiter.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ type (
160160
burst int
161161
expiresIn time.Duration
162162
lastCleanup time.Time
163+
164+
timeNow func() time.Time
163165
}
164166
// Visitor signifies a unique user's limiter details
165167
Visitor struct {
@@ -219,7 +221,8 @@ func NewRateLimiterMemoryStoreWithConfig(config RateLimiterMemoryStoreConfig) (s
219221
store.burst = int(config.Rate)
220222
}
221223
store.visitors = make(map[string]*Visitor)
222-
store.lastCleanup = now()
224+
store.timeNow = time.Now
225+
store.lastCleanup = store.timeNow()
223226
return
224227
}
225228

@@ -244,12 +247,13 @@ func (store *RateLimiterMemoryStore) Allow(identifier string) (bool, error) {
244247
limiter.Limiter = rate.NewLimiter(store.rate, store.burst)
245248
store.visitors[identifier] = limiter
246249
}
247-
limiter.lastSeen = now()
248-
if now().Sub(store.lastCleanup) > store.expiresIn {
250+
now := store.timeNow()
251+
limiter.lastSeen = now
252+
if now.Sub(store.lastCleanup) > store.expiresIn {
249253
store.cleanupStaleVisitors()
250254
}
251255
store.mutex.Unlock()
252-
return limiter.AllowN(now(), 1), nil
256+
return limiter.AllowN(store.timeNow(), 1), nil
253257
}
254258

255259
/*
@@ -258,14 +262,9 @@ of users who haven't visited again after the configured expiry time has elapsed
258262
*/
259263
func (store *RateLimiterMemoryStore) cleanupStaleVisitors() {
260264
for id, visitor := range store.visitors {
261-
if now().Sub(visitor.lastSeen) > store.expiresIn {
265+
if store.timeNow().Sub(visitor.lastSeen) > store.expiresIn {
262266
delete(store.visitors, id)
263267
}
264268
}
265-
store.lastCleanup = now()
269+
store.lastCleanup = store.timeNow()
266270
}
267-
268-
/*
269-
actual time method which is mocked in test file
270-
*/
271-
var now = time.Now

middleware/rate_limiter_test.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package middleware
22

33
import (
44
"errors"
5-
"fmt"
65
"math/rand"
76
"net/http"
87
"net/http/httptest"
@@ -340,7 +339,7 @@ func TestRateLimiterMemoryStore_Allow(t *testing.T) {
340339

341340
for i, tc := range testCases {
342341
t.Logf("Running testcase #%d => %v", i, time.Duration(i)*220*time.Millisecond)
343-
now = func() time.Time {
342+
inMemoryStore.timeNow = func() time.Time {
344343
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Add(time.Duration(i) * 220 * time.Millisecond)
345344
}
346345
allowed, _ := inMemoryStore.Allow(tc.id)
@@ -350,24 +349,22 @@ func TestRateLimiterMemoryStore_Allow(t *testing.T) {
350349

351350
func TestRateLimiterMemoryStore_cleanupStaleVisitors(t *testing.T) {
352351
var inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3})
353-
now = time.Now
354-
fmt.Println(now())
355352
inMemoryStore.visitors = map[string]*Visitor{
356353
"A": {
357354
Limiter: rate.NewLimiter(1, 3),
358-
lastSeen: now(),
355+
lastSeen: time.Now(),
359356
},
360357
"B": {
361358
Limiter: rate.NewLimiter(1, 3),
362-
lastSeen: now().Add(-1 * time.Minute),
359+
lastSeen: time.Now().Add(-1 * time.Minute),
363360
},
364361
"C": {
365362
Limiter: rate.NewLimiter(1, 3),
366-
lastSeen: now().Add(-5 * time.Minute),
363+
lastSeen: time.Now().Add(-5 * time.Minute),
367364
},
368365
"D": {
369366
Limiter: rate.NewLimiter(1, 3),
370-
lastSeen: now().Add(-10 * time.Minute),
367+
lastSeen: time.Now().Add(-10 * time.Minute),
371368
},
372369
}
373370

middleware/request_logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
225225
if config.Skipper == nil {
226226
config.Skipper = DefaultSkipper
227227
}
228-
now = time.Now
228+
now := time.Now
229229
if config.timeNow != nil {
230230
now = config.timeNow
231231
}

0 commit comments

Comments
 (0)