token-bucket

Token bucket algorithm for rate-limiting

License

License

GroupId

GroupId

org.isomorphism
ArtifactId

ArtifactId

token-bucket
Last Version

Last Version

1.7
Release Date

Release Date

Type

Type

jar
Description

Description

token-bucket
Token bucket algorithm for rate-limiting
Project URL

Project URL

http://github.com/bbeck/token-bucket
Source Code Management

Source Code Management

http://github.com/bbeck/token-bucket

Download token-bucket

How to add to project

<!-- https://jarcasting.com/artifacts/org.isomorphism/token-bucket/ -->
<dependency>
    <groupId>org.isomorphism</groupId>
    <artifactId>token-bucket</artifactId>
    <version>1.7</version>
</dependency>
// https://jarcasting.com/artifacts/org.isomorphism/token-bucket/
implementation 'org.isomorphism:token-bucket:1.7'
// https://jarcasting.com/artifacts/org.isomorphism/token-bucket/
implementation ("org.isomorphism:token-bucket:1.7")
'org.isomorphism:token-bucket:jar:1.7'
<dependency org="org.isomorphism" name="token-bucket" rev="1.7">
  <artifact name="token-bucket" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.isomorphism', module='token-bucket', version='1.7')
)
libraryDependencies += "org.isomorphism" % "token-bucket" % "1.7"
[org.isomorphism/token-bucket "1.7"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.guava : guava jar 18.0

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 1.10.19

Project Modules

There are no modules declared in this project.

Introduction

This library provides an implementation of a token bucket algorithm which is useful for providing rate limited access to a portion of code. The implementation provided is that of a "leaky bucket" in the sense that the bucket has a finite capacity and any added tokens that would exceed this capacity will "overflow" out of the bucket and be lost forever.

In this implementation the rules for refilling the bucket are encapsulated in a provided RefillStrategy instance. Prior to attempting to consume any tokens the refill strategy will be consulted to see how many tokens should be added to the bucket

We use Travis CI for build verification. Build Status

See also:

Usage

Using a token bucket is incredibly easy and is best illustrated by an example. Suppose you have a piece of code that polls a website and you would only like to be able to access the site once per second:

// Create a token bucket with a capacity of 1 token that refills at a fixed interval of 1 token/sec.
TokenBucket bucket = TokenBuckets.builder()
  .withCapacity(1)
  .withFixedIntervalRefillStrategy(1, 1, TimeUnit.SECONDS)
  .build();

// ...

while (true) {
  // Consume a token from the token bucket.  If a token is not available this method will block until
  // the refill strategy adds one to the bucket.
  bucket.consume(1);

  poll();
}

As another example suppose you wanted to rate limit the size response of a server to the client to 20 kb/sec but want to allow for a periodic burst rate of 40 kb/sec:

// Create a token bucket with a capacity of 40 kb tokens that refills at a fixed interval of 20 kb tokens per second
TokenBucket bucket = TokenBuckets.builder()
  .withCapacity(40960)
  .withFixedIntervalRefillStrategy(20480, 1, TimeUnit.SECONDS)
  .build();

// ...

while (true) {
  String response = prepareResponse();

  // Consume tokens from the bucket commensurate with the size of the response
  bucket.consume(response.length());

  send(response);
}

Maven Setup

The token bucket library is distributed through maven central. Just include it as a dependency in your pom.xml.

<dependency>
    <groupId>org.isomorphism</groupId>
    <artifactId>token-bucket</artifactId>
    <version>1.6</version>
</dependency>

License

Copyright 2012-2015 Brandon Beck Licensed under the Apache Software License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0.

Versions

Version
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0