lifeguard
Types
An error returned when failing to use a pooled worker.
pub type ApplyError {
NoResourcesAvailable
CheckOutTimeout
WorkerCallTimeout
WorkerCrashed(dynamic.Dynamic)
}
Constructors
-
NoResourcesAvailable
-
CheckOutTimeout
-
WorkerCallTimeout
-
WorkerCrashed(dynamic.Dynamic)
The strategy used to check out a resource from the pool.
pub type CheckoutStrategy {
FIFO
LIFO
}
Constructors
-
FIFO
-
LIFO
The interface for interacting with a pool of workers in Lifeguard.
pub opaque type Pool(msg)
Configuration for a Pool
.
pub opaque type PoolConfig(state, msg)
An actor spec for workers in a pool. Similar to actor.Spec
,
but this provides the initial selector for the actor. It will select on its own
subject by default, using function.identity
to pass the message straight through.
For clarity, it will be used as follows by Lifeguard:
actor.Spec(init_timeout: spec.init_timeout, loop: spec.loop, init: fn() {
// Check in the worker
let self = process.new_subject()
process.send(pool_subject, Register(self)) // Register the worker with the pool
let selector =
process.new_selector()
|> process.selecting(self, function.identity)
spec.init(selector)
})
pub type Spec(state, msg) {
Spec(
init: fn(process.Selector(msg)) ->
actor.InitResult(state, msg),
init_timeout: Int,
loop: fn(msg, state) -> actor.Next(msg, state),
)
}
Constructors
-
Spec( init: fn(process.Selector(msg)) -> actor.InitResult(state, msg), init_timeout: Int, loop: fn(msg, state) -> actor.Next(msg, state), )
An error returned when creating a Pool
.
pub type StartError {
PoolActorStartError(actor.StartError)
WorkerStartError(actor.StartError)
PoolSupervisorStartError(dynamic.Dynamic)
WorkerSupervisorStartError(dynamic.Dynamic)
}
Constructors
-
PoolActorStartError(actor.StartError)
-
WorkerStartError(actor.StartError)
-
PoolSupervisorStartError(dynamic.Dynamic)
-
WorkerSupervisorStartError(dynamic.Dynamic)
Functions
pub fn call(
pool pool: Pool(a),
msg msg: fn(Subject(b)) -> a,
checkout_timeout checkout_timeout: Int,
call_timeout call_timeout: Int,
) -> Result(b, ApplyError)
Send a message to a pooled actor and wait for a response. Equivalent to process.call
using a pooled actor.
pub fn new(spec spec: Spec(a, b)) -> PoolConfig(a, b)
Create a new PoolConfig
for creating a pool of actors.
import lifeguard
pub fn main() {
// Create a pool of 10 actors that do nothing.
let assert Ok(pool) =
lifeguard.new(
lifeguard.Spec(
init_timeout: 1000,
init: fn(selector) { actor.Ready(state: Nil, selector:) },
loop: fn(msg, state) { actor.continue(state) },
)
)
|> lifeguard.with_size(10)
|> lifeguard.start(1000)
}
Default values
Config | Default |
---|---|
size | 10 |
checkout_strategy | FIFO |
pub fn send(
pool pool: Pool(a),
msg msg: a,
checkout_timeout checkout_timeout: Int,
) -> Result(Nil, ApplyError)
Send a message to a pooled actor. Equivalent to process.send
using a pooled actor.
pub fn start(
config pool_config: PoolConfig(a, b),
timeout init_timeout: Int,
) -> Result(Pool(b), StartError)
Start a pool supervision tree using the given PoolConfig
and return a
Pool
.
pub fn supervisor(pool pool: Pool(a)) -> Pid
Get the supervisor PID for a running pool.
pub fn with_checkout_strategy(
config pool_config: PoolConfig(a, b),
strategy checkout_strategy: CheckoutStrategy,
) -> PoolConfig(a, b)
Set the order in which actors are checked out from the pool. Defaults to FIFO
.