Skip to content

Latest commit

 

History

History
61 lines (48 loc) · 1.8 KB

README.md

File metadata and controls

61 lines (48 loc) · 1.8 KB

Maven Central build-status

checklist Token-Bucket

API Changelog

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>6.0</version>
</dependency>

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