Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMachineNotInitialized is the error thatthe state machine is not initialized. ErrMachineNotInitialized = errors.New("state machine is not initialized") // ErrTransitionNotPermitted is the error that the state transition is not permitted. ErrTransitionNotPermitted = errors.New("state transition is not permitted") // ErrStateUndefined is the error that the state is undefined ErrStateUndefined = errors.New("state is undefined") // ErrStateHandlerNotMatched is the error that the current event is not matched to state handler ErrStateHandlerNotMatched = errors.New("event is not matched to state handler") // ErrNoTransitionApplied is the error that no state transition is applied ErrNoTransitionApplied = errors.New("no state transition is applied") )
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event struct { Err error State State StateTimedOut bool Block *blockchain.Block BlockHash *cp.Hash32B SenderAddr net.Addr ExpireAt *time.Time }
Event is holding request event info across the handler and the rule.
type Handler ¶
type Handler interface { Handle(event *Event) // TimeoutDuration returns the timeout of the state handler. // If it returns a duration, after the duration and the state is still not // If it returns nil, then no timeout task is set. TimeoutDuration() *time.Duration }
Handler handles events for the state The difference between state handler and rule is that
- rule dose not handle state event, it's just a transition from one state to another.
- rule must succeed. even it has side-effects, no matter they fail or not, the transition is deterministic to the destination state.
type Machine ¶
type Machine struct {
// contains filtered or unexported fields
}
Machine is the state machine.
func (*Machine) AddTransition ¶
AddTransition is a function for adding a valid state transition to the machine.
func (*Machine) CurrentState ¶
CurrentState returns the machine's current state. It returns "" when not initialized.
func (*Machine) HandleTransition ¶
HandleTransition tries to move to the next state by the request ctx.
func (*Machine) SetInitialState ¶
SetInitialState sets the initial state which not only handles request for itself but also accepts state types for all direct neighbor states
type NilTimeout ¶
type NilTimeout struct{}
NilTimeout is the base handler struct that has no timeout
func (*NilTimeout) TimeoutDuration ¶
func (h *NilTimeout) TimeoutDuration() *time.Duration
TimeoutDuration returns the duration for timeout
type TransitionRuleMap ¶
TransitionRuleMap is a set of map of transition destination states to rules.
func (TransitionRuleMap) Copy ¶
func (trs TransitionRuleMap) Copy() TransitionRuleMap
Copy clones the TransitionRuleMap.