https://github.com/cowwoc/token-bucket
A Java implementation of the Token Bucket algorithm
https://github.com/cowwoc/token-bucket
java limiter throttling token-bucket
Last synced: 9 months ago
JSON representation
A Java implementation of the Token Bucket algorithm
- Host: GitHub
- URL: https://github.com/cowwoc/token-bucket
- Owner: cowwoc
- License: apache-2.0
- Created: 2020-04-15T23:35:52.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-01T21:18:15.000Z (about 3 years ago)
- Last Synced: 2025-07-28T09:50:02.314Z (11 months ago)
- Topics: java, limiter, throttling, token-bucket
- Language: Java
- Homepage:
- Size: 642 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://search.maven.org/search?q=g:com.github.cowwoc.token-bucket)
[](../../actions?query=workflow%3ABuild)
#
Token-Bucket
[](https://cowwoc.github.io/token-bucket/6.0/docs/api/)
[](wiki/Changelog.md)
A Java implementation of the [Token Bucket algorithm](https://en.wikipedia.org/wiki/Token_bucket).
# Download
You can download this library from https://search.maven.org/search?q=g:com.github.cowwoc.token-bucket or using
the following Maven dependency:
```
com.github.cowwoc.token-bucket
token-bucket
6.0
```
# Usage
```
// Allow 60 requests per minute, with a maximum burst of 120 requests.
Bucket server1 = Bucket.builder().
addLimit(limit -> limit.
tokensPerPeriod(60).
period(Duration.ofMinute(1)).
maxTokens(120).
build()).
build();
// Allow 60 requests per minute, with a maximum burst of 10 requests per second.
Bucket server2 = Bucket.builder().
addLimit(limit -> limit.
tokensPerPeriod(60).
period(Duration.ofMinute(1)).
build()).
addLimit(limit -> limit.
tokensPerPeriod(10).
period(Duration.ofSecond(1)).
build()).
build();
// Choose a server and send requests
Bucket bucket = server1;
while (true)
{
bucket.consume();
client.pollServer();
}
```
# License
* Code licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
* Icons from www.svgrepo.com licensed under the CC0 License