micron-throttle

Token bucket based HTTP request throttle for Node.js

npm install micron-throttle

The throttle module can be used in just about any application that supports the function (req, res, next) middleware convention (such as express, connect, union or restify). For example in restify:

var http        = require('restify'),
    throttle    = require('micron-throttle');
 
// Create the server and pass-in the throttle middleware 
var server      = restify.createServer();
server.use(throttle({
    burst: 100,
    rate: 50,
    ip: true,
    overrides: {
        '192.168.1.1': {
            rate: 0,        // unlimited 
            burst: 0
        }
    }
}));
 
// Define a route 
server.get('/hello/:name', function (reqresnext) {
    res.send('hello ' + req.params.name);
});
 
// Listen 
server.listen(3333);
Name Default Type Description
rate10NumberSteady state number of requests/second to allow
burst25NumberIf available, the amount of requests to burst to
iptrueBooleanDo throttling on a /32 (source IP)
xfffalseBooleanDo throttling on a /32 (X-Forwarded-For)
usernamefalseBooleanDo throttling on req.username
overridesnullObjectPer "key" overrides
tokensTablen/aObjectStorage engine; must support set/get
maxKeys10000NumberIf using the built-in storage table, the maximum distinct throttling keys to allow at a time
npm test

This module is adapted from restify's throttle plug-in – originally developed by Mark Cavage.