constellation/worker_pool

Resilient OTP worker pools backed by Constellation demand. Workers renew demand only after the handler returns. Delivered batches are at-most-once: restart never replays them. Constructors live in worker_pool/types.

Types

pub opaque type Config(event, state)
pub type Event =
  types.Event
pub opaque type Pool(event, state)
pub type PoolError =
  types.PoolError
pub type Snapshot =
  types.Snapshot
pub type StartError =
  types.StartError
pub type WorkerId =
  types.WorkerId

Values

pub fn each(
  size size: Int,
  prefetch prefetch: Int,
  initial_state initial_state: fn(Int) -> state,
  handle_event handle_event: fn(state, event) -> state,
) -> Config(event, state)

Processes each event in a delivered batch, in order.

pub fn new(
  size size: Int,
  prefetch prefetch: Int,
  initial_state initial_state: fn(Int) -> state,
  handle_batch handle_batch: fn(state, List(event)) -> state,
) -> Config(event, state)

Builds a batch-processing worker pool configuration.

pub fn push(
  pool: Pool(event, state),
  events: List(event),
) -> Result(Nil, types.PoolError)

Pushes one atomic batch. A timeout does not imply the push was rejected.

pub fn snapshot(
  pool: Pool(event, state),
) -> Result(types.Snapshot, types.PoolError)
pub fn start(
  config: Config(event, state),
) -> Result(Pool(event, state), types.StartError)

Starts an unlinked push-driven pool.

pub fn start_link(
  config: Config(event, state),
) -> Result(Pool(event, state), types.StartError)

Starts a pool linked to its calling owner.

pub fn start_with_source(
  config: Config(event, state),
  notify_source: fn(source.Event) -> Nil,
) -> Result(
  #(Pool(event, state), source.Source(event)),
  types.StartError,
)

Source callbacks are asynchronous but fail-fast: a callback crash stops the pool rather than silently losing capacity notifications.

pub fn start_with_source_link(
  config: Config(event, state),
  notify_source: fn(source.Event) -> Nil,
) -> Result(
  #(Pool(event, state), source.Source(event)),
  types.StartError,
)

Source-backed pool linked to an owner that coordinates the source lifetime.

pub fn stop(
  pool: Pool(event, state),
) -> Result(Nil, types.PoolError)

Drains accepted work. Timeout does not cancel a running handler.

pub fn supervised(
  config: Config(event, state),
) -> Result(
  supervision.ChildSpecification(Pool(event, state)),
  types.ConfigError,
)

Returns a child specification whose handle resolves the restarted pool. In-flight work is not replayed; calls during restart may be unavailable.

pub fn with_buffer_capacity(
  config: Config(event, state),
  capacity: Int,
) -> Config(event, state)

Bounds undelivered events, excluding work already delivered to workers.

pub fn with_reporter(
  config: Config(event, state),
  reporter: fn(types.Event) -> Nil,
) -> Config(event, state)

Asynchronous best-effort telemetry; a callback panic drops only that event. Keep callbacks inexpensive: the notifier mailbox is not bounded.

pub fn with_timeout(
  config: Config(event, state),
  milliseconds: Int,
) -> Config(event, state)

Sets synchronous call and supervisor shutdown timeouts.

pub fn worker_id_to_int(id: types.WorkerId) -> Int
Search Document