【发布时间】:2020-07-02 13:57:51
【问题描述】:
我有以下Retry 和Circuit Breaker 政策:
var waitAndRetryPolicy = Policy
.Handle<Exception>(e => e is MycustomException)
.WaitAndRetryForeverAsync(
attempt => TimeSpan.FromMilliseconds(500),
(exception, calculatedWaitDuration) =>
{
_logger.LogInfo(exception.GetType().FullName);
_logger.LogInfo(".Log,then retry: " + exception.Message);
});
var circuitBreakerPolicy = Policy
.Handle<Exception>()
.CircuitBreakerAsync(
4,
TimeSpan.FromSeconds(10),
(ex, breakDelay) =>
{
_logger.LogError(".Breaker logging: Breaking the circuit for " + breakDelay.TotalMilliseconds + "ms!");
},
() => { _logger.LogError(".Breaker logging: Call ok! Closed the circuit again!"); },
() => { _logger.LogError(".Breaker logging: Half-open: Next call is a trial!"); }
);
return waitAndRetryPolicy.WrapAsync(circuitBreakerPolicy);
如果我使用自定义异常,则在记录以下内容后重试失败:
- 断路器记录:断开电路 10000 毫秒!
如果我使用标准的Exception 类型,它可以正常工作。即使断路器打开,重试也会触发:
var waitAndRetryPolicy = Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(
attempt => TimeSpan.FromMilliseconds(500),
(exception, calculatedWaitDuration) =>
{
_logger.LogInfo(exception.GetType().FullName);
_logger.LogInfo(".Log,then retry: " + exception.Message);
});
【问题讨论】:
标签: c# polly resiliency