mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-30 15:40:59 +02:00
protofsm: add ErrorReporter interface
We'll use this to be able to signal to a caller that a critical error occurred during the state transition.
This commit is contained in:
@@ -144,9 +144,21 @@ type StateMachine[Event any, Env Environment] struct {
|
|||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorReporter is an interface that's used to report errors that occur during
|
||||||
|
// state machine execution.
|
||||||
|
type ErrorReporter interface {
|
||||||
|
// ReportError is a method that's used to report an error that occurred
|
||||||
|
// during state machine execution.
|
||||||
|
ReportError(err error)
|
||||||
|
}
|
||||||
|
|
||||||
// StateMachineCfg is a configuration struct that's used to create a new state
|
// StateMachineCfg is a configuration struct that's used to create a new state
|
||||||
// machine.
|
// machine.
|
||||||
type StateMachineCfg[Event any, Env Environment] struct {
|
type StateMachineCfg[Event any, Env Environment] struct {
|
||||||
|
// ErrorReporter is used to report errors that occur during state
|
||||||
|
// transitions.
|
||||||
|
ErrorReporter ErrorReporter
|
||||||
|
|
||||||
// Daemon is a set of adapters that will be used to bridge the FSM to
|
// Daemon is a set of adapters that will be used to bridge the FSM to
|
||||||
// the daemon.
|
// the daemon.
|
||||||
Daemon DaemonAdapters
|
Daemon DaemonAdapters
|
||||||
@@ -586,6 +598,11 @@ func (s *StateMachine[Event, Env]) applyEvents(currentState State[Event, Env],
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Infof("FSM(%v): state transition: from_state=%T, "+
|
||||||
|
"to_state=%T",
|
||||||
|
s.cfg.Env.Name(), currentState,
|
||||||
|
transition.NextState)
|
||||||
|
|
||||||
// With our events processed, we'll now update our
|
// With our events processed, we'll now update our
|
||||||
// internal state.
|
// internal state.
|
||||||
currentState = transition.NextState
|
currentState = transition.NextState
|
||||||
@@ -638,9 +655,15 @@ func (s *StateMachine[Event, Env]) driveMachine() {
|
|||||||
case newEvent := <-s.events:
|
case newEvent := <-s.events:
|
||||||
newState, err := s.applyEvents(currentState, newEvent)
|
newState, err := s.applyEvents(currentState, newEvent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO(roasbeef): hard error?
|
s.cfg.ErrorReporter.ReportError(err)
|
||||||
|
|
||||||
log.Errorf("unable to apply event: %v", err)
|
log.Errorf("unable to apply event: %v", err)
|
||||||
continue
|
|
||||||
|
// An error occurred, so we'll tear down the
|
||||||
|
// entire state machine as we can't proceed.
|
||||||
|
go s.Stop()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
currentState = newState
|
currentState = newState
|
||||||
|
Reference in New Issue
Block a user