-
Notifications
You must be signed in to change notification settings - Fork 20
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
Make TokenBucketGroup a trait to allow easier customization #7
Comments
Ended up rolling a fully custom solution. The TokenBucketGroup-equivalent part:
This way there's no synchronization and only constant-time operations in the critical-path consume call, and the cleanup / adding of new tokens is done at a fixed rate in the background. The tradeoff is that it's no longer 100% exact, so there will be an extra connection allowed through every now and then, and if the background job gets stalled for whatever reason, then there will be no new tokens granted => request stoppage. Anyway, feel free to use or ignore, I'll close this issue in the meantime. |
sorry for the late response, I somehow didn't notice the issue. I've also been thinking about a more relaxed approach in favour of performance. But when I did some throughput measurements I was surprised that the bucket rebuild cost seems to be negligible. Did you notice any performance issues? |
If you create a trait:
and rename the current class to
DefaultTokenBucketGroup
, then it would be easier to customize the TokenBucketGroup implementation - there are tradeoffs between exactness and computational complexity, the current implementation is exact but at the cost of doing up torate
bucket rebuilds per second, which is totally fine at 10/s, but less so at 10k/s.I would also consider changing the return type from Long to Boolean since it's only (intended to be?) used in a boolean way - that would also hide more of the TokenBucketGroup internals.
(Yes, it's of course possible to just subclass and ignore the private members of the current TokenBucketGroup)
(Happy to provide a pull request, wanted to check for interest first)
The text was updated successfully, but these errors were encountered: