基于状态的Restsharp Polly重试政策

发布于 2025-02-09 21:15:38 字数 463 浏览 2 评论 0 原文

我试图根据响应状态代码操纵Polly重试策略。 如果状态代码为500,我需要在3分钟后重试,否则我需要在2、4秒后重试。 我现在有类似的东西,

.OrResult<RestResponse>(
    (response) => {
        return !response.IsSuccessful || response.StatusCode != System.Net.HttpStatusCode.Conflict;
    })
.WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15) })  

我可以添加 timespan.fromseconds(180),但是我只想在响应状态代码为500时才这样做。

有没有办法这样做?

I am trying to manipulate polly retry policy based on response status code.
If status code is 500, I need to retry after 3 minutes else I need to retry after 2, 4 seconds.
I have something like this right now,

.OrResult<RestResponse>(
    (response) => {
        return !response.IsSuccessful || response.StatusCode != System.Net.HttpStatusCode.Conflict;
    })
.WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15) })  

I can possibly add TimeSpan.FromSeconds(180) but I only want to do it if the response status code is 500.

Is there a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-02-16 21:15:38

waitandRetryAsync 具有几个过载。在您的情况下,您使用了一个接受 iEnumerable&lt; timespan&gt; 定义重试计数和每次尝试之间的延迟的方法。

有超载使您动态定义睡眠。在这种情况下,您必须提供 SleepDurationProvider

在波纹管示例中,我使用了

 public static AsyncRetryPolicy<TResult> WaitAndRetryAsync<TResult>(
       this PolicyBuilder<TResult> policyBuilder, 
       int retryCount,
       Func<int, DelegateResult<TResult>, Context, TimeSpan> sleepDurationProvider, 
       Func<DelegateResult<TResult>, TimeSpan, int, Context, Task> onRetryAsync)
  • int retrycount :最多应发出多少重试尝试
  • func&lt; int,delegeteresult&lt; tresult&gt;,context&gt timespan&gt; SleepDurationProvider :一个用户定义的功能,它接收重试尝试的数量,尝试的结果,上下文并预测 timespan
  • func&lt; func&lt; delegateresult&lt; timespan,int,上下文,任务&gt; onRetryAsync :用户定义的功能,如果应触发策略但在睡眠之前,

您可以实现所需的行为:

.WaitAndRetryAsync(retryCount: 3, 
    sleepDurationProvider: (int retryCount, DelegateResult<RestResponse> response, Context ctx) =>
{
    if (response.Result.StatusCode == HttpStatusCode.InternalServerError)
        return TimeSpan.FromMinutes(3);
    return retryCount switch
    {
        1 => TimeSpan.FromSeconds(2),
        2 => TimeSpan.FromSeconds(5),
        3 => TimeSpan.FromSeconds(15),
        _ => TimeSpan.FromSeconds(1) //It won't be used due to the retryCount
    };
}, onRetryAsync: (_, __, ___, ____) => Task.CompletedTask);
  • 如果响应的状态代码为500,则在3分钟的睡眠时间内返回
    • 在其他情况下,根据尝试数量来决定睡眠时间
  • 对于所有

The WaitAndRetryAsync has several overloads. In your case you have used one which accepts an IEnumerable<TimeSpan> which defines the retry count and the delays between each attempt.

There are overloads which allows you to define the sleepDurations dynamically. In those cases you have to provide a sleepDurationProvider.

In the bellow example I've used this overload:

 public static AsyncRetryPolicy<TResult> WaitAndRetryAsync<TResult>(
       this PolicyBuilder<TResult> policyBuilder, 
       int retryCount,
       Func<int, DelegateResult<TResult>, Context, TimeSpan> sleepDurationProvider, 
       Func<DelegateResult<TResult>, TimeSpan, int, Context, Task> onRetryAsync)
  • int retryCount: At most how many retry attempts should be issued
  • Func<int, DelegateResult<TResult>, Context, TimeSpan> sleepDurationProvider: A user-defined function which receives the number of retry attempt, the result of the attempt, a context and anticipates a TimeSpan in return
  • Func<DelegateResult<TResult>, TimeSpan, int, Context, Task> onRetryAsync: A user-defined function which is called if the policy should be triggered but before the sleep

With this you can achieve the desired behaviour:

.WaitAndRetryAsync(retryCount: 3, 
    sleepDurationProvider: (int retryCount, DelegateResult<RestResponse> response, Context ctx) =>
{
    if (response.Result.StatusCode == HttpStatusCode.InternalServerError)
        return TimeSpan.FromMinutes(3);
    return retryCount switch
    {
        1 => TimeSpan.FromSeconds(2),
        2 => TimeSpan.FromSeconds(5),
        3 => TimeSpan.FromSeconds(15),
        _ => TimeSpan.FromSeconds(1) //It won't be used due to the retryCount
    };
}, onRetryAsync: (_, __, ___, ____) => Task.CompletedTask);
  • If the response's status code is 500 then return with a 3 minutes sleep duration
    • For every other cases decide the sleep duration based on the number of attempt
  • The onRetryAsync delegate needs to be defined otherwise the compiler will not find this overload
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文