Tyrex.Pool (Tyrex v0.3.0)

Copy Markdown View Source

A pool of Deno runtimes with pluggable dispatch strategies.

Tyrex.Pool is a Supervisor that starts multiple Tyrex GenServer children and distributes eval calls across them using a configurable strategy.

Usage

# In your supervision tree
children = [
  {Tyrex.Pool, name: :js_pool, size: 4}
]

# Evaluate code on a pool-selected runtime
{:ok, result} = Tyrex.Pool.eval(:js_pool, "1 + 2")

Strategies

See Tyrex.Pool.Strategy for implementing custom strategies.

Lifecycle

The pool registers its metadata in :persistent_term and may own ETS tables via its strategy. Both are cleaned up automatically when the supervisor is stopped — Tyrex.Pool is safe to start and stop dynamically (e.g. one pool per tenant) without leaking persistent_term or ETS resources.

Summary

Functions

Returns a specification to start this module under a supervisor.

Run JavaScript code on a runtime selected by the pool's strategy.

Same as eval/3, but raises Tyrex.Error on error.

Start a pool of Tyrex runtimes.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

eval(pool_name, code, opts \\ [])

@spec eval(atom(), binary(), Keyword.t()) :: {:ok, term()} | {:error, Tyrex.Error.t()}

Run JavaScript code on a runtime selected by the pool's strategy.

Options

  • :key - Dispatch key (used by hash strategy for sticky sessions).
  • :timeout - Timeout for the call.
  • :blocking - Whether to use blocking mode.

eval!(pool_name, code, opts \\ [])

@spec eval!(atom(), binary(), Keyword.t()) :: term() | no_return()

Same as eval/3, but raises Tyrex.Error on error.

start_link(opts)

@spec start_link(Keyword.t()) :: Supervisor.on_start()

Start a pool of Tyrex runtimes.

Options

  • :name - Required. The name of the pool.
  • :size - Number of runtimes. Defaults to System.schedulers_online().
  • :strategy - Dispatch strategy module. Defaults to Tyrex.Pool.Strategy.RoundRobin.
  • :main_module_path - Path to the main JS module for all runtimes.
  • :permissions - Runtime permissions. See Tyrex.start/1 for details.