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.8
  • 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 accpets two parameter, both are optional

var instance = new LeakyBucket([ItemsPerInterval = 60][, Interval = 60]);

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);

Throttling

The throttle accepts two parameters, of which the first is optional

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() {
    // do something
});

Throttle an item with the cost of 10

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

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, allowed are no more than 60 requests per second

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) {

    });
});

Keywords

FAQs

Package last updated on 15 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