Skip to content

robertmircea/throttle

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Throttling API Build Status

Overview

API to throttle/rate-limit requests

This API implements two popular throttling strategies, namely:

  1. Fixed token bucket
  2. Leaky token buckt

Fixed token bucket

Details for this implementation can be found at: Token Bucket

Leaky token bucket

Details for this implementation can be found at: Leaky Bucket With in the API, Leaky buckets have been implemented as two types

  1. StepDownLeakyTokenBucket
  2. StepUpLeakyTokenBucket

StepDownLeakyTokenBucket resembles a bucket which has been filled with tokens at the beginning but subsequently leaks tokens at a fixed interval. StepUpLeakyTokenBucket resemembles an empty bucket at the beginning but get filled will tokens over a fixed interval.

Examples

Fixed Bucket Example

// construct strategy
ThrottleStrategy strategy = new FixedTokenBucket(100, 1, TimeUnit.MINUTES);

// provide the strategy to the throttler
Throttle throttle = new Throttle(strategy);

// throttle :)
boolean isThrottled = throttle.canProceed();

if(!isThrottled){ 
  // your logic
}

Step Up Leaky Bucket Example

// construct strategy
ThrottleStrategy strategy = new StepUpLeakyTokenBucket(100, 1, TimeUnit.MINUTES, 25, 15, TimeUnit.SECONDS);

// provide the strategy to the throttler
Throttle throttle = new Throttle(strategy);

// throttle :)
boolean isThrottled = throttle.canProceed();

if(!isThrottled){ 
  // your logic
}

Step Down Leaky Bucket Example

// construct strategy
ThrottleStrategy strategy = new StepDownLeakyTokenBucket(100, 1, TimeUnit.MINUTES, 25, 15, TimeUnit.SECONDS);

// provide the strategy to the throttler
Throttle throttle = new Throttle(strategy);

// throttle :)
boolean isThrottled = throttle.canProceed();

if(!isThrottled){ 
  // your logic
}

About

API to throttle/rate-limit requests

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published