-
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?
Conversation
nice one |
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 don't think this is right. Imho, Limit=0
should mean "allow 0 requests" ("always block").
What would you think of
httprate.WithNoLimit()
(akaLimit=-1
)- or zero increment
httprate.WithIncrement(0)
instead?
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.
Few nits, I'll commit the changes directly
func getIncrement(ctx context.Context) (int, bool) { | ||
value, ok := ctx.Value(incrementKey).(int) | ||
return value, ok | ||
} |
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
increment, ok := getIncrement(r.Context())
if !ok {
increment = 1
}
// If the increment is 0, we do not limit
if increment == 0 {
return false
}
} | ||
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
we should be sending some headers back
X-RateLimit-Limit: 0
X-RateLimit-Remaining: 0
} | ||
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
we should set X-RateLimit-Increment: 0
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.
This PR adds the capability of skipping rate limit by specifying 0