async-task-pool 
A concurrency throttle for async tasks.
Install
$ npm install --save async-task-pool
Usage
const asyncTaskPool = require('async-task-pool');
function createAsyncTaskHandler() {
const cp1 = childProcess.fork('./some/path');
return function asyncTaskHandler(eventEmitter, done, ...additionalArgs) {
eventEmitter.emit('starting');
cp.send({'messageData': additionalArgs});
cp.on('message', function (data) {
if (data.result) {
done();
}
});
}
}
const enqueue = asyncTaskPool(createAsyncTaskHandler, {limit: 1});
const eventEmitter1 = enqueue('arg1', 'arg2');
eventEmitter1.on('foo', ...);
const eventEmitter2 = enqueue('arg2', 'arg3');
eventEmitter2.on('foo', ...);
API
asyncTaskPool(createHandler, [options])
Returns a enqueue
function which is used to schedule async events for execution by the handlers.
createHandler
Type: callback()
Called with no arguments. It is called once every time a new task handler is required. It will be called at most options.limit
times. After the limit is reached, no new handlers will be created. Instead, handlers will be reused as they free themselves by calling the done
callback.
The return value should be a handlerCallback
, with the following signature:
handlerCallback(eventEmitter, done, ...additionalArgs)
eventEmitter
A new EventEmitter
is created when a task is scheduled using the enqueue
function. It is returned synchronously from the enqueu
function, but will not emit any events until passed to a handler. When a handler becomes available, the stored eventEmitter
is passed as the first argument to the handler.
This eventEmitter
is used as the main communication channel between the consumer of the throttled async task, and the task handler. As such, the consumer should not need to care which handler is servicing the task request, nor should it care if a handler was available immediately or after some delay.
done
Since handlers are just functions which may be called multiple times, they must notify when tasks are complete and they are ready to be recalled by invoking the done
callback. The done
callback is passed as the second argument to the handler. It is up to the handler to ensure it is always called.
...additionalArgs
Any arguments passed to enqueue
will be spread as additional arguments to the handler (after eventEmitter
and done
).
options
limit
Type: number
Default: 1
The maximum number of handlers that will ever be created. Once the limit is reached, further requests will be queued until handlers free themselves by calling the done
callback.
License
MIT © James Talmage