Skip to content

v0.9.0

Latest

Choose a tag to compare

@lingcoder lingcoder released this 19 Oct 09:53

Highlights

  • Safe for concurrent production use. Built state machines are immutable and lock-free. With 16 goroutines, FireEvent went from 35 to about 4 ns/op.
  • Stricter Build validation. Mistakes that used to fail silently at runtime now fail at build time.
  • Smaller public API. Only what users need is exported.
  • Valid, stable diagrams for any state or event name.

Breaking changes

Build no longer registers state machines. Register explicitly if you use GetStateMachine:

sm, err := builder.Build("order")
if err != nil {
	return err
}
if err := fsm.RegisterStateMachine("order", sm); err != nil {
	return err
}

Parallel transitions must be fired with FireParallelEvent. FireEvent now returns ErrParallelTransition instead of silently firing only the first branch.

Routing changed. For one state and event, the first satisfied conditional transition fires, and the unconditional transition is used only when none is satisfied. Before, an unconditional transition defined first always won. Review definitions where an unconditional transition comes before conditional ones.

New Build errors:

  • ErrIncompleteTransition: a definition never reached Perform, e.g. B -[next]-> C.
  • ErrAmbiguousTransition: more than one unconditional transition for the same state and event.
  • ErrMixedTransitions: parallel and non-parallel transitions for the same state and event.

FireParallelEvent returns partial results on failure. When an action fails, the remaining transitions are skipped and the states already reached are returned along with the error.

Action errors keep their cause. The message names the transition, e.g. action execution failed (A -[pay]-> B): db timeout. errors.Is matches both ErrActionExecutionFailed and the original error.

Internal types are no longer exported. This covers StateMachineImpl, State, NewState, Transition, TransitionType with External/Internal, the builder structs (TransitionBuilder, FromBuilder, ...) and the step markers (FromStep, ...). Code that uses the builder chain and the StateMachine interface is unaffected.

Diagram output uses generated node IDs and lists states and events sorted by name.

Fixes

  • A condition is evaluated once per FireEvent. Before, a condition whose result changed between two checks made FireEvent return the source state with a nil error and skip the action.
  • Reusing a builder after Build no longer mutates the built machine or crashes it with a concurrent map access.
  • WhenFunc(nil) and PerformFunc(nil) no longer panic. They mean "no condition" and "no action".
  • GetStateMachine explains a type-parameter mismatch.
  • Diagram labels with spaces, quotes, | or line breaks produce valid PlantUML and Mermaid.
  • ShowStateMachine and GenerateDiagram output no longer changes between calls.

Other changes

  • When is optional: On(event).Perform(action) defines an unconditional transition.
  • The README explains how to pass a context.Context through the payload.
  • Adds BenchmarkConcurrentStateTransition, and the existing benchmarks run on Windows again.

Full changelog: v0.8.0...v0.9.0