Grapher v0.9.1 Grapher.State View Source

A place for storing any data you may want to be available to all calls from a given process or for subsequent calls from the same process.

For example at ApartmentTherapy we use this to maintain the initial RequestID when calling other services primarily for tracing in our logs.

Stale state entries will be removed from the store once they hit a specific age, which can be configured under :grapher -> :state_lifetime or 30 seconds if unset

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Returns the state for the given PID if it has one, if a Grapher.Context.t/0 struct is found it is returned, otherwise for returns nil

Invoked when the server is started. start_link/3 or start/3 will block until it returns

Updates the state for the calling process, this completely replaces any current state struct. The actual state struct should be fetched and updated directly if there is a need to modify it before saving

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function expiration_query() View Source
expiration_query() :: :ets.match_spec()

Returns the state for the given PID if it has one, if a Grapher.Context.t/0 struct is found it is returned, otherwise for returns nil.

Parameters

  • pid: The pid for which the Context should be fetched.

Examples

iex> State.update(%Context{headers: %{"request-id" => "42"}})
iex> State.for(self())
%Context{headers: %{"request-id" => "42"}}
Link to this function init(atom) View Source
init(:ok) :: {:ok, nil}

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

args is the argument term (second argument) passed to start_link/3.

Returning {:ok, state} will cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning {:ok, state, timeout} is similar to {:ok, state} except handle_info(:timeout, state) will be called after timeout milliseconds if no messages are received within the timeout.

Returning {:ok, state, :hibernate} is similar to {:ok, state} except the process is hibernated before entering the loop. See c:handle_call/3 for more information on hibernation.

Returning {:ok, state, {:continue, continue}} is similar to {:ok, state} except that immediately after entering the loop the c:handle_continue/2 callback will be invoked with the value continue as first argument.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop or calling c:terminate/2. If used when part of a supervision tree the parent supervisor will not fail to start nor immediately try to restart the GenServer. The remainder of the supervision tree will be started and so the GenServer should not be required by other processes. It can be started later with Supervisor.restart_child/2 as the child specification is saved in the parent supervisor. The main use cases for this are:

  • The GenServer is disabled by configuration but might be enabled later.
  • An error occurred and it will be handled by a different mechanism than the Supervisor. Likely this approach involves calling Supervisor.restart_child/2 after a delay to attempt a restart.

Returning {:stop, reason} will cause start_link/3 to return {:error, reason} and the process to exit with reason reason without entering the loop or calling c:terminate/2.

Callback implementation for GenServer.init/1.

Link to this function purge_interval() View Source
purge_interval() :: integer()
Link to this function stale_after(span) View Source
stale_after(String.t() | integer()) :: integer()
Link to this function start_link() View Source
start_link() :: :ignore | {:error, any()} | {:ok, pid()}

Updates the state for the calling process, this completely replaces any current state struct. The actual state struct should be fetched and updated directly if there is a need to modify it before saving.

Parameters

Examples

iex> State.update(%Context{})
:ok