maxkueng / node-trickle

A time-based rate-limiting module for node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trickle

If you're looking for a promise-based alternative to this module check out wyt.

Trickle limits function executions based on time. It lets you define the number of executions per a certain amount of milliseconds.

This is particularly useful to obey an API provider's rate limit, for example.

Installation

Note: trickle is called "timetrickle" on npm because someone has occupied the name before I had a chance.

npm install timetrickle --save

or manually add it to your package.json

Example

var trickle = require('timetrickle');
    start = +Date.now();

// Helper function to show the time difference
function since (time) { return Math.round((time - start) / 1000) + 's'; }

// Limit once per second
var limit = trickle(1, 1000);

limit(function () {
    console.log('I am doing an API call here', since(+Date.now()));
});

limit(function () {
    console.log('I am doing another API call here', since(+Date.now()));
});

limit(function () {
    console.log('I am doing an API call here again', since(+Date.now()));
});

Will output:

I am doing an API call here 0s
I am doing an API call here again 1s
I am doing another API call here 2s

About

A time-based rate-limiting module for node.js

License:MIT License


Languages

Language:JavaScript 100.0%