A durable, distributed gen_server backed by a pluggable storage backend.
A dgen_server is an abstract entity composed of durable state and operations on
that state. The state lives in the configured backend (default: FoundationDB)
and the operations are defined by a callback module implementing the
dgen_server behaviour. This allows a gen_server to outlive any single Erlang
process, node, or cluster.
Zero or more Erlang processes may act on a dgen_server at any time. Processes
with consume enabled consume messages from the durable queue and invoke
callbacks; processes without it only publish messages.
Options
The following options may be passed via the Opts proplist:
tenant(required) -{DbHandle, Dir}pair identifying the backend subspace.consume(defaulttrue) - whether this process consumes messages from the queue.reset(defaultfalse) - whentrue, re-initialise the durable state even if it already exists.
Callbacks
init/1- return{ok, State}or{ok, Tuid, State}.handle_call/3- return{reply, Reply, State}or{reply, Reply, State, Actions}.handle_cast/2- return{noreply, State}or{noreply, State, Actions}.handle_info/2- return{noreply, State}or{noreply, State, Actions}.
Actions is a list of 1-arity funs executed after the transaction commits. The
argument is the
Summary
Functions
Sends a synchronous call request via the durable queue. Default timeout 5000ms.
Sends a synchronous call request via the durable queue.
Sends an asynchronous cast request to the dgen_server's durable queue.
Sends a batch of cast requests to the dgen_server's durable queue atomically.
Kills the dgen_server, deleting all durable state, queue items, and waiting
call keys. The process exits with Reason.
Sends a call that bypasses the durable queue and is handled immediately.
Like priority_call/2 but with an explicit timeout.
Sends a cast that bypasses the durable queue and is handled immediately.
Starts a dgen_server process without linking.
Starts a dgen_server process without linking, registered as Reg.
Starts a dgen_server process linked to the calling process.
Starts a dgen_server process linked to the calling process, registered as Reg.
Types
-type action() :: fun().
-type event_type() :: {call, from()} | cast | info.
-type from() :: term().
-type internalstate() :: #state{tenant :: dgen_backend:tenant(), mod :: atom(), tuid :: tuple(), watch :: undefined | dgen_backend:future(), cache :: boolean(), mod_state_cache :: undefined | {dgen_backend:versionstamp() | dgen_backend:future(), {ok, term()} | {error, not_found}}, cache_misses :: non_neg_integer()}.
-type lock_ret() :: {lock, state()}.
-type option() :: {tenant, dgen_backend:tenant()} | {consume, boolean()} | {reset, boolean()} | {cache, boolean()} | gen_server:start_opt().
-type options() :: [option()].
-type server() :: gen_server:server_ref().
-type start_ret() :: gen_server:start_ret().
-type state() :: term().
-type tuid() :: tuple().
Callbacks
-callback handle_cast(Msg :: term(), State :: state()) -> noreply_ret() | lock_ret() | stop_ret().
-callback handle_info(Info :: term(), State :: state()) -> noreply_ret() | stop_ret().
-callback handle_locked(EventType :: event_type(), Msg :: term(), State :: state()) -> reply_ret() | noreply_ret() | stop_ret().
Functions
Sends a synchronous call request via the durable queue. Default timeout 5000ms.
Sends a synchronous call request via the durable queue.
The request is enqueued durably and the caller blocks until a consumer
processes it and writes the reply, or until Timeout milliseconds elapse.
Options
timeout: Default5000. Timeout in milliseconds, orinfinity.
Sends an asynchronous cast request to the dgen_server's durable queue.
Sends a batch of cast requests to the dgen_server's durable queue atomically.
-spec code_change(term(), internalstate(), term()) -> {ok, internalstate()}.
-spec handle_call(term(), gen_server:from(), internalstate()) -> {reply, term(), internalstate()}.
-spec handle_cast(term(), internalstate()) -> {noreply, internalstate()} | {stop, term(), internalstate()}.
-spec handle_info(term(), internalstate()) -> {noreply, internalstate()} | {stop, term(), internalstate()}.
-spec init(term()) -> {ok, internalstate()} | {error, term()}.
Kills the dgen_server, deleting all durable state, queue items, and waiting
call keys. The process exits with Reason.
Sends a call that bypasses the durable queue and is handled immediately.
Use with caution: this breaks ordering guarantees with respect to queued messages and ignores locks. Can be useful for snapshot reads.
Like priority_call/2 but with an explicit timeout.
Sends a cast that bypasses the durable queue and is handled immediately.
Use with caution: this breaks ordering guarantees with respect to queued messages and ignores locks.
Starts a dgen_server process without linking.
See start_link/3 for details on Mod, Arg, and Opts.
-spec start(gen_server:server_name(), module(), term(), options()) -> start_ret().
Starts a dgen_server process without linking, registered as Reg.
See start_link/3 for details on Mod, Arg, and Opts.
Starts a dgen_server process linked to the calling process.
Modis the callback module implementing thedgen_serverbehaviour.Argis passed toMod:init/1.Optsis a proplist that must include{tenant, {Db, Dir}}and may includeconsumeandreset.
-spec start_link(gen_server:server_name(), module(), term(), options()) -> start_ret().
Starts a dgen_server process linked to the calling process, registered as Reg.
See start_link/3 for details on Mod, Arg, and Opts.
-spec terminate(term(), internalstate()) -> ok.