2020-01-13 17:44:59 +00:00
|
|
|
package statemachine
|
2020-01-06 21:01:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-01-13 16:34:21 +00:00
|
|
|
"github.com/filecoin-project/go-statestore"
|
|
|
|
|
2020-01-06 21:01:49 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("evtsm")
|
|
|
|
|
2020-01-13 17:44:59 +00:00
|
|
|
type Event struct {
|
|
|
|
User interface{}
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:01:49 +00:00
|
|
|
// returns func(ctx Context, st <T>) (func(*<T>), error), where <T> is the typeOf(User) param
|
|
|
|
type Planner func(events []Event, user interface{}) (interface{}, error)
|
|
|
|
|
2020-01-13 17:44:59 +00:00
|
|
|
type StateMachine struct {
|
2020-01-06 21:01:49 +00:00
|
|
|
planner Planner
|
|
|
|
eventsIn chan Event
|
|
|
|
|
|
|
|
name interface{}
|
|
|
|
st *statestore.StoredState
|
|
|
|
stateType reflect.Type
|
|
|
|
|
|
|
|
stageDone chan struct{}
|
|
|
|
closing chan struct{}
|
|
|
|
closed chan struct{}
|
|
|
|
|
|
|
|
busy int32
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:44:59 +00:00
|
|
|
func (fsm *StateMachine) run() {
|
2020-01-06 21:01:49 +00:00
|
|
|
defer close(fsm.closed)
|
|
|
|
|
|
|
|
var pendingEvents []Event
|
|
|
|
|
|
|
|
for {
|
|
|
|
// NOTE: This requires at least one event to be sent to trigger a stage
|
|
|
|
// This means that after restarting the state machine users of this
|
|
|
|
// code must send a 'restart' event
|
|
|
|
select {
|
|
|
|
case evt := <-fsm.eventsIn:
|
|
|
|
pendingEvents = append(pendingEvents, evt)
|
|
|
|
case <-fsm.stageDone:
|
|
|
|
if len(pendingEvents) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-fsm.closing:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if atomic.CompareAndSwapInt32(&fsm.busy, 0, 1) {
|
|
|
|
var nextStep interface{}
|
|
|
|
var ustate interface{}
|
|
|
|
|
|
|
|
err := fsm.mutateUser(func(user interface{}) (err error) {
|
|
|
|
nextStep, err = fsm.planner(pendingEvents, user)
|
|
|
|
ustate = user
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Executing event planner failed: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pendingEvents = nil
|
|
|
|
|
|
|
|
if nextStep == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := Context{
|
|
|
|
ctx: context.TODO(),
|
|
|
|
send: func(evt interface{}) error {
|
|
|
|
return fsm.send(Event{User: evt})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
res := reflect.ValueOf(nextStep).Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(ustate).Elem()})
|
|
|
|
|
|
|
|
if res[0].Interface() != nil {
|
|
|
|
log.Errorf("executing step: %+v", res[0].Interface().(error)) // TODO: propagate top level
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StoreInt32(&fsm.busy, 0)
|
|
|
|
fsm.stageDone <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:44:59 +00:00
|
|
|
func (fsm *StateMachine) mutateUser(cb func(user interface{}) error) error {
|
2020-01-06 21:01:49 +00:00
|
|
|
mutt := reflect.FuncOf([]reflect.Type{reflect.PtrTo(fsm.stateType)}, []reflect.Type{reflect.TypeOf(new(error)).Elem()}, false)
|
|
|
|
|
|
|
|
mutf := reflect.MakeFunc(mutt, func(args []reflect.Value) (results []reflect.Value) {
|
|
|
|
err := cb(args[0].Interface())
|
|
|
|
return []reflect.Value{reflect.ValueOf(&err).Elem()}
|
|
|
|
})
|
|
|
|
|
|
|
|
return fsm.st.Mutate(mutf.Interface())
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:44:59 +00:00
|
|
|
func (fsm *StateMachine) send(evt Event) error {
|
2020-01-06 21:01:49 +00:00
|
|
|
fsm.eventsIn <- evt // TODO: ctx, at least
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:44:59 +00:00
|
|
|
func (fsm *StateMachine) stop(ctx context.Context) error {
|
2020-01-06 21:01:49 +00:00
|
|
|
close(fsm.closing)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-fsm.closed:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|