Tyrex.Pool.Strategy behaviour (Tyrex v0.3.0)

Copy Markdown View Source

Behaviour for pool dispatch strategies.

Implement this behaviour to create custom routing logic for Tyrex.Pool.

Example

defmodule MyApp.LeastLoaded do
  @behaviour Tyrex.Pool.Strategy

  def init(pool_name, size), do: {pool_name, size}

  def select({pool_name, size}, _opts) do
    0..(size - 1)
    |> Enum.min_by(fn i ->
      :"#{pool_name}.Runtime.#{i}"
      |> Process.whereis()
      |> Process.info(:message_queue_len)
      |> elem(1)
    end)
  end
end

Summary

Callbacks

Initialize strategy state. Called once at pool startup. Returns opaque state.

Select a runtime index (0..size-1). Receives state and optional dispatch key.

Clean up strategy state on pool shutdown.

Callbacks

init(pool_name, size)

@callback init(pool_name :: atom(), size :: pos_integer()) :: state :: term()

Initialize strategy state. Called once at pool startup. Returns opaque state.

select(state, opts)

@callback select(state :: term(), opts :: keyword()) :: non_neg_integer()

Select a runtime index (0..size-1). Receives state and optional dispatch key.

terminate(state)

(optional)
@callback terminate(state :: term()) :: :ok

Clean up strategy state on pool shutdown.