-
Notifications
You must be signed in to change notification settings - Fork 18
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
Use WithRequestLimit
with 0
to skip rate limit
#46
base: master
Are you sure you want to change the base?
Changes from 2 commits
c32694f
7eaea8f
ac748ea
9719e45
1677b6d
37be242
8d182db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,12 +72,35 @@ func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string | |
currentWindow := time.Now().UTC().Truncate(l.windowLength) | ||
ctx := r.Context() | ||
|
||
limit := l.requestLimit | ||
if val := getRequestLimit(ctx); val > 0 { | ||
limit = val | ||
setHeader(w, l.headers.Reset, fmt.Sprintf("%d", currentWindow.Add(l.windowLength).Unix())) | ||
|
||
limit, ok := getRequestLimit(ctx) | ||
if !ok { | ||
limit = l.requestLimit | ||
} | ||
// If the limit is set to 0, we are always over limit | ||
if limit == 0 { | ||
return true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should be sending some headers back X-RateLimit-Limit: 0 |
||
} | ||
// If the limit is set to -1, we are never over limit | ||
if limit == _NoLimit { | ||
return false | ||
} | ||
|
||
increment, ok := getIncrement(r.Context()) | ||
if !ok { | ||
increment = 1 | ||
} | ||
// If the increment is 0, we are always on limit | ||
if increment == 0 { | ||
return false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
setHeader(w, l.headers.Limit, fmt.Sprintf("%d", limit)) | ||
setHeader(w, l.headers.Reset, fmt.Sprintf("%d", currentWindow.Add(l.windowLength).Unix())) | ||
|
||
if increment > 1 { | ||
setHeader(w, l.headers.Increment, fmt.Sprintf("%d", increment)) | ||
} | ||
|
||
l.mu.Lock() | ||
_, rateFloat, err := l.calculateRate(key, limit) | ||
|
@@ -88,11 +111,6 @@ func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string | |
} | ||
rate := int(math.Round(rateFloat)) | ||
|
||
increment := getIncrement(r.Context()) | ||
if increment > 1 { | ||
setHeader(w, l.headers.Increment, fmt.Sprintf("%d", increment)) | ||
} | ||
|
||
if rate+increment > limit { | ||
setHeader(w, l.headers.Remaining, fmt.Sprintf("%d", limit-rate)) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on no ctx key, we should by default return 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do it in the only place where this is used: https://github.com/klaidliadon/httprate/blob/ac748ea5a6b8921aef7a442985c77657a989932f/limiter.go#L90-L97