Skip to content

Commit 13bdec5

Browse files
committed
auto advance will try to advance state machine once per specified period, instead of waiting for the period between attempts
1 parent c6aa523 commit 13bdec5

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

statemachine.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func (sm *StateMachine) EmergencySwitch(stateId string, triggerEvents ...bool) (
238238

239239
func (sm *StateMachine) AutoAdvance(tryPeriod time.Duration, terminalStates []string) interface{} {
240240
for {
241+
ct := time.Now()
241242
result, err := sm.Advance()
242243
if err != nil {
243244
// stop state machine
@@ -253,7 +254,10 @@ func (sm *StateMachine) AutoAdvance(tryPeriod time.Duration, terminalStates []st
253254
}
254255
} else {
255256
// cannot advance yet, wait
256-
time.Sleep(tryPeriod)
257+
passed := time.Now().Sub(ct)
258+
if passed.Nanoseconds() < tryPeriod.Nanoseconds() {
259+
time.Sleep(time.Duration(tryPeriod.Nanoseconds() - passed.Nanoseconds()))
260+
}
257261
}
258262
}
259263
}

0 commit comments

Comments
 (0)