@@ -31,9 +31,9 @@ type Limiter interface {
31
31
// Rule returns the key and rate limit rule for the request
32
32
Rule (r * http.Request ) (rule * Rule , err error )
33
33
// ShouldSetXRateLimitHeaders returns true if the X-RateLimit-* headers should be set
34
- ShouldSetXRateLimitHeaders (* LimitError ) bool
34
+ ShouldSetXRateLimitHeaders (* Context ) bool
35
35
// OnRequestLimit returns the handler to be called when the rate limit is exceeded
36
- OnRequestLimit (* LimitError ) http.HandlerFunc
36
+ OnRequestLimit (* Context ) http.HandlerFunc
37
37
38
38
// Get returns the current count for the key and window
39
39
Get (key string , window time.Time ) (count int , err error ) //nostyle:getters
@@ -115,23 +115,23 @@ func (lm *limitMw) Handler(next http.Handler) http.Handler {
115
115
case <- ctx .Done ():
116
116
// Increment must be called even if the request limit is already exceeded
117
117
if err := lh .limiter .Increment (lh .key , currWindow ); err != nil {
118
- return newLimitError (http .StatusInternalServerError , err , lh )
118
+ return newContext (http .StatusInternalServerError , err , lh )
119
119
}
120
120
return nil
121
121
default :
122
122
}
123
123
rate , err := lh .status (now , currWindow )
124
124
if err != nil {
125
- return newLimitError (http .StatusPreconditionRequired , err , lh )
125
+ return newContext (http .StatusPreconditionRequired , err , lh )
126
126
}
127
127
nrate := int (math .Round (rate ))
128
128
if nrate >= lh .reqLimit {
129
- return newLimitError (http .StatusTooManyRequests , ErrRateLimitExceeded , lh )
129
+ return newContext (http .StatusTooManyRequests , ErrRateLimitExceeded , lh )
130
130
}
131
131
132
132
lh .rateLimitRemaining = lh .reqLimit - nrate
133
133
if err := lh .limiter .Increment (lh .key , currWindow ); err != nil {
134
- return newLimitError (http .StatusInternalServerError , err , lh )
134
+ return newContext (http .StatusInternalServerError , err , lh )
135
135
}
136
136
return nil
137
137
})
@@ -144,7 +144,7 @@ func (lm *limitMw) Handler(next http.Handler) http.Handler {
144
144
// Wait for all limiters to finish
145
145
if err := eg .Wait (); err != nil {
146
146
// Handle first error
147
- if e , ok := err .(* LimitError ); ok {
147
+ if e , ok := err .(* Context ); ok {
148
148
if e .lh .limiter .ShouldSetXRateLimitHeaders (e ) {
149
149
w .Header ().Set ("X-RateLimit-Limit" , fmt .Sprintf ("%d" , e .lh .reqLimit ))
150
150
w .Header ().Set ("X-RateLimit-Remaining" , fmt .Sprintf ("%d" , e .lh .rateLimitRemaining ))
0 commit comments