1.0.2 • Published 7 years ago

fast-throttler v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

alt text

NPM Version

Description

Throttle the amount of times your function runs by adding a limit.

Installation

npm install fast-throller --save

Simple Usage

const Throttler = require('fast-throttler'); 
var throttler = new Throttler({rate: 2}); //will throttle 2 requests per second 

For example, you can throttle a GET request per product id in Express.js like this

router.get('/product/:id', function(req, res){
  throttler(req.params.id)
    .then(function(){
       //...
       res.render('template', productData);
    })
    .catch(function(error){
       //...
       res.status(429); //Too many requests
    });
});

Options

ParameterTypeDefault Value
RateNumber1024
PeriodNumber1
CostFunction()=>1
KeyFunction(key)=>key

Events

NameDescription
onAllowedExecutes when throttler is operating within limits
onThrottledExecutes when throttler rate is overreached