Skip to content

Commit

Permalink
protofsm: add an upfront check for SendWhen predicates
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Roasbeef committed Mar 5, 2025
1 parent dacc5df commit f331e2c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions protofsm/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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()
Expand Down

0 comments on commit f331e2c

Please sign in to comment.