I am using httpClient to do a POST request. I am using Polly for retries. What is happening is that the first attempt takes 14s even though I specified a retry time of 2s. The first attempt fails after 14s, then there is a 2s gap until the second attempt. What I want it to try every 2s and timeout and retry on any error. Is this the right way of doing it?
var retryPolicy = Policy
.Handle<Exception>() // retry on any
.WaitAndRetryAsync(6,
retryAttempt => TimeSpan.FromMilliseconds(2000),
(response, calculatedWaitDuration, ctx) =>
{
Log.LogError($"Failed attempt {attempt++}. Waited for {calculatedWaitDuration}. Exception: {response?.ToString()}");
});
HttpResponseMessage httpResp = null;
await retryPolicy.ExecuteAsync(async () =>
{
httpResp = await DoPost();
httpResp?.EnsureSuccessStatusCode(); // throws HttpRequestException
return httpResp;
});
var respBody = await httpResp.Content.ReadAsStringAsync();
return respBody;
async Task<HttpResponseMessage> DoPost()
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new StringContent(json, Encoding.UTF8, Constants.JsonContentType),
Headers = { Authorization = await GetAuthenticationTokenAsync() }
};
ServicePointManager.Expect100Continue = false;
var httpResponseMessage = await StaticHttpClient.SendAsync(httpRequestMessage).ConfigureAwait(false);
return httpResponseMessage;
}