token-bucket

Maven Central build-status

This is a Java implementation of the Token Bucket algorithm.

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:

<dependency>
  <groupId>com.github.cowwoc.token-bucket</groupId>
  <artifactId>token-bucket</artifactId>
  <version>2.0</version>
</dependency>

Usage

Bucket bucket = new Bucket();

int tokensPerPeriod = 5;
Duration period = Duration.ofSeconds(1);
long initialTokens = 0;
long maxTokens = 120;
bucket.addLimit(new Limit(tokensPerPeriod, period, initialTokens, maxTokens));


// Polls a server with a limit of 5 requests per second and a maximum burst of 120 requests.
while (true)
{
  bucket.consume();
  client.pollServer();
}

License