Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/robbyt/go-supervisor
go 1.25.3

require (
github.com/robbyt/go-fsm v1.5.0
github.com/robbyt/go-fsm/v2 v2.1.0
github.com/stretchr/testify v1.11.1
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/objx v0.5.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robbyt/go-fsm v1.5.0 h1:yz+6sQ73ODkL8zWOh3I7kegoSJoGwLL8nTn47ggPikc=
github.com/robbyt/go-fsm v1.5.0/go.mod h1:yvh/ZRAvdzLz9Y3UB9Zz3YbnAumn4nI2P5xvaC6e3II=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/robbyt/go-fsm/v2 v2.1.0 h1:SLmBW1QJfz5K4ZxNxGZiJYLZWHh3AtDEYWvAAQbNais=
github.com/robbyt/go-fsm/v2 v2.1.0/go.mod h1:KzfgQsGXKLPl5bi9HjMreIK3y7zpvtytz6tVVJ+CUNQ=
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
104 changes: 89 additions & 15 deletions internal/finitestate/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,113 @@ import (
"log/slog"
"time"

"github.com/robbyt/go-fsm"
"github.com/robbyt/go-fsm/v2"
"github.com/robbyt/go-fsm/v2/hooks"
"github.com/robbyt/go-fsm/v2/hooks/broadcast"
"github.com/robbyt/go-fsm/v2/transitions"
)

const (
StatusNew = fsm.StatusNew
StatusBooting = fsm.StatusBooting
StatusRunning = fsm.StatusRunning
StatusReloading = fsm.StatusReloading
StatusStopping = fsm.StatusStopping
StatusStopped = fsm.StatusStopped
StatusError = fsm.StatusError
StatusUnknown = fsm.StatusUnknown
StatusNew = transitions.StatusNew
StatusBooting = transitions.StatusBooting
StatusRunning = transitions.StatusRunning
StatusReloading = transitions.StatusReloading
StatusStopping = transitions.StatusStopping
StatusStopped = transitions.StatusStopped
StatusError = transitions.StatusError
StatusUnknown = transitions.StatusUnknown
)

// TypicalTransitions is a set of standard transitions for a finite state machine.
var TypicalTransitions = fsm.TypicalTransitions
var TypicalTransitions = transitions.Typical

// Machine is a wrapper around go-fsm.Machine that provides additional functionality.
// Machine is a wrapper around go-fsm v2 that provides the v1 API compatibility.
// It manages both the FSM and broadcast functionality.
type Machine struct {
*fsm.Machine
broadcastManager *broadcast.Manager
}

// GetStateChan returns a channel that emits the state whenever it changes.
// GetStateChanWithTimeout returns a channel that emits the state whenever it changes.
// The channel is closed when the provided context is canceled.
// For v1 API compatibility, the current state is sent immediately to the channel.
func (s *Machine) GetStateChanWithTimeout(ctx context.Context) <-chan string {
return s.GetStateChanWithOptions(ctx, fsm.WithSyncTimeout(5*time.Second))
return s.getStateChanInternal(ctx, broadcast.WithTimeout(5*time.Second))
}

// GetStateChan returns a channel that emits the state whenever it changes.
// The channel is closed when the provided context is canceled.
// For v1 API compatibility, the current state is sent immediately to the channel.
func (s *Machine) GetStateChan(ctx context.Context) <-chan string {
return s.getStateChanInternal(ctx)
}

// GetStateChanWithOptions returns a channel that emits the state whenever it changes
// with custom broadcast options.
// For v1 API compatibility, the current state is sent immediately to the channel.
func (s *Machine) GetStateChanWithOptions(ctx context.Context, opts ...broadcast.Option) <-chan string {
return s.getStateChanInternal(ctx, opts...)
}

// getStateChanInternal is a helper that creates a channel and sends the current state to it.
// This maintains v1 API compatibility where GetStateChan immediately sends the current state.
func (s *Machine) getStateChanInternal(ctx context.Context, opts ...broadcast.Option) <-chan string {
wrappedCh := make(chan string, 1)

userCh, err := s.broadcastManager.GetStateChan(ctx, opts...)
if err != nil {
close(wrappedCh)
return wrappedCh
}

currentState := s.GetState()
wrappedCh <- currentState

go func() {
defer close(wrappedCh)
for state := range userCh {
wrappedCh <- state
}
}()

return wrappedCh
}

// New creates a new finite state machine with the specified logger using "standard" state transitions.
// This function provides compatibility with the v1 API while using v2 under the hood.
func New(handler slog.Handler) (*Machine, error) {
f, err := fsm.New(handler, StatusNew, TypicalTransitions)
registry, err := hooks.NewRegistry(
hooks.WithLogHandler(handler),
hooks.WithTransitions(TypicalTransitions),
)
if err != nil {
return nil, err
}

broadcastManager := broadcast.NewManager(handler)

err = registry.RegisterPostTransitionHook(hooks.PostTransitionHookConfig{
Name: "broadcast",
From: []string{"*"},
To: []string{"*"},
Action: broadcastManager.BroadcastHook,
})
if err != nil {
return nil, err
}

f, err := fsm.New(
StatusNew,
TypicalTransitions,
fsm.WithLogHandler(handler),
fsm.WithCallbackRegistry(registry),
)
if err != nil {
return nil, err
}
return &Machine{Machine: f}, nil

return &Machine{
Machine: f,
broadcastManager: broadcastManager,
}, nil
}
23 changes: 11 additions & 12 deletions internal/finitestate/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"time"

"github.com/robbyt/go-fsm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -183,19 +182,19 @@ func TestMachineInterface(t *testing.T) {
func TestTypicalTransitions(t *testing.T) {
t.Parallel()

t.Run("verify TypicalTransitions matches fsm package", func(t *testing.T) {
assert.Equal(t, fsm.TypicalTransitions, TypicalTransitions)
t.Run("verify TypicalTransitions is not nil", func(t *testing.T) {
assert.NotNil(t, TypicalTransitions)
})

t.Run("verify status constants match fsm package", func(t *testing.T) {
assert.Equal(t, fsm.StatusNew, StatusNew)
assert.Equal(t, fsm.StatusBooting, StatusBooting)
assert.Equal(t, fsm.StatusRunning, StatusRunning)
assert.Equal(t, fsm.StatusReloading, StatusReloading)
assert.Equal(t, fsm.StatusStopping, StatusStopping)
assert.Equal(t, fsm.StatusStopped, StatusStopped)
assert.Equal(t, fsm.StatusError, StatusError)
assert.Equal(t, fsm.StatusUnknown, StatusUnknown)
t.Run("verify status constants are defined", func(t *testing.T) {
assert.Equal(t, "New", StatusNew)
assert.Equal(t, "Booting", StatusBooting)
assert.Equal(t, "Running", StatusRunning)
assert.Equal(t, "Reloading", StatusReloading)
assert.Equal(t, "Stopping", StatusStopping)
assert.Equal(t, "Stopped", StatusStopped)
assert.Equal(t, "Error", StatusError)
assert.Equal(t, "Unknown", StatusUnknown)
})
}

Expand Down