Skip to content

Express middleware to limit a number of simultaneously processing requests using queue

License

Notifications You must be signed in to change notification settings

pmoleri/express-queue

 
 

Repository files navigation

npm version Build Status Coverage Status Code Climate Inch CI

Dependency Status devDependency Status

Known Vulnerabilities

express-queue

Express middleware to limit a number of simultaneously processing requests using queue

If you have different needs regarding the functionality, please add a feature request.

Installation

npm install --save express-queue

Usage

var express = require('express');
var queue = require('express-queue');
var app = express();

// Using queue middleware
app.use(queue({ activeLimit: 2, queuedLimit: -1 }));
// activeLimit - max request to process simultaneously
// queuedLimit - max requests in queue until reject (-1 means do not reject)
//
// May be also:
// app.get('/api', queue({ activeLimit: 2, queuedLimit: -1})

You can access MiniQueue object used internally. To get current queue length you may do following:

const express = require('express');
const expressQueue = require('../');
const queueMw = expressQueue({ activeLimit: 2, queuedLimit: -1 });

const app = express();
app.use(queueMw);

console.log(`queueLength: ${queueMw.queue.getLength()}`);

For more info on Queue object used refer to npmjs.com/package/mini-queue package docs and/or source code.

Reject handler

If you set queuedLimit, when the queue is full, the middleware will reject any incoming request. The default handler will send a 503 status code, but you can setup your own handler with rejectHandler option, e.g.:

  const queueMw = expressQueue({ activeLimit: 2, queuedLimit: 5, rejectHandler: (req, res) => { res.sendStatus(200); } });

Example

Please, refer to ./examples/ directory for a working example.

Credits

Alexander

Links to package pages:

github.com   npmjs.com   travis-ci.org   coveralls.io   inch-ci.org

License

MIT

About

Express middleware to limit a number of simultaneously processing requests using queue

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%