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();
}
You need to set
TimeoutStrategy.Pessimisticbecause theThread.Sleepdoes not useCancellationTokento interupt its waiting.Optimistic means that your to-be-decorated method can collaboratively cancel its work via a
CancellationTokenExecute.tokento bothDelayandWaitWait:Task.Delay(5000).Wait(token);Dotnet fiddle link: https://dotnetfiddle.net/ei6iPA