A simple, tested implementation of a 'token bucket' algorithm.
  • JavaScript 100%
Find a file
2016-05-12 15:18:29 -07:00
test Allow bursting 2016-05-12 15:18:29 -07:00
.gitignore Initial commit 2016-04-13 01:51:07 -07:00
.jsbeautifyrc Initial commit 2016-04-13 01:51:07 -07:00
.jshintrc Initial commit 2016-04-13 01:51:07 -07:00
index.js Allow bursting 2016-05-12 15:18:29 -07:00
package.json Allow bursting 2016-05-12 15:18:29 -07:00
README.md Initial commit 2016-04-13 01:51:07 -07:00

tokenpipe

A simple, tested implementation of a 'token bucket' algorithm.

Code Example

const TokenPipe = require( 'tokenpipe' );
const tokenPipe = TokenPipe( {
    rate: '10/second'
} );

(function example() {
    if ( tokenPipe.consume() ) {
        console.log( 'below rate limit' );
    }
    else {
        console.log( 'over rate limit' );
    }

    setTimeout( example, Math.random() * 100 );
})();

Motivation

The various token bucket implementations I came across either:

  • didn't have tests
  • weren't standalone, were part of a larger project
  • were complex to configure

Installation

npm install --save tokenpipe

API Reference

factory method

const TokenPipe = require( 'tokenpipe' );

const defaultPipe = TokenPipe(); // defaults to 1/s

const rateDefinedPipe = TokenPipe( {
    rate: '10/minute'
} );

const countAndPeriodDefinedPipe = TokenPipe( {
    count: 10,
    period: 1000 * 60
} );

Object.assign

const TokenPipe = require( 'tokenpipe' ).TokenPipe;

const tokenPipe = Object.assign( {}, TokenPipe, {
    count: 10,
    period: 1000 * 60;
} );

TokenPipe.consume()

const TokenPipe = require( 'tokenpipe' );
const tokenPipe = TokenPipe( {
    rate: '10/s'
} );

tokenPipe.consume(); // returns true if there are available tokens, false otherwise

Tests

npm run test

Contributors

If you'd like to contribute:

  1. Add tests for any new feature or bugfix
  2. Ensure your code passes jshint according to the .jshintrc
  3. Ensure your code is formatted according to the .jsbeautifyrc
  4. Submit a Pull Request

License

MIT