fsm

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TransitionTo

func TransitionTo[S FiniteState](inputs ...func(context.Context, S)) *transition[S]

TransitionTo returns a transition to the next state, with optional inputs feeding into next state.

func WithBackOff

func WithBackOff(t Transition, backOff backoff.BackOff) *transitionWithBackOff

WithBackOff delays the transition.

Types

type EndState

type EndState struct{}

func (*EndState) Do

func (*EndState) IsTerminal

func (e *EndState) IsTerminal() bool

type FiniteState

type FiniteState interface {
	IsTerminal() bool              // IsTerminal returns true if the state is a terminal (end) state
	Do(context.Context) Transition // Do returns the next transition
}

FiniteState is a state in Finite-State-Machine

type State

type State struct{}

func (*State) IsTerminal

func (s *State) IsTerminal() bool

type StateMachine

type StateMachine struct {
	Maps map[reflect.Type]FiniteState
	clock.Clock
}

StateMachine is a Finite-State-Machine (FSM) that starts from a given state, Do() the state, and then transition to the next state until terminal state is reached.

Please use one struct type to represent one FiniteState.

func MustNewStateMachine

func MustNewStateMachine(states ...FiniteState) *StateMachine

func NewStateMachine

func NewStateMachine(states ...FiniteState) (*StateMachine, error)

func (*StateMachine) Start

func (sm *StateMachine) Start(ctx context.Context, state FiniteState) (FiniteState, error)

type Transition

type Transition interface {
	Next() reflect.Type
	FeedInputTo(context.Context, any)
}

Transition decides the next state to hop

type TransitionWithBackOff

type TransitionWithBackOff interface {
	Transition
	GetBackOff() backoff.BackOff
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL