使用连接到重试策略的特定超时 [英] Use a specific timeout connected to a retrypolicy

查看:187
本文介绍了使用连接到重试策略的特定超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下方式创建重试策略:

I'm creating a retry policy the following way:

var policy = Policy.Handle<Exception>().WaitAndRetryAsync...

如何为上面的重试策略设置超时时间? Policy.TimeoutAsync返回TimeoutPolicy,因此我无法执行

How to chail/build a timeout for the retrypolicy above? Policy.TimeoutAsync returns a TimeoutPolicy, hence I'm not able to do something like

var policy = Policy.TimeoutAsync(30).Handle<Exception>().WaitAndRetryAsync....

超时是否成为我所有重试策略的常用设置?

Does the timeout become a common setting for all my retry policies?

推荐答案

要组合策略,请分别构建每个策略,然后使用

To combine policies, you build each policy separately, then combine them using PolicyWrap.

要建立一个整体超时,该超时适用于整个重试(即整个操作):

To build an overall timeout which applies across all retries as a whole (ie across the entire operation):

var overallTimeoutPolicy = Policy.TimeoutAsync(60); 
var waitAndRetryPolicy = Policy
    .Handle<WhateverException>()
    .WaitAndRetryAsync(/* your wait-and-retry specification*/);
var combinedPolicy = overallTimeoutPolicy.WrapAsync(waitAndRetryPolicy);

await combinedPolicy.ExecuteAsync(cancellationToken => ...)

要对每个特定的尝试强加超时,请以其他顺序包装重试和超时策略:

To impose a timeout on each specific try, wrap the retry and timeout policies in the other order:

var timeoutPerTry = Policy.TimeoutAsync(10); 
var waitAndRetryPolicy = Policy
    .Handle<WhateverException>()
    .WaitAndRetryAsync(/* your wait-and-retry specification*/);
var combinedPolicy = waitAndRetryPolicy.WrapAsync(timeoutPerTry);

await combinedPolicy.ExecuteAsync(cancellationToken => ...);

或者甚至使用两种超时(每次尝试,每次总体操作):

Or even use both kinds of timeout (per-try, per-overall-operation):

var overallTimeout = Policy.TimeoutAsync(60); 
var timeoutPerTry = Policy.TimeoutAsync(10); 
var waitAndRetryPolicy = Policy
    .Handle<WhateverException>()
    .WaitAndRetryAsync(/* your wait-and-retry specification*/);

var combinedPolicy = Policy
    .WrapAsync(overallTimeout, waitAndRetryPolicy, timeoutPerTry); // demonstrates alternative PolicyWrap syntax

await combinedPolicy.ExecuteAsync(cancellationToken => ...);

PolicyWrap Wiki 提供了完整的语法详细信息,而

The PolicyWrap wiki gives full syntax details, and advice on the effects of different orderings, when combining policies within a wrap.

要回答:

超时是否成为我所有重试策略的常用设置?

Does the timeout become a common setting for all my retry policies?

策略在您使用它们的任何地方(无论是单独使用还是作为PolicyWrap的一部分使用)都适用.

Policies apply wherever you use them (whether used individually, or as part of a PolicyWrap).

您可以线程安全使用您在多个呼叫站点上配置的任何TimeoutPolicy实例.因此,要将超时用作所有重试策略的通用设置,只需在每个呼叫站点的PolicyWrap中包括相同的TimeoutPolicy实例.如果需要,单个TimeoutPolicy实例可以用不同重试策略实例安全地包装.

You can thread-safely use any TimeoutPolicy instance you have configured at multiple call sites. So, to apply that timeout as a common setting for all your retry policies, simply include that same TimeoutPolicy instance in the PolicyWrap for each call site. The single TimeoutPolicy instance can safely be wrapped with different retry policy instances, if desired.

如果等待和重试规范和超时规范对于所有呼叫站点都是通用的,则只需创建一个包含这两个实例的PolicyWrap实例(按上述代码示例),然后在任何地方重用该PolicyWrap实例.再次-线程安全.

If both your wait-and-retry specification, and timeout specification, are common for all call sites, simply make one PolicyWrap instance encompassing both (per above code examples), and re-use that PolicyWrap instance everywhere. Again - thread safe.

这篇关于使用连接到重试策略的特定超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