Skip to content

zjshen14/go-fsm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-fsm

It is a lightweight finite state machine (FSM) implementation in Golang. It has been inspired by the FSM used in Apache Hadoop YARN:

  • Thread safe
  • Multiple destination states
  • Dynamic destination state determination

Installation

go get github.com/zjshen14/go-fsm

Usage

package main

import (
	"fmt"

	"github.com/zjshen14/go-fsm"
)

type evt struct {
	t fsm.EventType
}

func (e *evt) Type() fsm.EventType { return e.t }

func main() {
	fsm, _ := fsm.NewBuilder().
		AddInitialState("s1").
		AddStates("s2", "s3").
		AddTransition("s1", "e1", func(event fsm.Event) (fsm.State, error) { return "s2", nil }, []fsm.State{"s2"}).
		AddTransition("s2", "e2", func(event fsm.Event) (fsm.State, error) { return "s3", nil }, []fsm.State{"s3"}).
		AddTransition("s3", "e3", func(event fsm.Event) (fsm.State, error) { return "s1", nil }, []fsm.State{"s1"}).
		Build()

	fmt.Println(fsm.CurrentState())
	fsm.Handle(&evt{t: "e1"})
	fmt.Println(fsm.CurrentState())
	fsm.Handle(&evt{t: "e2"})
	fmt.Println(fsm.CurrentState())
	fsm.Handle(&evt{t: "e3"})
	fmt.Println(fsm.CurrentState())
}

License

go-fsm is under the Apache 2.0 license. See the LICENSE file for details.

About

Lightweight FSM in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages