Sync Polly with timeout

22 views Asked by At

This code runs to the end, no TimeoutRejectedException. If I change the strategy to TimeoutStrategy.Pessimistic, it will enter the executed logic twice, before throwing the TimeoutRejectedException.

What should I do, if I want to retry after a timeout with the retry policy? (if it does a timeout, I want to try again after a pause until I hit MaxRetries)?

Is the only solution to make my executing logic itself throw the timeout? (The code is .net framework 4.8 and it made to run in LinqPad 5 and Polly v 8.3.1).

The Optimistic strategy does not seem to actually do anything in this setup, does it?

var retry = Policy
    .Handle<Exception>()
    .Retry(1);

var timeout = Policy
    .Timeout(1, Polly.Timeout.TimeoutStrategy.Optimistic);

var wrap = Policy.Wrap(retry, timeout);

try
{
    wrap
        .Execute(() =>
        {
            "Enter executed logic".Dump();
            Thread.Sleep(5000);
            "Exit executed logic".Dump();
        });
}
catch(TimeoutRejectedException ex)
{
    ex.Message.Dump();
}
1

There are 1 answers

1
Peter Csala On BEST ANSWER

You need to set TimeoutStrategy.Pessimistic because the Thread.Sleep does not use CancellationToken to interupt its waiting.

var retry = Policy
    .Handle<Exception>()
    .Retry(1);

var timeout = Policy
    .Timeout(1, Polly.Timeout.TimeoutStrategy.Pessimistic);

var wrap = Policy.Wrap(retry, timeout);

try
{
    wrap
        .Execute(() =>
        {
            "Enter executed logic".Dump();
            Thread.Sleep(5000);
            "Exit executed logic".Dump();
        });
}
catch(TimeoutRejectedException ex)
{
    ex.Message.Dump();
}

Optimistic means that your to-be-decorated method can collaboratively cancel its work via a CancellationToken

var retry = Policy
    .Handle<Exception>()
    .Retry(1);

var timeout = Policy
    .Timeout(1, Polly.Timeout.TimeoutStrategy.Optimistic);

var wrap = Policy.Wrap(retry, timeout);

try
{
    wrap
        .Execute(token =>
        {
            "Enter executed logic".Dump();
            Task.Delay(5000, token).Wait(token);
            "Exit executed logic".Dump();
        }, CancellationToken.None);
}
catch(TimeoutRejectedException ex)
{
    ex.Message.Dump();
}
  • Please note that in this case I've used a different overload of Execute.
  • It is not mandatory to pass the token to both Delay and Wait
    • It is enough to pass only to the Wait: Task.Delay(5000).Wait(token);

Dotnet fiddle link: https://dotnetfiddle.net/ei6iPA