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

Get actual count of tokens in a Bucket #31

Closed
stemau98 opened this issue May 5, 2017 · 8 comments
Closed

Get actual count of tokens in a Bucket #31

stemau98 opened this issue May 5, 2017 · 8 comments
Assignees

Comments

@stemau98
Copy link

stemau98 commented May 5, 2017

This is more a question than a issue. Hopefully anyone can help me!
Is it possible to determine the amount of token in a bucket at runtime?
I' ve read the docs but I don't see any possibility to do so?

After bucket.tryConsume(1) I need the amount of tokens left in the bucket!

@vladimir-bukhtoyarov
Copy link
Collaborator

vladimir-bukhtoyarov commented May 5, 2017

Yes, it is possible. Before answering, I need to know your usecase, please describe it a little bit. Do you use local or distributed(JCache) bucket? What you plan to do with information about remaining tokens?

@stemau98
Copy link
Author

stemau98 commented May 5, 2017

I'm using distributed buckets to rate the limit for API calls. And I would like to set header fields like X-Rate-Limit-Remaining with the remaining tokens.

@vladimir-bukhtoyarov
Copy link
Collaborator

There is tricky way which I use for unit testing:

Bucket bucket = null;
BucketState snapshot = bucket.createSnapshot();
long availableTokens = snapshot.getAvailableTokens(bucket.getConfiguration().getBandwidths());

But it is not designed to be used for production, and especially will be a performance penalty for distributed case, because createSnapshot performs network request.

Your usecase looks reasonable for me, I will add new tryConsume variant, which returns both consumption result and remaining tokens, just wait for a week.

@stemau98
Copy link
Author

stemau98 commented May 6, 2017

Great to hear this! Thanks for your great support!

@vladimir-bukhtoyarov
Copy link
Collaborator

This issue is tightly related with #35, need to implement both together.

@vladimir-bukhtoyarov
Copy link
Collaborator

vladimir-bukhtoyarov commented May 16, 2017

Implemented. "2.1" will be released soon. Example of usage can be found in the bottom of this page.

@stemau98
Copy link
Author

Great work! Thanks

@vladimir-bukhtoyarov
Copy link
Collaborator

version 2.1.0 has been released

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

No branches or pull requests

2 participants