Common GenServer patterns and utilities to reduce code duplication. Provides standardized handlers for common operations like state retrieval, metrics collection, and configuration management.
Summary
Functions
Ensures a named process is running. Starts it via start_fun if not found.
Standard handler for getting specific state fields.
Standard handler for getting metrics from state.
Standard handler for getting state information.
Standard handler for getting status information.
Standard handler for resetting metrics.
Standard handler for updating configuration.
Utility to increment a metric counter.
Initialize default state with common fields.
Splits a keyword list into GenServer server options and init args.
Utility to update a metric value.
Functions
Ensures a named process is running. Starts it via start_fun if not found.
start_fun is a zero-arity function that starts the process UNLINKED
(GenServer.start/3, Agent.start/2) and registers it under name. It
must return {:ok, pid} or {:error, {:already_started, pid}}.
The process is started unlinked because the first caller is often a
rendering process, a session process or an ExUnit test process. With
start_link, that caller's exit took the singleton, and all of its state,
with it, and a crash in the singleton killed whichever process happened to
call first.
Two callers can both find no process and both run start_fun. The loser
gets {:error, {:already_started, pid}}, which counts as started.
Examples
ensure_started(MyServer, fn ->
GenServer.start(MyServer, [], name: MyServer)
end)
Standard handler for getting specific state fields.
Standard handler for getting metrics from state.
Standard handler for getting state information.
Standard handler for getting status information.
Standard handler for resetting metrics.
Standard handler for updating configuration.
Utility to increment a metric counter.
Initialize default state with common fields.
Splits a keyword list into GenServer server options and init args.
Server options (:name, :timeout, :debug, :spawn_opt) are
separated from the remaining application-specific options.
Examples
iex> split_server_opts(name: MyServer, timeout: 5000, foo: :bar)
{[name: MyServer, timeout: 5000], [foo: :bar]}
iex> split_server_opts(%{name: MyServer})
{[name: MyServer], []}
Utility to update a metric value.