File tree 1 file changed +17
-9
lines changed
1 file changed +17
-9
lines changed Original file line number Diff line number Diff line change 5
5
"golang.org/x/time/rate"
6
6
"net/http"
7
7
"sync"
8
+ "time"
8
9
)
9
10
10
11
// RateLimiterStore is the interface to be implemented by custom stores.
@@ -73,27 +74,34 @@ func RateLimiterWithConfig(config RateLimiterConfig) echo.MiddlewareFunc {
73
74
}
74
75
75
76
// RateLimiterMemoryStore is the built-in store implementation for RateLimiter
76
- type RateLimiterMemoryStore struct {
77
- visitors map [string ]* rate.Limiter
78
- mutex sync.Mutex
79
- rate rate.Limit
80
- burst int
81
- }
77
+ type (
78
+ RateLimiterMemoryStore struct {
79
+ visitors map [string ]visitor
80
+ mutex sync.Mutex
81
+ rate rate.Limit
82
+ burst int
83
+ }
84
+ visitor struct {
85
+ * rate.Limiter
86
+ lastSeen time.Time
87
+ }
88
+ )
82
89
83
90
// Allow implements RateLimiterStore.Allow
84
91
func (store * RateLimiterMemoryStore ) Allow (identifier string ) bool {
85
92
store .mutex .Lock ()
86
93
87
94
if store .visitors == nil {
88
- store .visitors = make (map [string ]* rate. Limiter )
95
+ store .visitors = make (map [string ]visitor )
89
96
}
90
97
91
98
limiter , exists := store .visitors [identifier ]
92
99
if ! exists {
93
- limiter = rate .NewLimiter (store .rate , store .burst )
100
+ limiter .Limiter = rate .NewLimiter (store .rate , store .burst )
101
+ limiter .lastSeen = time .Now ()
94
102
store .visitors [identifier ] = limiter
95
103
}
96
-
104
+ limiter . lastSeen = time . Now ()
97
105
store .mutex .Unlock ()
98
106
return limiter .Allow ()
99
107
}
You can’t perform that action at this time.
0 commit comments