Shardex.Adapter behaviour (shardex v0.1.0)

Copy Markdown View Source

Behaviour for pool adapters.

init/2 runs once per {shard, role} pool (at boot and on add_shard), never on the routing path. It returns:

  • :state - adapter state passed to run/2
  • :ref - the value handed to callers (Shardex.ref/3)
  • :child_spec - how to start the pool (any Supervisor.child_spec/2 input), or nil when the pool is managed elsewhere
  • :meta - adapter-specific info exposed on %Shardex.Pool{}

run/2 wraps a call in adapter context (e.g. Ecto's put_dynamic_repo). Default: fun.(ref).

Pool process names should be built with Module.concat([instance, shard, role]).

Summary

Types

ctx()

@type ctx() :: %{instance: atom(), shard: atom(), role: atom()}

init_result()

@type init_result() :: %{
  :state => term(),
  :ref => term(),
  optional(:child_spec) =>
    Supervisor.module_spec() | Supervisor.child_spec() | nil,
  optional(:meta) => map()
}

Callbacks

init(opts, ctx)

@callback init(opts :: keyword(), ctx()) :: {:ok, init_result()} | {:error, term()}

run(state, function)

(optional)
@callback run(state :: term(), (term() -> result)) :: result when result: term()