如何在使用Poly重试策略时处理100秒超时



我在.net核心应用程序中使用重试策略,超过100秒后超时。我可能会以某种不正确的方式使用Poly吗?或者它是故意的,只增加超时时间可能会有所帮助?

以下是我使用Poly的方式:启动:

// populate timeouts array from appsettings
var resilencyOptions = services.BuildServiceProvider().GetRequiredService<IOptions<ResiliencyOptions>>().Value;
var attempts = resilencyOptions.TimeOutsInSeconds.Count;
TimeSpan[] timeouts = new TimeSpan[attempts];
int i = 0;
foreach (var timeout in resilencyOptions.TimeOutsInSeconds)
{
timeouts[i++] = TimeSpan.FromSeconds(timeout);
}
// register
services.AddTransient<LoggingDelegatingHandler>();
services.AddHttpClient<IMyClient, MyClient>()
.AddHttpMessageHandler<LoggingDelegatingHandler>()
.AddPolicyHandler(ResiliencyPolicy.GetRetryPolicy(attempts, timeouts))
.AddPolicyHandler(ResiliencyPolicy.GetCircuitBreakerPolicy());

图书馆:

/// <summary>
/// Resiliency policy.
/// </summary>
public class ResiliencyPolicy
{
/// <summary>
/// Get a retry policy.
/// </summary>
/// <param name="numberofAttempts"> Количество попыток.</param>
/// <param name="timeOfAttempts"> Массив с таймаутами между попытками, если передается неполный или пустой, попытки делаются в секундах 2^.</param>
/// <returns></returns>
public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(int numberofAttempts = 5, TimeSpan[] timeOfAttempts = null)
{
//  In case timeOfAttempts is null or its elements count doesnt correspond to number of attempts provided,
//  we will wait for:
//  2 ^ 1 = 2 seconds then
//  2 ^ 2 = 4 seconds then
//  2 ^ 3 = 8 seconds then
//  2 ^ 4 = 16 seconds then
//  2 ^ 5 = 32 seconds
return HttpPolicyExtensions
.HandleTransientHttpError()
.WaitAndRetryAsync(
retryCount: numberofAttempts,
sleepDurationProvider: retryAttempt =>  ((timeOfAttempts == null) || (timeOfAttempts.Length != numberofAttempts)) ?
TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) :
timeOfAttempts[retryAttempt],
onRetry: (exception, retryCount, context) =>
{
Logging.Global.LogError($"Retry {retryCount} of {context.PolicyKey} at {context.OperationKey}, due to: {exception}.");
});
}
/// <summary>
/// Get circuit breaker policy.
/// </summary>
/// <param name="numberofAttempts">количество попыток</param>
/// <param name="durationOfBreaksInSeconds">количество секунд (таймаут) между попытками</param>
/// <returns></returns>
public static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy(int numberofAttempts = 5, int durationOfBreaksInSeconds = 30)
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.CircuitBreakerAsync(
handledEventsAllowedBeforeBreaking: numberofAttempts,
durationOfBreak: TimeSpan.FromSeconds(durationOfBreaksInSeconds)
);
}
}

从自定义http客户端调用:

public class MyClient : IMyClient
{
private readonly HttpClient _httpClient;
private readonly ILogger<MyClient> _logger;
public MyClient(HttpClient httpClient, ILogger<MyClient> logger)
{
_httpClient = httpClient;
_logger = logger;
}
public async Task<bool> Notify(string url, Guid id, string orderId, int state, int category, DateTime date, CancellationToken cancellationToken)
{
// prepare request
var request = new
{
Id = id,
OrderId = orderId,
State = state,
Category = category,
Date = date
};
var data = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
// send request
_logger.LogInformation("sending request to {url}", url);
var response = await _httpClient.PostAsync(url, data, cancellationToken);
// process response
if (response.IsSuccessStatusCode)
return true;
var content = await response.Content.ReadAsStringAsync(cancellationToken);
response.Content?.Dispose();
throw new HttpRequestException($"{response.ReasonPhrase}. {content.Replace(""", "").TrimEnd()}", null, response.StatusCode);
}
}

