View Source partisan_gen_statem behaviour (partisan v5.0.0-beta.21)
This module is an adaptation of Erlang's gen_statem
module.
It replaces all instances of erlang:send/2
and erlang:monitor/2
with their Partisan counterparts.
It maintains the gen_statem
API with the following exceptions:
call/3` - overrides the 3rd to accept not only a timeout value but also a list of options containing any of the following: `{timeout, timeout()}
|{channel, partisan:channel()}
.cast/3` - identical to `cast/2
with the 3th argument is a list of options containing any of the following:{channel, partisan:channel()}
.send_request/4` - identical to `send_request/3
with the 4th argument is a list of options containing any of the following:{channel, partisan:channel()}
.
NOTICE
At the moment this only works for partisan_pluggable_peer_service_manager
backend.
Link to this section Summary
Callbacks
State callback for all states when callback_mode() =:= handle_event_function.
Link to this section Types
-type action() :: postpone | {postpone, Postpone :: postpone()} | {next_event, EventType :: event_type(), EventContent :: term()} | {change_callback_module, NewModule :: module()} | {push_callback_module, NewModule :: module()} | pop_callback_module | enter_action().
-type callback_mode() :: state_functions | handle_event_function.
-type callback_mode_result() :: callback_mode() | [callback_mode() | state_enter()].
-type data() :: term().
-type enter_action() :: hibernate | {hibernate, Hibernate :: hibernate()} | timeout_action() | reply_action().
-type enter_loop_opt() :: {hibernate_after, HibernateAfterTimeout :: timeout()} | {debug, Dbgs :: [partisan_sys:debug_option()]}.
-type event_handler_result(StateType) :: event_handler_result(StateType, term()).
-type event_handler_result(StateType, DataType) :: {next_state, NextState :: StateType, NewData :: DataType} | {next_state, NextState :: StateType, NewData :: DataType, Actions :: [action()] | action()} | state_callback_result(action()).
-type event_timeout() :: Time :: timeout() | integer().
-type event_type() :: external_event_type() | timeout_event_type() | internal.
-type external_event_type() :: {call, From :: from()} | cast | info.
-type from() :: {To :: pid() | partisan_remote_ref:p(), Tag :: term()}.
-type generic_timeout() :: Time :: timeout() | integer().
-type handle_event_result() :: event_handler_result(state()).
-type hibernate() :: boolean().
-type init_result(StateType) :: init_result(StateType, term()).
-type postpone() :: boolean().
-type reply_action() :: {reply, From :: from(), Reply :: term()}.
-type request_id() :: term().
-type server_name() ::
{global, GlobalName :: term()} | {via, RegMod :: module(), Name :: term()} | {local, atom()}.
-type server_ref() :: pid() | (LocalName :: atom()) | {Name :: atom(), Node :: atom()} | {global, GlobalName :: term()} | {via, RegMod :: module(), ViaName :: term()} | partisan_remote_ref:p() | partisan_remote_ref:n().
-type start_mon_ret() :: {ok, {pid(), reference()}} | ignore | {error, term()}.
-type start_opt() :: {timeout, Time :: timeout()} | {spawn_opt, [partisan_proc_lib:start_spawn_option()]} | enter_loop_opt().
-type start_ret() :: {ok, pid()} | ignore | {error, term()}.
-type state() :: state_name() | term().
-type state_callback_result(ActionType) :: state_callback_result(ActionType, term()).
-type state_callback_result(ActionType, DataType) :: {keep_state, NewData :: DataType} | {keep_state, NewData :: DataType, Actions :: [ActionType] | ActionType} | keep_state_and_data | {keep_state_and_data, Actions :: [ActionType] | ActionType} | {repeat_state, NewData :: DataType} | {repeat_state, NewData :: DataType, Actions :: [ActionType] | ActionType} | repeat_state_and_data | {repeat_state_and_data, Actions :: [ActionType] | ActionType} | stop | {stop, Reason :: term()} | {stop, Reason :: term(), NewData :: DataType} | {stop_and_reply, Reason :: term(), Replies :: [reply_action()] | reply_action()} | {stop_and_reply, Reason :: term(), Replies :: [reply_action()] | reply_action(), NewData :: DataType}.
-type state_enter() :: state_enter.
-type state_enter_result(State) :: state_enter_result(State, term()).
-type state_enter_result(State, DataType) :: {next_state, State, NewData :: DataType} | {next_state, State, NewData :: DataType, Actions :: [enter_action()] | enter_action()} | state_callback_result(enter_action()).
-type state_function_result() :: event_handler_result(state_name()).
-type state_name() :: atom().
-type state_timeout() :: Time :: timeout() | integer().
-type timeout_action() :: (Time :: event_timeout()) | {timeout, Time :: event_timeout(), EventContent :: term()} | {timeout, Time :: event_timeout(), EventContent :: term(), Options :: timeout_option() | [timeout_option()]} | {{timeout, Name :: term()}, Time :: generic_timeout(), EventContent :: term()} | {{timeout, Name :: term()}, Time :: generic_timeout(), EventContent :: term(), Options :: timeout_option() | [timeout_option()]} | {state_timeout, Time :: state_timeout(), EventContent :: term()} | {state_timeout, Time :: state_timeout(), EventContent :: term(), Options :: timeout_option() | [timeout_option()]} | timeout_cancel_action() | timeout_update_action().
-type timeout_cancel_action() ::
{timeout, cancel} | {{timeout, Name :: term()}, cancel} | {state_timeout, cancel}.
-type timeout_event_type() :: timeout | {timeout, Name :: term()} | state_timeout.
-type timeout_option() :: {abs, Abs :: boolean()}.
-type timeout_update_action() ::
{timeout, update, EventContent :: term()} |
{{timeout, Name :: term()}, update, EventContent :: term()} |
{state_timeout, update, EventContent :: term()}.
-type transition_option() :: postpone() | hibernate() | event_timeout() | generic_timeout() | state_timeout().
Link to this section Callbacks
-callback callback_mode() -> callback_mode_result().
-callback handle_event(enter, OldState :: state(), State, Data :: data()) -> state_enter_result(State); (event_type(), EventContent :: term(), State :: state(), Data :: data()) -> event_handler_result(state()).
-callback init(Args :: term()) -> init_result(state()).
-callback state_name(enter, OldStateName :: state_name(), Data :: data()) -> state_enter_result(state_name); (event_type(), EventContent :: term(), Data :: data()) -> event_handler_result(state_name()).
Link to this section Functions
-spec call(ServerRef :: server_ref(), Request :: term()) -> Reply :: term().
-spec call(ServerRef :: server_ref(), Request :: term(), Timeout :: timeout() | {clean_timeout, T :: timeout()} | {dirty_timeout, T :: timeout()} | [{timeout, timeout()} | {clean_timeout, T :: timeout()} | {dirty_timeout, T :: timeout()} | {any(), term()}]) -> Reply :: term().
-spec cast(ServerRef :: server_ref(), Msg :: term()) -> ok.
-spec check_response(Msg :: term(), RequestId :: request_id()) -> {reply, Reply :: term()} | no_reply | {error, {term(), server_ref()}}.
-spec enter_loop(Module :: module(), Opts :: [enter_loop_opt()], State :: state(), Data :: data()) -> no_return().
-spec enter_loop(Module :: module(), Opts :: [enter_loop_opt()], State :: state(), Data :: data(), Server_or_Actions :: server_name() | pid() | [action()]) -> no_return().
-spec enter_loop(Module :: module(), Opts :: [enter_loop_opt()], State :: state(), Data :: data(), Server :: server_name() | pid(), Actions :: [action()] | action()) -> no_return().
-spec receive_response(RequestId :: request_id()) -> {reply, Reply :: term()} | {error, {term(), server_ref()}}.
-spec receive_response(RequestId :: request_id(), timeout()) -> {reply, Reply :: term()} | timeout | {error, {term(), server_ref()}}.
-spec reply([reply_action()] | reply_action()) -> ok.
-spec reply(From :: from(), Reply :: term()) -> ok.
-spec send_request(ServerRef :: server_ref(), Request :: term()) -> RequestId :: request_id().
-spec send_request(Name :: server_ref(), Request :: term(), Opts :: list()) -> RequestId :: request_id().
-spec start(ServerName :: server_name(), Module :: module(), Args :: term(), Opts :: [start_opt()]) -> start_ret().
-spec start_link(ServerName :: server_name(), Module :: module(), Args :: term(), Opts :: [start_opt()]) -> start_ret().
-spec start_monitor(Module :: module(), Args :: term(), Opts :: [start_opt()]) -> start_mon_ret().
-spec start_monitor(ServerName :: server_name(), Module :: module(), Args :: term(), Opts :: [start_opt()]) -> start_mon_ret().
-spec stop(ServerRef :: server_ref()) -> ok.
-spec stop(ServerRef :: server_ref(), Reason :: term(), Timeout :: timeout()) -> ok.
-spec wait_response(RequestId :: request_id()) -> {reply, Reply :: term()} | {error, {term(), server_ref()}}.
-spec wait_response(RequestId :: request_id(), timeout()) -> {reply, Reply :: term()} | timeout | {error, {term(), server_ref()}}.