Selector executing both

I have the following workflow:

func MyWorkflow(ctx workflow.Context,...) error {
    duration := time.Until(sendAt)
    timerFuture := workflow.NewTimer(ctx, duration)
    selector.AddReceive(ctx.Done(), func(c workflow.ReceiveChannel, more bool) {...})
    selector.AddFuture(timerFuture, func(f workflow.Future) {...})
    selector.Select(ctx)
}

So selector at the bottom will block until one of two things happen:

  1. ctx.Done()
  2. timer is due

So when I execute CancelWorkflow, it still goes inside the closure of AddFuture.
How can I make it so that on Cancel, only closure of AddReceive is executed and then finishes workflow?

Thanks

Your example can be replaced with a single workflow.Sleep. It returns an error when the context passed to the call is cancelled.

The same happens for the Timer. Its future becomes ready with CanceledError on the context cancelation.

So no need to wait for the ctx.Done.

1 Like