A Java implementation of the Token Bucket algorithm.
You can download this library from https://search.maven.org/search?q=g:com.github.cowwoc.token-bucket or using the following Maven dependency:
<dependency>
<groupId>com.github.cowwoc.token-bucket</groupId>
<artifactId>token-bucket</artifactId>
<version>6.0</version>
</dependency>
// 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();
}
- 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