Does Polly Retry on errors like "connection was terminated abnormally" or "A connection with server could not be established" etc

38 views Asked by At

I am using HTTPClient with WinHttpHanlder (for http/2) and my Polly retry policy looks something like this

var waitAndRetry = Policy
.HandleResult<HttpResponseMessage>(r => HTTPCodesWorthRetrying(r))
.Or<HandleRequestException>()
.Or<TimeoutRejectionException>().
 WaitAndRetryAsync(
 3,
 retryAttempt => ExponentialBackoff(retryAttempt),
 onRetry => {
   //some logging
 }
)                

I have noticed that in case of HandleRequestException (with IOException or WinHttpException as inner exceptions) call is retried 3 times but still fails.

My question is :

  1. Does Polly reconnects and establishes new connection during retry on errors like "connection was terminated abnormally" or "A connection with server could not be established" etc or "Server returned invalid or unrecognized response".

  2. Or I am retrying too quickly, initial retry delay is 100ms ?

Did not find any details in polly documentation.

1

There are 1 answers

0
Peter Csala On

Does Polly reconnects and establishes new connection during retry on errors like "connection was terminated abnormally" or "A connection with server could not be established" etc or "Server returned invalid or unrecognized response".

Polly's retry policy (or strategy in case of V8) is domain agnostic. That means they don't have any "smart" logic depending on what they are decorating. The exact same retry logic is executed for a mathematical calculation or for a network call.

For each and every attempt (including the initial attempt as well) the exact same user provided delegate/callback is executed. You have the ability to slightly modify the execution by changing a variable inside the onRetry which is defined outside of the policy.

var downstreamUrl = "primary-host-address";
var waitAndRetry = Policy
.HandleResult<HttpResponseMessage>(r => HTTPCodesWorthRetrying(r))
 WaitAndRetryAsync(..., onRetry => {
   string downstreamUrl = "secondary-host-address";
 }
);               

waitAndRetry.ExecuteAsync(ct => Foo(downstreamUrl, ct), CancellationToken.None);

Or I am retrying too quickly, initial retry delay is 100ms ?

Most probably yes. If there is a connection issue due to some transient network failure it is unlikely that it will self-heal via that short time. It is a good practice to use exponential back-off. This contrib defines a backoff strategy called DecorrelatedJitterBackoffV2 which can be really handy in this situation.