The parseq
module provides tools for working with callbacks and eventual programming. The name combines the prefixes of parallel and sequence.
The parseq
module requires that the actor has the @.delay
capability.
use parseq_make def parseq = parseq_make(@.delay)
@.delay
is only necessary if time limits are used.
The parseq
module works with functions of these types: callbacks, requestors, cancels, and factories.
Callbacks are a functional pattern used to deliver results from the future. A callback function has a signature of
ƒ callback(value, reason)
Instead of returning a value, a function may be written to use a provided callback function, passing the value as an input value.
return callback(value)
If the value is null
, that indicates failure. The optional input value reason can be used to provide documentation about the failure.
return callback(null, "My dog ate my return value.")
The requestor
function makes and returns a requestor function. A requestor function has a signature of
ƒ requestor(callback, value)
The requestor will eventually call the callback function, giving its value.
A callback frunction has a signature of
ƒ callback(value, reason)
If the requestor was successful, it will pass its result as the callback's value. If the requestor is not successful, it will pass null
as the callback's value, and optionally a reason for the failure.
The requestor
function takes a record input value that specifies the record to be made. The record contains these fields:
type
(required)
The types are "fallback"
, "parallel"
, "race"
, "sequence"
.
requestors
(required except when type is "parallel"
)An array of requestor functions.
The parallel takes a set of requestor functions and returns a requestor function that starts them all at once. It ultimately produces an array of all of the results.
The requireds and optionals are an array or record of requestor functions. Either can be null
, but not both. Otherwise, requireds and optionals must be the same type. The required requestors must all succeed if the parallel requestor is to succeed. An optional requestor can be unsuccessful without affecting the success of the whole.
A time_limit (in seconds) can be imposed.
A throttle may be used to limit the number of requestors that are running concurrently. This can be used to reduce stress on fragile systems.