Skip to content

cowwoc/token-bucket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A Java implementation of the Token Bucket algorithm

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages