0.1.1 • Published 7 years ago
multi-throttle v0.1.1
Express throttling middleware
This middleware throttles the number of concurrent requests and the numbers of requests within in a given interval.
Requests that exceed the limit of requests / interval are added to a queue and will be executed once current requests
finish execution.
If the number of queued requests exceeds the value for maxQueueSize, the requests will be dropped until an item is
removed from the queue.
Installation
npm install -S multi-throttleUsage
Add the middleware to your express app and adjust the configuration options.
import express from 'express';
import multiThrottle from 'multi-throttle';
const app = express();
app.use(multiThrottle({
/**
* The number of maximum allowed requests in the given interval
*/
maxRequests: 10,
/**
* The interval within which the maxRequest limit is enforced in ms
*/
interval: 1000,
/**
* Maximum number of concurrent running requests
*/
maxConcurrency: 5,
/**
* Maximum number of queued requests before incoming requests are dropped
*/
maxQueueSize: 10,
/**
* Optional:
* Function to send custom response object / status code when a request is dropped.
*/
handleError?: (req: express.Request, res: express.Response) => void
}));