问题描述
我正在尝试Polly CircuitBreakerAsync,但它没有按我预期的那样工作.
I'm just trying out the Polly CircuitBreakerAsync and it's not working as I expect.
我在这里做错了什么?我希望下面的代码能够完成,并说电路仍处于关闭状态.
What am I doing wrong here? I expect the code below to complete and say the circuit is still closed.
using Polly;
using System;
using System.Threading.Tasks;
public class Program
{
public static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(
3, // ConsecutiveExceptionsAllowedBeforeBreaking,
TimeSpan.FromSeconds(5) // DurationOfBreak
);
Console.WriteLine("Circuit state before execution: " + circuitBreaker.CircuitState);
await circuitBreaker.ExecuteAsync(() => Task.Delay(25));
await circuitBreaker.ExecuteAsync(() => Task.Delay(25));
await circuitBreaker.ExecuteAsync(() => { throw new System.Exception(); });
await circuitBreaker.ExecuteAsync(() => Task.Delay(25));
await circuitBreaker.ExecuteAsync(() => Task.Delay(25));
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
}
}
提琴: https://dotnetfiddle.net/unfKsC
输出:
Circuit state before execution: Closed
Run-time exception (line 25): Exception of type 'System.Exception' was thrown.
Stack Trace:
[System.Exception: Exception of type 'System.Exception' was thrown.]
at Program.<MainAsync>b__2() :line 25
at Polly.Policy.<>c__DisplayClass116_0.<ExecuteAsync>b__0(Context ctx, CancellationToken ct)
at Polly.CircuitBreakerSyntaxAsync.<>c__DisplayClass4_1.<<CircuitBreakerAsync>b__2>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.CircuitBreaker.CircuitBreakerEngine.<ImplementationAsync>d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.Policy.<ExecuteAsync>d__135.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Program.<MainAsync>d__a.MoveNext() :line 25
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Program.Main(String[] args) :line 9
推荐答案
这按预期工作
https://github.com/App-vNext/Polly/wiki/Circuit-Breaker
断路器是一种测量和断开装置:衡量您通过它进行的操作引发的已处理异常,以及超过配置的故障阈值时会中断.
A circuit-breaker exists as a measuring-and-breaking device: tomeasure handled exceptions thrown by actions you place through it, andto break when the configured failure threshold is exceeded.
- 断路器不会精心安排重试.
- 断路器不(不同于重试)吸收异常.通过该策略执行的操作引发的所有异常(两者都由政策处理的异常,而不是有意处理的)重新发射.由策略更新指标管理的异常电路状态该政策未处理的异常不会.
- A circuit-breaker does not orchestrate retries.
- A circuit-breaker does not (unlike retry) absorb exceptions. All exceptions thrown by actions executed through the policy (bothexceptions handled by the policy and not) are intentionallyrethrown. Exceptions handled by the policy update metrics governingcircuit state; exceptions not handled by the policy do not.
简而言之,它无法处理您的异常,而是将其抛出
In short it doesn't handle your exceptions, it rethrows them
这篇关于Polly CircuitBreakerAsync无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!