@balena/promise-queue
v1.3.2
Published
Queue sync or async functions with max concurrency, max size, max age, and fifo/lifo ordering
Downloads
5,066
Readme
promise-queue
This module allows you to queue sync/async functions with max concurrency, max size, max-age, and fifo/lifo ordering
Constructor
let q = new PromiseQueue({
name: 'device_logs',
concurrency: 40,
maxSize: 400,
maxAge: 30 * 1000,
order: 'fifo'
});
Use
export function middleware(
req: Request,
res: Response,
next: NextFunction,
): void {
q.add(() =>
new Promise(resolve => {
res.once('close', resolve);
res.once('finish', resolve);
next();
}),
)
.catch(err => {
if (err instanceof PromiseQueueError) {
reject(res);
}
});
}
Metrics
PromiseQueue.metrics
is an EventEmitter
which emits the following events:
arrival
, data:undefined
: a promise arrives at the queuequeueLength
, data:number
: the length of the queue on arrivalinFlight
, data:number
: the number of active promises on arrivaldequeue
, data:undefined
: a promise stops waiting in the queue and starts processingqueueTime
, data:number
: time a promise spent waiting in queue on dequeuecompletion
, data:undefined
: a promise completes processingserviceTime
, data:number
: time a promise spent processing on completionlatency
, data:number
: sum of queueTime and serviceTime on completionrejection
, data:undefined
: a promise to be added is rejected becausemaxSize
is already reachedtimeout
, data:undefined
: a promise's latency (queue time + service time) exceedsmaxAge
, and it is rejected