控制器模拟端点可用性:

[ApiController]
[Route("[controller]")]
public class RabbitController : ControllerBase
{
private static int _numAttempts;
public RabbitController(IBus client)
{
_client = client;
}
[HttpPost("ProcessTestREST")]
public IActionResult ProcessTestREST(Object data)
{
_numAttempts++;
if (_numAttempts%4==3)
{
return Ok();
}
else
{
return StatusCode((int)HttpStatusCode.InternalServerError, "Something went wrong");
}
}
}    

我得到这个错误:

"由于配置了HttpClient,请求被取消。超时时间为100秒">

这里需要注意的重要一点是,HttpClient.Timeout适用于整个调用集合,其中包括所有重试和等待:https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory#use-案例应用超时

HttpClient的默认值是100秒,所以如果重试和等待时间超过了100秒,那么Polly将抛出TimeoutException。

有几种方法可以解决这个问题:

  1. HttpClient.Timeout设置为所有重试所需的最大时间长度
  2. 将超时策略置于重试策略之前,这使其行为类似于全局超时策略

在我的情况下,我做了#1,因为我希望我的超时策略独立应用于每个请求,所以我在重试策略之后保留了超时策略。文档进一步解释了这是如何工作的。

检查https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0#动态选择策略

var timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(
TimeSpan.FromSeconds(10));
var longTimeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(
TimeSpan.FromSeconds(30));
builder.Services.AddHttpClient("PollyDynamic")
.AddPolicyHandler(httpRequestMessage =>
httpRequestMessage.Method == HttpMethod.Get ? timeoutPolicy : longTimeoutPolicy);

超时策略应在AddHttpClient阶段设置,以覆盖官方文档中定义的100秒默认值。

您对polly相关请求的超时应该涵盖重试策略的最大值。

请注意,如果您想忽略重试,请使用自定义客户端,以便超时为默认值。

您需要确保HttpClient的超时大于Polly策略的任何超时。您需要使用AddHttpClient重载,将客户端的默认超时从100秒更改为100秒。

var notFoundTimeout = TimeSpan.FromMinutes(5);
var transientTimeout = TimeSpan.FromSeconds(5);
var clientTimeout = notFoundTimeout.Add(new TimeSpan(0, 1, 0));
var notFoundRetryPolicy = Policy.Handle<HttpRequestException>() // 404 not found errors
.OrResult<HttpResponseMessage>(response => response.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(3, (int tryIndex) => notFoundTimeout);
services.AddHttpClient(CLIENT_NAME, config => config.Timeout = clientTimeout)
.AddPolicyHandler(notFoundRetryPolicy)
.AddTransientHttpErrorPolicy(
builder => builder.WaitAndRetryAsync(3, (int tryIndex) => transientTimeout));

我可能会迟到,但允许我投入2美分。

所有其他的答案都集中在HttpClientTimeout属性的100秒默认值上,并试图解决这个问题。真正的问题是AddPolicyHandler如何在引擎盖下工作。

我在这里详细介绍了PolicyHttpMessageHandler是如何破坏党的。在类型化的HttpClient的情况下,解决方案是在类型化客户端内部移动策略,以避免使用AddPolicyHandler


您已经将策略分离到一个专用类ResiliencyPolicy中。(顺便说一句,您可以将类声明为static(。我建议公开一个组合策略,而不是公开两个策略。

public static IAsyncPolicy<HttpResponseMessage> GetCombinedPolicy(int attempts = 5, TimeSpan[] timeouts = null)
=> Policy.WrapAsync<HttpResponseMessage>(GetRetryPolicy(attempts, timeouts), GetCircuitBreakerPolicy())

您可以在构造HttpClient:时尝试此操作

HttpClient client = new();
client.Timeout = TimeSpan.FromMinutes(5); // or your desire

最新更新