Giter Club home page Giter Club logo

stopcock's Introduction

stopcock

Version npm Build Status Coverage Status

Limit the execution rate of a function using the token bucket algorithm. Useful for scenarios such as REST APIs consumption where the amount of requests per unit of time should not exceed a given threshold.

Install

npm install --save stopcock

API

The module exports a single function that takes two arguments.

stopcock(fn[, options])

Returns a function which should be called instead of fn.

Arguments

  • fn - The function to rate limit calls to.
  • options - A plain JavaScript object that contains the configuration options.

Options

  • limit - The maximum number of allowed calls per interval. Defaults to 2.
  • interval - The timespan where limit is calculated. Defaults to 1000.
  • bucketSize - The capacity of the bucket. Defaults to 40.
  • queueSize - The maximum size of the internal queue. Defaults to 2^32 - 1 which is the maximum array size in JavaScript.

Return value

A function that returns a promise which resolves to the value returned by the original fn function. The returned function has a size accessor property which returns the internal queue size. When the queue is at capacity the promise is rejected.

Example

const stopcock = require('stopcock');

function request(i) {
  return Promise.resolve(`${i} - ${new Date().toISOString()}`);
}

function log(data) {
  console.log(data);
}

const get = stopcock(request, { bucketSize: 5 });

for (let i = 0; i < 10; i++) {
  get(i).then(log);
}

/*
0 - 2017-03-30T16:46:39.938Z
1 - 2017-03-30T16:46:39.940Z
2 - 2017-03-30T16:46:39.940Z
3 - 2017-03-30T16:46:39.940Z
4 - 2017-03-30T16:46:39.940Z
5 - 2017-03-30T16:46:40.443Z
6 - 2017-03-30T16:46:40.943Z
7 - 2017-03-30T16:46:41.441Z
8 - 2017-03-30T16:46:41.942Z
9 - 2017-03-30T16:46:42.439Z
*/

License

MIT

stopcock's People

Contributors

lpinca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

serhiy91

stopcock's Issues

Is there any way to access the queue length?

The queue length is not exposed, is there any way to access the length of the queue?

A potential use case,
do not add a batch job list if queue is already beyond a certain threshold

Another use case,
After each job is added or done a console.log is made which includes the current length of the queue to get an idea of how long is left untill the processing is complete.

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.