meta

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func(args any)

Action defines the action to execute when a state is reached.

type Loop

type Loop interface {
	// Name returns name of the loop.
	Name() string

	// Conf configure the loop with the given configuration.
	// This methods should be called before Run and not be called
	// after the loop is running.
	Conf(conf LoopConfig) bool

	// Run starts the loop with the provided hooks.
	//
	// NOTE: Run is not re-entrant and must not be called within a callback.
	// When the loop is configured to run in async mode,
	// hook functions must be guaranteed to be goroutine-safe.
	Run(hooks LoopRunHook)

	// Alive returns true if loop is running.
	Alive() bool

	// Stopped returns true if loop is stopped.
	Stopped() bool

	// Stop acts the same as Quit except that
	// it waits until the loop quits.
	Stop()

	// Quit stops the internal timer,
	// closes channels, clears all states.
	Quit()
}

Loop interface exposed

func NewLoop

func NewLoop(name string, conf LoopConfig) Loop

NewLoop creates a new loop object with the given name and conf.

type LoopConf

type LoopConf = LoopConfig

type LoopConfig

type LoopConfig struct {
	// Tick defines clock pulse base precision/resolution of a time-wheel loop in milliseconds.
	// Zero value means the default(1000 ms).
	Tick uint32

	// Work defines the tick count of work check interval.
	// Zero value means the default(1 tick).
	Work uint32

	// Idle defines the tick count of idle check interval.
	// Zero value means the default(1 tick).
	Idle uint32

	// Sync, if set to false, hook functions provided by Run
	// will be executed in another go routine, i.e. asynchronously.
	//
	// It's false by default.
	Sync bool

	// BailOnError, if set to true, tells the loop manager to break the underlying loop
	// when error is returned by user hooks, otherwise, the loop continues to next iteration.
	//
	// It's false by default.
	BailOnError bool
}

LoopConfig loop configuration, all options have default values except CbWork

type LoopHooks

type LoopHooks = LoopRunHook

type LoopRunHook

type LoopRunHook struct {
	// optional, called, once, before the underlying loop starts
	BeforeRun func() error

	// mandatory, called periodically based on the configured ticks
	Working func() error

	// optional, called periodically based on the configured ticks or suppressed if set to nil
	Stalling func() error

	// optional, called, once, before the underlying loop quits
	BeforeQuit func() error
}

LoopRunHook defines loop lifecycle callbacks. for ref, see http://docs.libuv.org/en/v1.x/loop.html

type State

type State[T string | int32] struct {
	Name   T      //name of this state
	Ticks  uint   //optional ticks to wait before trigger action, 0 means no wait
	Action Action //action of this state when ticks expire
	Args   interface{}
	// contains filtered or unexported fields
}

State is the state meta info.

type StateMachine

type StateMachine[T string | int32] struct {
	// contains filtered or unexported fields
}

StateMachine sums all states and related options to make a DFA.

func NewStateMachine

func NewStateMachine[T string | int32](name string, precision time.Duration) *StateMachine[T]

NewStateMachine creates a new state machine with the given name and ticker duration.

func (*StateMachine[T]) EnableStateTrace

func (sm *StateMachine[T]) EnableStateTrace(on bool)

EnableStateTrace enables or disables the tracing of internal flow. It's disabled by default.

func (*StateMachine[T]) GetState

func (sm *StateMachine[T]) GetState() T

GetState returns the current state.

func (*StateMachine[T]) MoveToState

func (sm *StateMachine[T]) MoveToState(s T) bool

MoveToState moves the current state to the vien one

func (*StateMachine[T]) Pause

func (sm *StateMachine[T]) Pause()

Pause pauses the internal timer tick loop.

NOTE: Not goroutine-safe.

func (*StateMachine[T]) RegisterState

func (sm *StateMachine[T]) RegisterState(s *State[T]) bool

RegisterState registers a new state to the machine. The old is replaced if a state with the same name exists.

NOTE: This method is not goroutine-safe, call it when initialization only.

func (*StateMachine[T]) RegisterStates

func (sm *StateMachine[T]) RegisterStates(ss []*State[T]) bool

RegisterStates registers a slice of states, which contains at least two elements, to the state machine.

The starting state and stopping state are set to the first and last element of the slice separately.

The State.Id field is set according to the slice suffix starting from 0.

NOTE: This method is not goroutine-safe, call it when initialization only.

func (*StateMachine[T]) RestoreState

func (sm *StateMachine[T]) RestoreState()

RestoreState moves to the latest saved state.

NOTE: Not goroutine-safe.

func (*StateMachine[T]) Resume

func (sm *StateMachine[T]) Resume()

Resume resumes the internal timer tick loop.

NOTE: Not goroutine-safe.

func (*StateMachine[T]) SaveState

func (sm *StateMachine[T]) SaveState()

SaveState saves the current state for restore.

NOTE: Not goroutine-safe.

func (*StateMachine[T]) SetStartingState

func (sm *StateMachine[T]) SetStartingState(state T) bool

SetStartingState sets a state, identified by the given name, as the starting state. False is returned if no state found for the given name, or true is returned.

NOTE: This method is not goroutine-safe, call it when initialization only.

func (*StateMachine[T]) SetStoppingState

func (sm *StateMachine[T]) SetStoppingState(state T) bool

SetStoppingState sets a state, identified by the given name, as the stopping state. False is returned if no state found for the given name, or true is returned.

NOTE: This method is not goroutine-safe, call it when initialization only.

func (*StateMachine[T]) Shutdown

func (sm *StateMachine[T]) Shutdown()

Shutdown stops the internal loop and wait until the stopping state action returns.

func (*StateMachine[T]) Startup

func (sm *StateMachine[T]) Startup() bool

Startup starts running of the machine.

type TimeWheelLoop

type TimeWheelLoop struct {
	// contains filtered or unexported fields
}

TimeWheelLoop provides a simple breakable loop impl.

func (*TimeWheelLoop) Alive

func (l *TimeWheelLoop) Alive() bool

Alive returns true if loop is running.

func (*TimeWheelLoop) Conf

func (l *TimeWheelLoop) Conf(conf LoopConfig) bool

Conf configure the loop with the given configuration. This methods should be called before Run and not be called after the loop is running.

func (*TimeWheelLoop) Name

func (l *TimeWheelLoop) Name() string

Name returns name of the loop.

func (*TimeWheelLoop) Quit

func (l *TimeWheelLoop) Quit()

func (*TimeWheelLoop) Run

func (l *TimeWheelLoop) Run(hooks LoopRunHook)

Run starts the loop with the provided hooks.

NOTE: Run is not re-entrant and must not be called within a callback. When the loop is configured to run in async mode, hook functions must be guaranteed to be goroutine-safe.

func (*TimeWheelLoop) Stop

func (l *TimeWheelLoop) Stop()

Stop stops the internal timer, close channels and clear all states.

func (*TimeWheelLoop) Stopped

func (l *TimeWheelLoop) Stopped() bool

Jump to

Keyboard shortcuts

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