Polly CircuitBreakerAsync 未按预期工作
Polly CircuitBreakerAsync is not working as I expect
我只是在试用 Polly CircuitBreakerAsync,它没有像我预期的那样工作。
我在这里做错了什么?我希望下面的代码能够完成并表示电路仍然关闭。
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);
}
}
Fiddle: 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
Exception handling
A circuit-breaker exists as a measuring-and-breaking device: to
measure handled exceptions thrown by actions you place through it, and
to 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 (both
exceptions handled by the policy and not) are intentionally
rethrown. Exceptions handled by the policy update metrics governing
circuit state; exceptions not handled by the policy do not.
简而言之,它不处理您的异常,而是重新抛出它们
一般断路器
您的代码按预期工作。断路器本身不会中断,因为您已将连续错误计数设置为 3。这意味着如果您有 3 次连续失败的调用,那么它将从 Closed
状态转换为 Open
。如果您尝试执行另一个调用,那么它将抛出 BrokenCircuitException
。在 Closed
状态下,如果已抛出异常且未达到阈值,则它会重新抛出异常。
我总是建议将断路器视为代理。如果一切正常,它允许调用。如果消耗的子系统/子组件似乎出现故障,那么它将阻止进一步调用以避免不必要的负载。
用于调试的回调函数
定义断路器策略时,您可以指定 3 个回调:
onBreak
:当它从Closed
或HalfOpen
过渡到Open
时
onReset
:当它从HalfOpen
过渡到Close
onHalfOpen
:当它从Open
过渡到HalfOpen
修改后的政策声明:
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(3, TimeSpan.FromSeconds(5),
onBreak: (ex, @break) => Console.WriteLine($"{"Break",-10}{@break,-10:ss\.fff}: {ex.GetType().Name}"),
onReset: () => Console.WriteLine($"{"Reset",-10}"),
onHalfOpen: () => Console.WriteLine($"{"HalfOpen",-10}")
);
连续失败次数
让我们将连续失败阈值更改为 1,让我们将您的 ExecuteAsync
调用包装在 try catch 中:
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(1, TimeSpan.FromSeconds(5),
onBreak: (ex, @break) => Console.WriteLine($"{"Break",-10}{@break,-10:ss\.fff}: {ex.GetType().Name}"),
onReset: () => Console.WriteLine($"{"Reset",-10}"),
onHalfOpen: () => Console.WriteLine($"{"HalfOpen",-10}")
);
Console.WriteLine("Circuit state before execution: " + circuitBreaker.CircuitState);
try
{
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));
}
catch (Exception ex)
{
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
Console.WriteLine(ex.GetType().Name);
}
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
输出如下:
Circuit state before execution: Closed
Break 05.000 : Exception
Circuit state after execution: Open
Exception
如您所见,断路器已断开并从 Closed
状态变为 Open
状态。它已重新抛出您的异常。
结合重试和断路器
为了轻松演示 CB 何时抛出 BrokenCircuitException
我将在 CB 周围使用重试逻辑。
var retry = Policy
.Handle<Exception>()
.Or<BrokenCircuitException>()
.WaitAndRetryAsync(
retryCount: 1,
sleepDurationProvider: _ => TimeSpan.FromSeconds(1),
onRetry: (exception, delay, context) =>
{
Console.WriteLine($"{"Retry",-10}{delay,-10:ss\.fff}: {exception.GetType().Name}");
});
此策略将尝试在 Exception
或 BrokenCircuitException
被抛出时重新执行您的委托。它在初始尝试和第一次(也是唯一一次)重试之间有 1 秒的延迟。
让我们合并这两个策略,然后修改 ExecuteAsync
调用:
var strategy = Policy.WrapAsync(retry, circuitBreaker);
try
{
await strategy.ExecuteAsync(() => { throw new System.Exception(); });
}
catch (Exception ex)
{
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
Console.WriteLine(ex.GetType().Name);
}
输出如下:
Circuit state before execution: Closed
Break 05.000 : Exception
Retry 01.000 : Exception
Circuit state after execution: Open
BrokenCircuitException
- 初始调用失败并抛出
Exception
- CB 因达到阈值而中断并重新抛出异常
- 组合策略会将问题从 CB 升级到重试
- 重试句柄
Exception
这就是它在尝试再次重新执行委托之前等待一秒钟的原因
- Retry 尝试再次调用委托但它失败了,因为 CB 是
Open
这就是抛出 BrokenCircuitException
的原因
- 因为没有进一步的重试,所以重试策略将重新抛出其异常(现在是
BrokenCircuitException
实例)
- 该异常被我们的
catch
块捕获。
微调示例
让我们稍微修改一下这些策略的参数:
- CB 的
durationOfBreak
从 5 秒到 1.5
- 重试的
retryCount
从 1 到 2
var retry = Policy
.Handle<Exception>()
.Or<BrokenCircuitException>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1),
onRetry: (exception, delay, context) =>
{
Console.WriteLine($"{"Retry",-10}{delay,-10:ss\.fff}: {exception.GetType().Name}");
});
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(1500),
onBreak: (ex, @break) => Console.WriteLine($"{"Break",-10}{@break,-10:ss\.fff}: {ex.GetType().Name}"),
onReset: () => Console.WriteLine($"{"Reset",-10}"),
onHalfOpen: () => Console.WriteLine($"{"HalfOpen",-10}")
);
Console.WriteLine("Circuit state before execution: " + circuitBreaker.CircuitState);
var strategy = Policy.WrapAsync(retry, circuitBreaker);
try
{
await strategy.ExecuteAsync(() => { throw new System.Exception(); });
}
catch (Exception ex)
{
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
Console.WriteLine(ex.GetType().Name);
}
输出如下:
Circuit state before execution: Closed
Break 01.500 : Exception
Retry 01.000 : Exception
Retry 01.000 : BrokenCircuitException
HalfOpen
Break 01.500 : Exception
Circuit state after execution: Open
Exception
我希望这个小演示应用程序能帮助您更好地了解断路器的工作原理。
我只是在试用 Polly CircuitBreakerAsync,它没有像我预期的那样工作。
我在这里做错了什么?我希望下面的代码能够完成并表示电路仍然关闭。
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);
}
}
Fiddle: 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
Exception handling
A circuit-breaker exists as a measuring-and-breaking device: to measure handled exceptions thrown by actions you place through it, and to 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 (both exceptions handled by the policy and not) are intentionally rethrown. Exceptions handled by the policy update metrics governing circuit state; exceptions not handled by the policy do not.
简而言之,它不处理您的异常,而是重新抛出它们
一般断路器
您的代码按预期工作。断路器本身不会中断,因为您已将连续错误计数设置为 3。这意味着如果您有 3 次连续失败的调用,那么它将从 Closed
状态转换为 Open
。如果您尝试执行另一个调用,那么它将抛出 BrokenCircuitException
。在 Closed
状态下,如果已抛出异常且未达到阈值,则它会重新抛出异常。
我总是建议将断路器视为代理。如果一切正常,它允许调用。如果消耗的子系统/子组件似乎出现故障,那么它将阻止进一步调用以避免不必要的负载。
用于调试的回调函数
定义断路器策略时,您可以指定 3 个回调:
onBreak
:当它从Closed
或HalfOpen
过渡到Open
时
onReset
:当它从HalfOpen
过渡到Close
onHalfOpen
:当它从Open
过渡到HalfOpen
修改后的政策声明:
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(3, TimeSpan.FromSeconds(5),
onBreak: (ex, @break) => Console.WriteLine($"{"Break",-10}{@break,-10:ss\.fff}: {ex.GetType().Name}"),
onReset: () => Console.WriteLine($"{"Reset",-10}"),
onHalfOpen: () => Console.WriteLine($"{"HalfOpen",-10}")
);
连续失败次数
让我们将连续失败阈值更改为 1,让我们将您的 ExecuteAsync
调用包装在 try catch 中:
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(1, TimeSpan.FromSeconds(5),
onBreak: (ex, @break) => Console.WriteLine($"{"Break",-10}{@break,-10:ss\.fff}: {ex.GetType().Name}"),
onReset: () => Console.WriteLine($"{"Reset",-10}"),
onHalfOpen: () => Console.WriteLine($"{"HalfOpen",-10}")
);
Console.WriteLine("Circuit state before execution: " + circuitBreaker.CircuitState);
try
{
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));
}
catch (Exception ex)
{
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
Console.WriteLine(ex.GetType().Name);
}
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
输出如下:
Circuit state before execution: Closed
Break 05.000 : Exception
Circuit state after execution: Open
Exception
如您所见,断路器已断开并从 Closed
状态变为 Open
状态。它已重新抛出您的异常。
结合重试和断路器
为了轻松演示 CB 何时抛出 BrokenCircuitException
我将在 CB 周围使用重试逻辑。
var retry = Policy
.Handle<Exception>()
.Or<BrokenCircuitException>()
.WaitAndRetryAsync(
retryCount: 1,
sleepDurationProvider: _ => TimeSpan.FromSeconds(1),
onRetry: (exception, delay, context) =>
{
Console.WriteLine($"{"Retry",-10}{delay,-10:ss\.fff}: {exception.GetType().Name}");
});
此策略将尝试在 Exception
或 BrokenCircuitException
被抛出时重新执行您的委托。它在初始尝试和第一次(也是唯一一次)重试之间有 1 秒的延迟。
让我们合并这两个策略,然后修改 ExecuteAsync
调用:
var strategy = Policy.WrapAsync(retry, circuitBreaker);
try
{
await strategy.ExecuteAsync(() => { throw new System.Exception(); });
}
catch (Exception ex)
{
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
Console.WriteLine(ex.GetType().Name);
}
输出如下:
Circuit state before execution: Closed
Break 05.000 : Exception
Retry 01.000 : Exception
Circuit state after execution: Open
BrokenCircuitException
- 初始调用失败并抛出
Exception
- CB 因达到阈值而中断并重新抛出异常
- 组合策略会将问题从 CB 升级到重试
- 重试句柄
Exception
这就是它在尝试再次重新执行委托之前等待一秒钟的原因 - Retry 尝试再次调用委托但它失败了,因为 CB 是
Open
这就是抛出BrokenCircuitException
的原因 - 因为没有进一步的重试,所以重试策略将重新抛出其异常(现在是
BrokenCircuitException
实例) - 该异常被我们的
catch
块捕获。
微调示例
让我们稍微修改一下这些策略的参数:
- CB 的
durationOfBreak
从 5 秒到 1.5 - 重试的
retryCount
从 1 到 2
var retry = Policy
.Handle<Exception>()
.Or<BrokenCircuitException>()
.WaitAndRetryAsync(2, _ => TimeSpan.FromSeconds(1),
onRetry: (exception, delay, context) =>
{
Console.WriteLine($"{"Retry",-10}{delay,-10:ss\.fff}: {exception.GetType().Name}");
});
var circuitBreaker = Policy
.Handle<Exception>()
.CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(1500),
onBreak: (ex, @break) => Console.WriteLine($"{"Break",-10}{@break,-10:ss\.fff}: {ex.GetType().Name}"),
onReset: () => Console.WriteLine($"{"Reset",-10}"),
onHalfOpen: () => Console.WriteLine($"{"HalfOpen",-10}")
);
Console.WriteLine("Circuit state before execution: " + circuitBreaker.CircuitState);
var strategy = Policy.WrapAsync(retry, circuitBreaker);
try
{
await strategy.ExecuteAsync(() => { throw new System.Exception(); });
}
catch (Exception ex)
{
Console.WriteLine("Circuit state after execution: " + circuitBreaker.CircuitState);
Console.WriteLine(ex.GetType().Name);
}
输出如下:
Circuit state before execution: Closed
Break 01.500 : Exception
Retry 01.000 : Exception
Retry 01.000 : BrokenCircuitException
HalfOpen
Break 01.500 : Exception
Circuit state after execution: Open
Exception
我希望这个小演示应用程序能帮助您更好地了解断路器的工作原理。