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

Support for refilling buckets over maximum #151

Closed
shahr opened this issue Jan 20, 2021 · 4 comments
Closed

Support for refilling buckets over maximum #151

shahr opened this issue Jan 20, 2021 · 4 comments
Labels

Comments

@shahr
Copy link

shahr commented Jan 20, 2021

Hi,

I'm an avid fan of bucket4j. Recently I've had a requirement where I need to add more tokens to the bucket than the limit allows. The example scenario is as follows:

  • Let's assume that the user is rate limited on 2 kinds of operations: add and delete.
  • We set the limit to 10 operations over a duration of a second (for example).
  • However, we want to distinguish the type of operations such that when the user tries to perform an add operations more than 10 times within a second, we want to apply rate limit as normal; but if the user performs one delete operation, then an add operation - we want the net rate limit applied to be 0 (i.e. credit the bucket with a token for the delete and debit the bucket for the add operation).
  • The problem comes in where if a user attempts 100 delete operations in a second, the bucket would only be credited with 10 tokens (as that is the bucket's maximum capacity). Subsequently, the user would only abe able to perform 10 add operations instead of 100 (due to prior credits that were issued for the deletes)

Based on the above scenario, would it be possible to introduce a feature to allow setting the number of available tokens to be greater than the maximum capacity of the bucket?

@vladimir-bukhtoyarov
Copy link
Collaborator

vladimir-bukhtoyarov commented Jan 22, 2021

Hello @shahr

It is possible to support this feature. But I will able to do it only in March because of lack of free time.

@shahr
Copy link
Author

shahr commented Jan 22, 2021

That's excellent. Thank you. I am happy to wait. For now, we have a work around to use the normal rate limit and capture overflow in a separate cache then perform some logic to do the accounting. However, once the feature is ready (and I can help test it), we can remove this custom logic.

@vladimir-bukhtoyarov
Copy link
Collaborator

Hello @shahr

Desired functionality has been release under the version 6.1.0. You need to use forceAddTokens instead of addTokens.

@shahr
Copy link
Author

shahr commented Mar 21, 2021

Thanks @vladimir-bukhtoyarov - I'll test this out in the coming week.

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

No branches or pull requests

2 participants