8.16.2019
rgene2@gmail.com
test lib WIP
Due
library below exposing a utility function- Compatible to run on Node.js Version ^10.0.0 and Chrome
A cargo
with queue
-like parallel processing and capacity optimization.
//worker function, accepting an array of tasks
async function worker (tasks) {
// Process tasks
}
const due = require('due')
// Create a due of 5 workers,
// each one processing up to 20 tasks
const workers = due(worker, 20, 5)
// Add a task to be processed by the workers
workers.push(task)
Creates a due
object with the specified capacity
, concurrency
and delay
. Tasks added to the due
will be processed altogether (up to the capacity
limit) in parallel batches (up to the concurrency
limit). If all workers are in progress, the task is queued until one becomes available. If the due
hasn’t reached capacity
, the task is queued for delay
milliseconds.
Due passes an array of tasks to one of a group of workers, repeating when the worker is finished.
- A worker processes a maximum of
capacity
tasks at once. - A task is processed at most
delay
milliseconds after being pushed to thedue
- Tasks are processed as soon as
capacity
is reached ordelay
has passed, depending on workers availability.
worker(tasks)
- An asynchronous function for processing an array of queued tasks. Returns a Promise.capacity
- An integer for determining how many tasks should be processed per round.concurrency
- An optional integer for determining how many worker functions should be run in parallel; if omitted, the default is1
.delay
- An optional integer for determining how long should thedue
wait to reachcapacity
; if omitted, the default is0
.
The due
object returned has the following properties and methods:
length
- number of tasks waiting to be processed (read-only).running
-true
if there are tasks being processed,false
otherwise (read-only).idle
-false
if there are tasks waiting or being processed,true
otherwise (read-only).concurrency
- an integer for determining how manyworker
functions should be run in parallel. This property can be changed after adue
is created to alter the concurrency on-the-fly.push(task)
- addstask
to thedue
. Returns a Promise resolving once theworker
has finished processing the task. Instead of a single task, atasks
array can be submitted.empty
- Optional callback that is called when the last task from thedue
is given to aworker
(= all tasks are being processed).drain
- Optional callback that is called when the last task from thedue
has returned from theworker
(= all tasks have been processed).
Async-like
due(worker, capacity)
due(worker, capacity, concurrency)
due(worker, capacity, concurrency, delay)
due(worker, options)
due(options, worker)
Possible options
are
capacity
concurrency
delay
empty
callbackdrain
callback
Compared to Async
Object | Tasks per worker (capacity) | Workers per object (concurrency) |
---|---|---|
queue | 1 | x |
cargo | y |
1 |
due | y |
x |
Equivalent to async.cargo(worker, capacity)
Equivalent to async.queue(worker, concurrency)
Roughly equivalent to using a queue and a cargo together
const queue = async.queue(worker, concurrency)
const cargo = async.cargo(function (tasks, cargoCb) {
queue.push(tasks)
cargoCb() // call immediately
}, capacity)
cargo.push(task, taskCb)
In the async
version, taskCb
will never be called (it would mean passing cargoCb
to queue.push(tasks, cargoCb)
, which therefore waits for the worker to complete before pushing other tasks to the queue, making the queue useless).
Instead of processing tasks on next tick as async.cargo
does, due
waits for delay
milliseconds before processing tasks.
If capacity
is reached before delay
, delay
is ignored and tasks are processed immediately. This is the capacity optimization of due
.
Let's say you own a few container ships
Running a ship is a very expensive operation and you get paid by the container, so you want to put as many containers per ship as possible (= try to fill as close to capacity as possible), and therefore reduce the number of ships running to a minimum.
On the other hand, containers carry perishable goods, so they shouldn't wait more than delay before departure, once they're handed to your shipping company.
If all your ships are already sailing and you receive more containers, they'll wait at port until a ship becomes available to be loaded onto.
- Worker = a ship
- Task = a container
- Concurrency = how many ships you own
- Capacity = how many containers can each ship carry
- Delay = how long can a container wait before leaving port