Skip to content

Commit d2295db

Browse files
committed
Rename LimitError to Context
1 parent dbd6772 commit d2295db

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

error.go renamed to context.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"time"
66
)
77

8-
var _ error = &LimitError{}
8+
var _ error = &Context{}
99
var ErrRateLimitExceeded error = errors.New("rate limit exceeded")
1010

11-
// LimitError is the error returned by the middleware.
12-
type LimitError struct {
11+
// Context is the error returned by the middleware.
12+
type Context struct {
1313
StatusCode int
1414
Err error
1515
Limiter Limiter
@@ -20,8 +20,8 @@ type LimitError struct {
2020
lh *limitHandler
2121
}
2222

23-
func newLimitError(statusCode int, err error, lh *limitHandler) *LimitError {
24-
return &LimitError{
23+
func newContext(statusCode int, err error, lh *limitHandler) *Context {
24+
return &Context{
2525
StatusCode: statusCode,
2626
Err: err,
2727
Limiter: lh.limiter,
@@ -34,6 +34,6 @@ func newLimitError(statusCode int, err error, lh *limitHandler) *LimitError {
3434
}
3535

3636
// Error returns the error message.
37-
func (e *LimitError) Error() string {
37+
func (e *Context) Error() string {
3838
return e.Err.Error()
3939
}

rl.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ type Limiter interface {
3131
// Rule returns the key and rate limit rule for the request
3232
Rule(r *http.Request) (rule *Rule, err error)
3333
// ShouldSetXRateLimitHeaders returns true if the X-RateLimit-* headers should be set
34-
ShouldSetXRateLimitHeaders(*LimitError) bool
34+
ShouldSetXRateLimitHeaders(*Context) bool
3535
// OnRequestLimit returns the handler to be called when the rate limit is exceeded
36-
OnRequestLimit(*LimitError) http.HandlerFunc
36+
OnRequestLimit(*Context) http.HandlerFunc
3737

3838
// Get returns the current count for the key and window
3939
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 {
115115
case <-ctx.Done():
116116
// Increment must be called even if the request limit is already exceeded
117117
if err := lh.limiter.Increment(lh.key, currWindow); err != nil {
118-
return newLimitError(http.StatusInternalServerError, err, lh)
118+
return newContext(http.StatusInternalServerError, err, lh)
119119
}
120120
return nil
121121
default:
122122
}
123123
rate, err := lh.status(now, currWindow)
124124
if err != nil {
125-
return newLimitError(http.StatusPreconditionRequired, err, lh)
125+
return newContext(http.StatusPreconditionRequired, err, lh)
126126
}
127127
nrate := int(math.Round(rate))
128128
if nrate >= lh.reqLimit {
129-
return newLimitError(http.StatusTooManyRequests, ErrRateLimitExceeded, lh)
129+
return newContext(http.StatusTooManyRequests, ErrRateLimitExceeded, lh)
130130
}
131131

132132
lh.rateLimitRemaining = lh.reqLimit - nrate
133133
if err := lh.limiter.Increment(lh.key, currWindow); err != nil {
134-
return newLimitError(http.StatusInternalServerError, err, lh)
134+
return newContext(http.StatusInternalServerError, err, lh)
135135
}
136136
return nil
137137
})
@@ -144,7 +144,7 @@ func (lm *limitMw) Handler(next http.Handler) http.Handler {
144144
// Wait for all limiters to finish
145145
if err := eg.Wait(); err != nil {
146146
// Handle first error
147-
if e, ok := err.(*LimitError); ok {
147+
if e, ok := err.(*Context); ok {
148148
if e.lh.limiter.ShouldSetXRateLimitHeaders(e) {
149149
w.Header().Set("X-RateLimit-Limit", fmt.Sprintf("%d", e.lh.reqLimit))
150150
w.Header().Set("X-RateLimit-Remaining", fmt.Sprintf("%d", e.lh.rateLimitRemaining))

testutil/limiter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func (l *Limiter) Rule(r *http.Request) (*rl.Rule, error) {
5858
}, nil
5959
}
6060

61-
func (l *Limiter) ShouldSetXRateLimitHeaders(le *rl.LimitError) bool {
61+
func (l *Limiter) ShouldSetXRateLimitHeaders(le *rl.Context) bool {
6262
return true
6363
}
6464

65-
func (l *Limiter) OnRequestLimit(le *rl.LimitError) http.HandlerFunc {
65+
func (l *Limiter) OnRequestLimit(le *rl.Context) http.HandlerFunc {
6666
return func(w http.ResponseWriter, r *http.Request) {
6767
if l.statusCode != 0 {
6868
w.WriteHeader(l.statusCode)

0 commit comments

Comments
 (0)