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

Throttle #97

Merged
merged 5 commits into from
Jul 3, 2021
Merged

Throttle #97

merged 5 commits into from
Jul 3, 2021

Conversation

umputun
Copy link
Owner

@umputun umputun commented Jun 23, 2021

implements simple throttling #91

Copy link

@fkirill fkirill left a comment

Choose a reason for hiding this comment

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

This certainly will work, see a couple of nuanced comments.

}
}

if httpError := tollbooth.LimitByKeys(lmt, keys); httpError != nil {
Copy link

Choose a reason for hiding this comment

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

There appears to be a bug in tollbooth, where they stop iterating over the keys as soon as they detect that the error is returned.
However, we actually need to iterate over all keys all the time, otherwise, we're leaking limiter permits (or, more precisely under-counting the permits for the keys that follow the first key that exceeded the limit).
Potentially worth copying the method from tollbooth and re-implementing it here.

lmt := tollbooth.NewLimiter(float64(reqSec), nil)
fn := func(w http.ResponseWriter, r *http.Request) {
if httpError := tollbooth.LimitByKeys(lmt, []string{"system"}); httpError != nil {
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
Copy link

Choose a reason for hiding this comment

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

Well... Technically it's too early to return an error here. We also need to go and check routes before returning. Essentially we need to check all the keys even though we already know that the request should not be admitted into the system (see also the comment below for the explanation).

Copy link
Owner Author

Choose a reason for hiding this comment

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

I see what you mean in both comments, good points. But I think we may ignore both issues for now and undercount hits in those cases. Practically it will limit excessive activity good enough, not as precise as it can but still ok.

@umputun umputun merged commit 7103968 into master Jul 3, 2021
@umputun umputun deleted the throttle branch July 3, 2021 06:23
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.

2 participants