Coder Social home page Coder Social logo

throttle's Introduction

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 bucket

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. StepDownLeakyTokenBucketStrategy
  2. StepUpLeakyTokenBucketStrategy

StepDownLeakyTokenBucketStrategy resembles a bucket which has been filled with tokens at the beginning but subsequently leaks tokens at a fixed interval. StepUpLeakyTokenBucketStrategy 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 FixedTokenBucketStrategy(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 StepUpLeakyTokenBucketStrategy(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 StepDownLeakyTokenBucketStrategy(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
}

Wait For Token Availability Example

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

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

while(!throttle.canProceed()){
	Thread.sleep(throttle.waitTime(TimeUnit.MILLISECONDS));
}

// your logic

throttle's People

Contributors

robertoestivill avatar sudohippie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.