Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

klaidliadon
Copy link
Contributor

This PR adds the capability of skipping rate limit by specifying 0

@pkieltyka
Copy link
Member

nice one

Copy link
Contributor

@VojtechVitek VojtechVitek left a 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

  1. httprate.WithNoLimit() (aka Limit=-1)
  2. or zero increment httprate.WithIncrement(0)

instead?

Copy link
Contributor

@VojtechVitek VojtechVitek left a 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

Comment on lines +18 to +21
func getIncrement(ctx context.Context) (int, bool) {
value, ok := ctx.Value(incrementKey).(int)
return value, ok
}
Copy link
Contributor

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

Copy link
Contributor Author

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
	}

context.go Outdated Show resolved Hide resolved
}
// If the limit is set to 0, we are always over limit
if limit == 0 {
return true
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants