基于状态的Restsharp Polly重试政策
我试图根据响应状态代码操纵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时才这样做。
有没有办法这样做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
waitandRetryAsync
具有几个过载。在您的情况下,您使用了一个接受iEnumerable&lt; timespan&gt;
定义重试计数和每次尝试之间的延迟的方法。有超载使您动态定义
睡眠
。在这种情况下,您必须提供SleepDurationProvider
。在波纹管示例中,我使用了:
int retrycount
:最多应发出多少重试尝试func&lt; int,delegeteresult&lt; tresult&gt;,context&gt timespan&gt; SleepDurationProvider
:一个用户定义的功能,它接收重试尝试的数量,尝试的结果,上下文并预测timespan
您可以实现所需的行为:
The
WaitAndRetryAsync
has several overloads. In your case you have used one which accepts anIEnumerable<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 asleepDurationProvider
.In the bellow example I've used this overload:
int retryCount
: At most how many retry attempts should be issuedFunc<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 aTimeSpan
in returnFunc<DelegateResult<TResult>, TimeSpan, int, Context, Task> onRetryAsync
: A user-defined function which is called if the policy should be triggered but before the sleepWith this you can achieve the desired behaviour:
onRetryAsync
delegate needs to be defined otherwise the compiler will not find this overload