在 500 响应之后,在进程重新启动之前不会再次发送请求

After a 500 response no requests are sent again until process is restarted

我正在使用 Polly 来实现请求重试。但是,在请求返回 500 响应后,Polly 在重试时不仅不发送请求,而且在进程重新启动之前根本不发送请求。相反,Polly 只是等到超时。以下是代码:

var timeoutPolicy = TimeoutPolicy
    .TimeoutAsync<HttpResponseMessage>(c => c.GetTimeout() ?? TimeSpan.FromSeconds(30)); //timeout para cada intento

var retryPolicy = HttpPolicyExtensions
    .HandleTransientHttpError()
    .Or<TimeoutRejectedException>() //tirado por la TimeoutPolicy
    .WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) });

var policyHandler = new PolicyHandler(Policy.WrapAsync(retryPolicy, timeoutPolicy));

我如何修复代码,使 Polly 在 500 后重试时实际发送请求以及后续请求?

我相信应用程序崩溃与这些政策无关。

我已经使用如下调试日志修改了您的代码:

public static readonly HttpClient client = new HttpClient();
public static async Task Main(string[] args)
{
    var sw = Stopwatch.StartNew();
    Console.WriteLine($"{sw.Elapsed.Seconds}: Application starts");

    var timeoutPolicy = TimeoutPolicy
        .TimeoutAsync<HttpResponseMessage>(
            TimeSpan.FromSeconds(30),
            onTimeoutAsync: (ctx, ts, t) => {
                Console.WriteLine($"{sw.Elapsed.Seconds}: Timeout has occurred");
                return Task.CompletedTask;
        });

    var retryPolicy = HttpPolicyExtensions
        .HandleTransientHttpError()
        .Or<TimeoutRejectedException>() 
        .WaitAndRetryAsync(
            new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) },
            onRetryAsync: (dr, ts) => {
                Console.WriteLine($"{sw.Elapsed.Seconds}: Retry will be triggered");
                return Task.CompletedTask;
            });

    var strategy = Policy.WrapAsync(retryPolicy, timeoutPolicy);
    await strategy.ExecuteAsync(async (ct) => {
        var response = await client.GetAsync("https://httpstat.us/500");
        Console.WriteLine($"{sw.Elapsed.Seconds}: Response has been received");
        return response;
    }, CancellationToken.None);

    Console.WriteLine($"{sw.Elapsed.Seconds}: Application finishes");
}
  • 只要 httpClient 收到响应,它就会打印 Response has been received
    • 我使用 httpstat.us 仅用于测试目的
  • 只要 httpClient 在 30 秒内没有收到响应,它就会打印 Timeout has occurred
  • 每当应该触发重试策略但在延迟惩罚之前打印Retry will be triggered
  • 每当发出所有重试且其中 none 次成功或其中一个请求成功时,它会打印 Application finishes

使用当前设置,输出将如下所示:

1: Response has been received
1: Retry will be triggered
2: Response has been received
2: Retry will be triggered
8: Response has been received
8: Retry will be triggered
18: Response has been received
18: Application finishes

如您所见,我们发出了 4 次请求(一次初始请求和 3 次重试),但运气不佳。但最终应用程序继续其工作。

错误是由以下原因引起的issue

我是这样修复的:

var retryPolicy = HttpPolicyExtensions
    .HandleTransientHttpError()
    .Or<TimeoutRejectedException>() //tirado por la TimeoutPolicy
    .WaitAndRetryAsync(new[] 
      { 
        TimeSpan.FromSeconds(1), 
        TimeSpan.FromSeconds(5), 
        TimeSpan.FromSeconds(10) 
      }, 
      onRetry: (x, _) =>
      {
          //dispose response when retrying
          x.Result?.Dispose();
      });