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 rate type does not match TocketBucketGroup rate #14

Closed
tarmath opened this issue Jul 11, 2020 · 16 comments
Closed

RateLimiter rate type does not match TocketBucketGroup rate #14

tarmath opened this issue Jul 11, 2020 · 16 comments

Comments

@tarmath
Copy link

tarmath commented Jul 11, 2020

RateLimiter's rate is a Float, while that of TocketBucketGroup is a Double.

Since RateLimiter's rate is only used to create the TokenBucketGroup, it should likely be a Double as well...?

@tarmath
Copy link
Author

tarmath commented Jul 11, 2020

If I may also ask at the same time, I am curious if you have a general recommendation / experience on the maximum number of buckets you would generally recommend this solution for?

Thanks for this great library!

@sief
Copy link
Owner

sief commented Jul 12, 2020

thanks @tarmath. The rate parameter in the constructor of TokenBucketGroup was changed to Double because it is later used to calculate the ratePerNano, it saves a call to toDouble. But with the require(rate >= 0.000001f) it is questionable if ratePerNano has to be a Double in the first place.

@sief
Copy link
Owner

sief commented Jul 12, 2020

I still think it's safer to keep it as is, somebody might change or remove the require without taking taking this into account.

@sief
Copy link
Owner

sief commented Jul 12, 2020

Regarding the number of buckets: the only limit I can think of is system resources. The bucket map only contains buckets that are not full, so under normal conditions it should be empty. I did some stress tests a while ago and the impact on throughput seemed to be negilible. I've been using this lib in production with multiple TokenBucketGroup instances for many years and have not encountered any memory or other resource consuption problems so far.

@tarmath
Copy link
Author

tarmath commented Jul 17, 2020

It does seem a bit strange to keep it a mix of Float and Double rather than consolidate on one (no matter which), but happy to close this issue regardless as that's not something that would cause issues in this context anyhow...

@tarmath tarmath closed this as completed Jul 17, 2020
@tarmath
Copy link
Author

tarmath commented Jul 17, 2020

(assuming you were waiting for me to close it)

@tarmath
Copy link
Author

tarmath commented Jul 18, 2020

just noticed, similarly: Bucket size is Int, but consume returns a Long...

@sief
Copy link
Owner

sief commented Jul 18, 2020

bucket size seems to be Long, too:

class TokenBucketGroup(size: Long, rate: Double, clock: Clock = CurrentTimeClock)

What am I missing here?

@tarmath
Copy link
Author

tarmath commented Jul 18, 2020

Right, but when creating these through RateLimiter, size is Int and rate is Float:

https://github.com/sief/play-guard/blob/master/module/app/com/digitaltangible/playguard/RateLimitActionFilter.scala#L152-L156

yet the only thing these 2 arguments do, is be passed to the TockenBucketGroup...

@tarmath
Copy link
Author

tarmath commented Jul 18, 2020

As a sidenote, I am currently returning in the response headers the limit and number of remaining tokens, somewhat mimicking github's v3 APIs as documented here:

https://developer.github.com/v3/rate_limit/#response

In order for me to return the limit (aka bucket size), I currently have to hold that value somewhere else since it is not exposed by the RateLimiter / TocketBucketGroup.

Simply declaring size as val would be enough to make this work. Perhaps something you might be interested in changing?

Thank you again!

@sief
Copy link
Owner

sief commented Jul 19, 2020

right, the RateLimiter should use Long for the size, there is no require limit on size anyway.

Declaring size as val is fine for me, I'd even make rate a val as well.

Are you interested in making a PR?

@tarmath
Copy link
Author

tarmath commented Jul 19, 2020

I already would have, but my hands are tied unfortunately...

@sief
Copy link
Owner

sief commented Jul 20, 2020

ok, done, I changed RateLimiter.rate to Double as well ;)

@tarmath
Copy link
Author

tarmath commented Jul 20, 2020

oh great! fantastic! much appreciated! 🙏

@tarmath
Copy link
Author

tarmath commented Jul 20, 2020

can these changes be released?

@sief
Copy link
Owner

sief commented Jul 20, 2020

releasing to maven central is quite a hassle, I'll see what I can do next week.

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

No branches or pull requests

2 participants