如何知道 retryPolicy.excute() 中的重试迭代次数
How to know the retry iteration number in retryPolicy.excute()
多次重试同一请求时出现错误 "The request message was already sent. Cannot send the same request message multiple times"。因此,据我所知,我需要为第二次重试重新创建一个新的 HttpRequestMessage。有没有办法知道 retryPolicy.Execute() 中的重试迭代次数,以便我可以克隆 HttpRequestMessage 进行第二次重试为空?
using Polly;
using Polly.Retry;
namespace RetryLogic {
public class HttpFactory
{
private static readonly TimeSpan defaultTimeoutInMinutes = TimeSpan.FromMinutes(5);
private static readonly RetryPolicy<HttpResponseMessage> retryPolicy;
private static readonly HttpClient client;
static HttpFactory()
{
retryPolicy = Policy
.Handle<Exception>()
.OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.GatewayTimeout
|| resp.StatusCode == HttpStatusCode.ServiceUnavailable
|| resp.StatusCode == (HttpStatusCode)550)
.WaitAndRetry(1, GetRetryDelay);
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
client = new HttpClient(handler)
{
Timeout = defaultTimeoutInMinutes
};
}
public static HttpRequestMessage CreateRequest(HttpMethod method, string pathAndQuery, NameValueCollection headers, Stream requestBody)
{
HttpRequestMessage request = new HttpRequestMessage(method, pathAndQuery);
if (requestBody != null)
{
request.Content = new StreamContent(requestBody);
request.Headers.TransferEncodingChunked = true;
}
return AddRequestHeaders(request, headers);
}
public static HttpResponseMessage MakeRequest(HttpRequestMessage request, ILoggerFactory factory, List<HttpOptions> httpOptions = null)
{
return retryPolicy.Execute(() =>
{
// How to know the retry iteration count here?
HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead;
if (httpOptions != null)
{
foreach (HttpOptions httpOption in httpOptions)
{
switch (httpOption)
{
case HttpOptions.SkipReadingResponseContent:
completionOption = HttpCompletionOption.ResponseHeadersRead;
break;
case HttpOptions.TransferChunkEncodedRequest:
client.DefaultRequestHeaders.TransferEncodingChunked = true;
break;
}
}
}
HttpResponseMessage response = client.SendAsync(request, completionOption).Result;
return response;
});
}
}
public enum HttpOptions
{
SkipReadingResponseContent,
TransferChunkEncodedRequest
} }
重试 Polly 未提供任何内置功能来了解迭代次数。
int iterationNumber = 0;
policy.Execute(() => {
iterationNumber++;
// ...
} );
多次重试同一请求时出现错误 "The request message was already sent. Cannot send the same request message multiple times"。因此,据我所知,我需要为第二次重试重新创建一个新的 HttpRequestMessage。有没有办法知道 retryPolicy.Execute() 中的重试迭代次数,以便我可以克隆 HttpRequestMessage 进行第二次重试为空?
using Polly;
using Polly.Retry;
namespace RetryLogic {
public class HttpFactory
{
private static readonly TimeSpan defaultTimeoutInMinutes = TimeSpan.FromMinutes(5);
private static readonly RetryPolicy<HttpResponseMessage> retryPolicy;
private static readonly HttpClient client;
static HttpFactory()
{
retryPolicy = Policy
.Handle<Exception>()
.OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.GatewayTimeout
|| resp.StatusCode == HttpStatusCode.ServiceUnavailable
|| resp.StatusCode == (HttpStatusCode)550)
.WaitAndRetry(1, GetRetryDelay);
var handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
client = new HttpClient(handler)
{
Timeout = defaultTimeoutInMinutes
};
}
public static HttpRequestMessage CreateRequest(HttpMethod method, string pathAndQuery, NameValueCollection headers, Stream requestBody)
{
HttpRequestMessage request = new HttpRequestMessage(method, pathAndQuery);
if (requestBody != null)
{
request.Content = new StreamContent(requestBody);
request.Headers.TransferEncodingChunked = true;
}
return AddRequestHeaders(request, headers);
}
public static HttpResponseMessage MakeRequest(HttpRequestMessage request, ILoggerFactory factory, List<HttpOptions> httpOptions = null)
{
return retryPolicy.Execute(() =>
{
// How to know the retry iteration count here?
HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead;
if (httpOptions != null)
{
foreach (HttpOptions httpOption in httpOptions)
{
switch (httpOption)
{
case HttpOptions.SkipReadingResponseContent:
completionOption = HttpCompletionOption.ResponseHeadersRead;
break;
case HttpOptions.TransferChunkEncodedRequest:
client.DefaultRequestHeaders.TransferEncodingChunked = true;
break;
}
}
}
HttpResponseMessage response = client.SendAsync(request, completionOption).Result;
return response;
});
}
}
public enum HttpOptions
{
SkipReadingResponseContent,
TransferChunkEncodedRequest
} }
重试 Polly 未提供任何内置功能来了解迭代次数。
int iterationNumber = 0;
policy.Execute(() => {
iterationNumber++;
// ...
} );