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

RateLimiter rejects all events when Limit is between 0 and 1 #1853

Open
3 tasks done
aschiffmann opened this issue Apr 21, 2021 · 1 comment
Open
3 tasks done

RateLimiter rejects all events when Limit is between 0 and 1 #1853

aschiffmann opened this issue Apr 21, 2021 · 1 comment

Comments

@aschiffmann
Copy link
Task lists! Give feedback

@aschiffmann aschiffmann commented Apr 21, 2021

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Issue Description

When using the RateLimiter Middleware with a rate between 0 and 1 all events will be rejected instead of applying the specified rate. E.g.: e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(0.5)))

I am not saying that it is a common use case to have a rate.Limit between 0 and 1, but it is very confusing that NewRateLimiterMemoryStore() accepts a value of type float, the docs don't mention any restrictions for non-zero values but still the argument is interpreted as zero instead of its actual value.

Expected behaviour

One of those:

  • the docs explains how a fractional rate.Limit is handled
  • NewRateLimiterMemoryStore() only accepts integer values
  • the RateLimiter interprets a fractional rate.Limit correctly, e.g. a limit of 0.5 will result in an equivalent rate of 30 requests/minute

Actual behaviour

a fractional rate.Limit between 0 and 1 is surprisingly interpreted like a zero Limit

Version/commit

v4.2.2

@aldas
Copy link
Contributor

@aldas aldas commented Apr 27, 2021

As ratelimiter middleware (default implementation) is wrapper around https://pkg.go.dev/golang.org/x/time/rate library and rate value goes directly to that library I think the docs explains how a fractional rate.Limit is handled would be correct solution.

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

Successfully merging a pull request may close this issue.

None yet
2 participants