Documentation ¶
Overview ¶
Copyright (c) 2021-2023 - for information on the respective copyright owner see the NOTICE file and/or the repository https://github.com/carbynestack/ephemeral.
SPDX-License-Identifier: Apache-2.0
Index ¶
Constants ¶
const ( // CallbackAfterEnter is a callback type which is triggered when a new state just entered. CallbackAfterEnter = "AfterEnter" // CallbackBeforeEnter is a callback type which is triggered when a new state just entered. CallbackBeforeEnter = "BeforeEnter" // CallbackWhenStateTimeout is a type of callback which is triggered when state timeout is reached. CallbackWhenStateTimeout = "WhenStateTimeout" )
const (
Stopped = "_Stopped"
)
Variables ¶
This section is empty.
Functions ¶
func InitCallbacksAndTransitions ¶
func InitCallbacksAndTransitions(cbs []*Callback, trs []*Transition) (map[string][]*Callback, map[TransitionID]*Transition)
InitCallbacksAndTransitions converts slices to maps.
Types ¶
type Action ¶
type Action func(interface{}) error
Action is a user defined function executed in the callback.
type Callback ¶
Callback is a function which is executed as a response to event during state transition.
func AfterEnter ¶
AfterEnter defines state this callback is bound to.
func BeforeEnter ¶
BeforeEnter defines callback which is executed before entering the state.
func WhenStateTimeout ¶
func WhenStateTimeout() *Callback
WhenStateTimeout defines a callback which is called when state timeout is reached.
type FSM ¶
type FSM struct {
// contains filtered or unexported fields
}
FSM is a finate state machine. Before and after callbacks for the same source state can be defined. If several callbacks are provided for each type, all of them are executed in order.
func NewFSM ¶
func NewFSM(ctx context.Context, initState string, trn map[TransitionID]*Transition, cb map[string][]*Callback, stateTimeout time.Duration, logger *zap.SugaredLogger) (*FSM, error)
NewFSM returns a new finate state machine.
func (*FSM) Run ¶
Run consumes events from the queue until an error occurs or the FSM has been stopped. The error is caused either by an unregistered event or by the callback itself. If the FSM was stopped its state is updated, the timer is stopped and the error channel is closed. The method is blocking and must be started exactly once.
`errChan` is expected to be a buffered channel with minimum capacity of "1".
type History ¶
type History struct {
// contains filtered or unexported fields
}
History contains all received events and passed states including the current one.
type Transition ¶
type Transition struct { ID TransitionID Event, Src, Dst string Timeout time.Duration }
Transition defines a transition between FSM states.
func WhenIn ¶
func WhenIn(state string) *Transition
WhenIn specifies the source state of the transition.
func WhenInAnyState ¶
func WhenInAnyState() *Transition
WhenInAnyState targets transition from all states.
func (*Transition) GoTo ¶
func (i *Transition) GoTo(dst string) *Transition
GoTo specifies the destination State.
func (*Transition) GotEvent ¶
func (i *Transition) GotEvent(event string) *Transition
GotEvent specifies the triggering event for the transition.
func (*Transition) Stay ¶
func (i *Transition) Stay() *Transition
Stay forces the transition to stay in the source state.
func (*Transition) WithTimeout ¶
func (i *Transition) WithTimeout(d time.Duration) *Transition
WithTimeout defines an individual timeout within the transition to the next state is expected.
type TransitionID ¶
type TransitionID struct {
Event, Source string
}
TransitionID is a tuple containing external Event and source State.