View Source Finitomata.Pool (Finitomata v0.22.0)
The instance of FSM backed up by Finitomata
.
FSM representation
graph TD
idle --> |init| ready
idle --> |do| ready
ready --> |do| ready
ready --> |stop| done
Fully asynchronous pool to manage many similar processes, like connections.
The pool is to be started using start_pool/1
directly or with pool_spec/1
in the
supervision tree.
initialize/2
is explicitly separated because usually this is to be done after some
external service initialization. In a case of AMQP
connection management, one
would probably start the connection process and then a pool to manage channels.
Once initialize/2
has been called, the run/3
function might be invoked to
asynchronously execute the function passed as actor
to start_pool/1
.
If the callbacks on_result/2
and/or on_error/2
are defined, they will be invoked
respectively. Finally, the message to the calling process will be sent, unless
the third argument in a call to run/3
is nil
.
Summary
Types
The actor function in the pool
The handler function in the pool
The ID of the Pool
The simple actor function in the pool
The simple handler of result/error in the pool
The actor function in the pool, receiving the state as a second argument
The actor function in the pool, receiving the state as a second argument
Kind of event which might be send to initiate the transition.
Functions
Getter for the internal compiled-in FSM information.
Returns the generator to be used in StreamData
-powered property testing, based
on the specification given to use Estructura.Nested
, which contained
Casts the map representation as given to Estructura.Nested.shape/1
to
the nested Estructura
instance.
Returns a specification to start this module under a supervisor.
The convenient macro to allow using states in guards, returns a compile-time
list of states for Finitomata.Pool
.
Gets the value for the given key from the structure
Initializes the started _FSM_s with the same payload, or with what payload_fun/1
would return as many times as the number of workers.
Safely parses the json, applying all the specified validations and coercions
Same as parse/1
but either returns the result of successful parsing or raises
Child spec for Finitomata.Pool
to embed the process into a supervision tree
Puts the value for the given key into the structure, passing coercion and validation,
returns {:ok, updated_struct}
or {:error, reason}
if there is no such key
Puts the value for the given key into the structure, passing coercion and validation, returns the value or raises if there is no such key
The runner for the actor
function with the specified payload.
Starts an FSM alone with name
and payload
given.
Starts a pool of asynchronous workers wrapped by an FSM.
Types
@type actor() :: naive_actor() | responsive_actor()
The actor function in the pool
@type handler() :: naive_handler() | responsive_handler()
The handler function in the pool
@type id() :: Finitomata.id()
The ID of the Pool
The simple actor function in the pool
The simple handler of result/error in the pool
@type responsive_actor() :: (term(), Finitomata.State.payload() -> {:ok, term()} | {:error, any()})
The actor function in the pool, receiving the state as a second argument
The actor function in the pool, receiving the state as a second argument
@type state() :: :done | :ready | :idle | :*
Kind of event which might be send to initiate the transition.
FSM representation
graph TD
idle --> |init| ready
idle --> |do| ready
ready --> |do| ready
ready --> |stop| done
Functions
Getter for the internal compiled-in FSM information.
@spec __generator__(%Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() }) :: StreamData.t(%Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() })
Returns the generator to be used in StreamData
-powered property testing, based
on the specification given to use Estructura.Nested
, which contained
shape
%{
id: {StreamData, :atom, [:alias]},
errors: [:term],
payload: :term,
actor: {StreamData, :constant, [&Function.identity/1]},
on_error: {Finitomata.Pool.Actor, :handler, [:error]},
on_result: {Finitomata.Pool.Actor, :handler, [[]]}
}
The argument given would be used as a template to generate new values.
Casts the map representation as given to Estructura.Nested.shape/1
to
the nested Estructura
instance.
If split: true
is passed as an option, it will attempt to put foo_bar
into nested %{foo: %{bar: _}}
Returns a specification to start this module under a supervisor.
See Supervisor
.
The convenient macro to allow using states in guards, returns a compile-time
list of states for Finitomata.Pool
.
@spec get( %Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() }, Estructura.Config.key(), any() ) :: any()
Gets the value for the given key from the structure
@spec initialize(Finitomata.id(), (pos_integer() -> any()) | any()) :: :ok
Initializes the started _FSM_s with the same payload, or with what payload_fun/1
would return as many times as the number of workers.
@spec parse(binary()) :: {:ok, struct()} | {:error, Exception.t()}
Safely parses the json, applying all the specified validations and coercions
Same as parse/1
but either returns the result of successful parsing or raises
@spec pool_spec(keyword()) :: Supervisor.child_spec()
Child spec for Finitomata.Pool
to embed the process into a supervision tree
@spec put( %Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() }, Estructura.Config.key(), any() ) :: {:ok, %Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() }} | {:error, any()}
Puts the value for the given key into the structure, passing coercion and validation,
returns {:ok, updated_struct}
or {:error, reason}
if there is no such key
@spec put!( %Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() }, Estructura.Config.key(), any() ) :: %Finitomata.Pool{ actor: term(), errors: term(), id: term(), on_error: term(), on_result: term(), payload: term() } | no_return()
Puts the value for the given key into the structure, passing coercion and validation, returns the value or raises if there is no such key
@spec run(Finitomata.id(), Finitomata.event_payload(), pid()) :: :ok
The runner for the actor
function with the specified payload.
Basically, upon calling run/3
, the following chain of calls would have happened:
actor.(payload, state)
(oractor.(payload)
if the function of arity one had been given)on_result(result, id)
/on_error(result, id)
if callbacks are specified- the message of the shape
{:transition, :success/:failure, self(), {payload, result, on_result/on_error}})
will be sent topid
unlessnil
given as a third argument
Starts an FSM alone with name
and payload
given.
Usually one does not want to call this directly, the most common way would be
to start a Finitomata
supervision tree or even better embed it into
the existing supervision tree and start FSM with Finitomata.start_fsm/4
passing Finitomata.Pool
as the first parameter.
For distributed applications, use Infinitomata.start_fsm/4
instead.
@spec start_pool( id: Finitomata.id(), payload: :term, count: pos_integer(), actor: actor(), on_error: handler(), on_result: handler() ) :: GenServer.on_start()
Starts a pool of asynchronous workers wrapped by an FSM.
@spec start_pool( id :: Finitomata.id(), count :: pos_integer(), [actor: actor(), on_error: handler(), on_result: handler(), payload: :term] | %{ :actor => actor(), optional(:on_error) => handler(), optional(:on_result) => handler(), optional(:payload) => :term } | [implementation: module(), payload: :term] | %{:implementation => module(), optional(:payload) => :term} ) :: GenServer.on_start()