From f331e2cc3538e8db8dd13259184ec926f2dc9dc6 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 4 Mar 2025 18:12:41 -0800 Subject: [PATCH] protofsm: add an upfront check for SendWhen predicates In this commit, we add an upfront check for `SendWhen` predicates before deciding to launch a goroutine. This ensures that when a message comes along that is already ready to send, we do the send in a synchronous manner. --- protofsm/state_machine.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/protofsm/state_machine.go b/protofsm/state_machine.go index 4d0215b69..c759d061a 100644 --- a/protofsm/state_machine.go +++ b/protofsm/state_machine.go @@ -377,9 +377,18 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context, }) } - // If this doesn't have a SendWhen predicate, then we can just - // send it off right away. - if !daemonEvent.SendWhen.IsSome() { + canSend := func() bool { + return fn.MapOptionZ( + daemonEvent.SendWhen, + func(pred SendPredicate) bool { + return pred() + }, + ) + } + + // If this doesn't have a SendWhen predicate, or if it's already + // true, then we can just send it off right away. + if !daemonEvent.SendWhen.IsSome() || canSend() { return sendAndCleanUp() } @@ -397,14 +406,7 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context, for { select { case <-predicateTicker.C: - canSend := fn.MapOptionZ( - daemonEvent.SendWhen, - func(pred SendPredicate) bool { - return pred() - }, - ) - - if canSend { + if canSend() { s.log.InfoS(ctx, "Send active predicate") err := sendAndCleanUp()