Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

leaky-bucket

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

leaky-bucket

A fast and efficient leaky bucket implementation

  • 0.1.11
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

leaky-bucket

A fast and efficient leaky bucket implementation

This module uses sematic versioning

installation

npm i leaky-bucket

build status

Build Status

usage

var LeakyBucket = require('leaky-bucket');

Constructor

The constructor accepts three parameters, all are optional

var instance = new LeakyBucket([capacity = 60]
                               [, Interval = 60]
                               [, maxWaitingTime = 300]);

Create a new leaky bucket which is allowed to execute 120 items per minute

var bucket = new LeakyBucket(120);

Create a new leaky bucket which is allowed to execute 200 items every 30 seconds

var bucket = new LeakyBucket(200, 30);

Create a new leaky bucket which is allowed to execute 200 items every 30 seconds with a maxWaitingTime of 60 seconds

var bucket = new LeakyBucket(200, 30, 60);

You may also use an options object instead of the parameters

var bucket = new LeakyBucket({
      capacity: 200         // items per interval, defaults to 60
    , iterval: 30           // seconds, defaults to 60
    , maxWaitingTime: 60    // seconds, defaults to 300
});

Throttling

The throttle accepts two parameters, of which both are optional

  • The first parameter can either be a callback function or the cost of the opertion
  • The seocnd parameter can be the callback function

If you do not pass a callabck a promise is returned. The first argument of the callback is an error object (or the promise fails) if the item could not be executed becuas the max waiting time was exceeded.

bucktet.throttle([cost], callback);

The cost parameter can be used to let items cost more than other. The cost of one item defaults to 1. If you execute an item with the cost of 2 it will use 2 slots instead of one.

Throttle an item

bucket.throttle(function(err) {
    // do something
});

Throttle an item with the cost of 10

bucket.throttle(10, function(err) {
    // do something
});

Throttle an using Promises

bucket.throttle().then(function() {
    // ok, do your stuff ...
}).catch(function(err) {
    // max waiting time exceeded, dont execute anything
});

Flags

You may start your app using the debug-leaky-bucket flag, this will enable logging for the module

node . --debug-leaky-bucket

Examples

Rate limit API calls on the client side, allowed are no more than 60 requests per minute

var   LeakyBucket = require('leaky-bucket')
    , request     = require('request')
    , bucket;


// create bucket instance, 60 request per minute
bucket = new LeakyBucket(60);


// this will throttle request if required
bucket.throttle(function() {

    // execute request using the request module
    request({
          method: 'get'
        , url: 'http://awesome.api/win'
    }, function(err, response, body) {

    });
});

Rate limit API calls on the server side, allowed are no more than 60 requests per minute

var   LeakyBucket = require('leaky-bucket')
    , request     = require('request')
    , bucket;


// create bucket instance, 60 request per minute, max waiting time = 0
bucket = new LeakyBucket(60, 60, 0);


// this lets pass all request that are within the limit and fail all that
// exceed it
bucket.throttle(function(err) {
    if (err) response.send(429, 'too many requests!');
    else response.send(200, '{id:4, ...}')
});

Keywords

FAQs

Package last updated on 16 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc