Skip to content

Instantly share code, notes, and snippets.

@smothiki
Created October 11, 2016 21:46
Show Gist options
  • Save smothiki/9bd4211481d97b9b031c96239ecd14a3 to your computer and use it in GitHub Desktop.
Save smothiki/9bd4211481d97b9b031c96239ecd14a3 to your computer and use it in GitHub Desktop.

Simple state machine example

  • This example contains a simple.go which is the main program and example.go will have the execution steps of a state machine. Util.go describes state machine.
  • The state machine has an init event which starts from Idle state and reach state1.
  • close event ends with closed state.
  • every transition event is between init and close. A transition from state1 to state2 is named as state1_2.
  • Every intermediate state except from idle and closed are reentrants only if there is an error and this reentrant states are handled in error transition.

A small ASCII Diagram for how it works

                                                                     +--------->-----+
                                     +----->-------+                 |    error      |
                                     |   error     |                 +---------<-----+
       +-----------+                 +-----<-------+                 |               |
       |           |                 |             |    state1_2     |    state2     |
       | idle      |     init        |  state1     |                 |               |
       |           +---------------->+             +---------------->+               |
       |           |                 |             |                 +---------------+
       +-----------+                 +-------------+                         |
                                                                             |
                                                                             |  state2_3
                                                                             |
                                                                             v
                                                                      +--------------------->---+
                                      +---------------+    state3_4   |               |         |
        +-----------+    close        |               |               |     state3    |  error  |
        |           |                 |  state4       +<--------------+               |         |
        |           +<----------------+               |               |               |         |
        |   closed  |                 |               |               +----------------^-----<--+
        |           |                 +-------->------+
        +-----------+                 ^               |
                                      |    error      |
                                      +--------<------+
  • Above diagram represents a simple state machine that starts with Idle and ends with closed.
  • Every event is a transition from State X to State Y. An Error transition occurs from State X to State X.
  • Error transition is re-entrant and executes as per the # of retries.
  • If for a Job a particular state was already executed and started with even init. All the transitions are No Ops until a transition with the previous state was encountered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment