fsm

package
v0.0.0-...-8f856cf Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package fsm implements a horizontally scalable finite state machine utilizing MySQL.

Traditional finite state machines manage the states in memory. Package fsm updates and gets the states directly on the MySQL side, so that it avoids the in-memory problem. Thus it is able to be scaled horizontally with no difficulty.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	*DBConfig
	// Init is the initial state of the finite state machine.
	Init string
	// Events is a map from the event name to its transitions.
	Events map[string]Event
}

Config is the configuratoin of the finite state machine.

func NewConfig

func NewConfig(db *sql.DB, table, field, init string, events []Event) *Config

NewConfig returns a new configuration for the finite state machine and its MySQL configurations.

type DBConfig

type DBConfig struct {
	// DB represents the connection to the database.
	DB *sql.DB
	// Table is the name of the table to be updated.
	Table string
	// Field refers to the field in the row to be upated.
	Field string
	// Debug deicdes whehter the debugging logs should be shown.
	Debug bool
}

DBConfig is the configuration of the MySQL database.

type Event

type Event struct {
	// Name is the name of the event.
	Name string

	// Src is a slice containing all the possible source states.
	Src []string

	// Dst is a destination state that source states transfer to when the event
	// happens.
	Dst string
}

Event holds the name of an event, with its corresponding state transitions.

type FSM

type FSM struct {
	*Config
	// ID is used in the MySQL database as the primary key.
	ID string
}

FSM is a finite state machine storing states in the MySQL database.

func NewFSM

func NewFSM(config *Config, id string) *FSM

NewFSM returns a new FSM.

func (*FSM) Current

func (fsm *FSM) Current() (string, error)

Current returns the current state of the state machine.

func (*FSM) Event

func (fsm *FSM) Event(event string) error

Event updates the state machine according to events defined.

A new record is inserted into the database if the ID doesn't exist. Otherwise the row identified by the ID is updated.

func (*FSM) Initialize

func (fsm *FSM) Initialize() error

Initialize inserts into the table with the initial state.

Jump to

Keyboard shortcuts

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