Name: machine
Owner: gophergala2016
Description: Go State Machine
Created: 2016-01-24 01:15:08.0
Updated: 2018-05-20 14:21:19.0
Pushed: 2016-01-25 04:20:06.0
Homepage: null
Size: 11
Language: Go
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
machine is a go library to write state machine. The idea came to me when I watched Rob Pike's Lexical Scanning in Go. So the basic idea is that you create a State Machine and start with initial state. Every state either generates a new state or stops the system. There are no errors coming from state machine. Since an error can lead to another state.
I tried to provide a simple and powerful interfaces so one can easily extend the state machine into next level.
there are only 3 interfaces which help me to abstract the complexity of state machine.
Joiner interface {
Wait(timeout int64)
o
Transitioner interface {
Next(State)
Fork(context.Context, ...State) Joiner
Done()
o
Machine interface {
Run(context.Context, State) Joiner
And also the main type of my state machine State
which is a simple function that accepts context.Context
and Transitioner
.
State func(context.Context, Transitioner)
by just having these 4 things, we can easily build any state machines.
Joiner
has a single method. it should be block until either timeout passes or machine run finishes and processes all states.
Transitioner
is a blue print of state pipeline. It provides state to go to another state.
Fork
is special case which creates a brand new state machine for each states that being pass into it and joins them at the end by providing a single Joiner
object.
Done
is being used to tell the state machine that state has hit the end of the process.
Machine
is the interface for the state machine engine. At the moment I have provided a simple local state machine. but consider this, you can extend it and make it as a distributed state machine.
Run
starts the engine by providing context.Context
and first initial State
. context.Context
is being used as a way to share context with all states inside that particular state machine and ability to cancel and stop state machine in the middle of process.
The project will be continued at github.com/alinz/machine