Polly CircuitBreakerAsync 没有按我的预期工作

Chr*_*ris 4 c# task-parallel-library circuit-breaker async-await polly

我只是尝试 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

小提琴: 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
Run Code Online (Sandbox Code Playgroud)

Pet*_*ala 11

一般断路器

Your code works as expected. The circuit breaker itself will not break because you have set the consecutive error count to 3. It means that if you have 3 successive failed calls then it will transition from Closed state to Open. If you try to execute yet another call then it will throw a BrokenCircuitException. In Closed state if an exception has been thrown and the threshold has not been reached then it re-throws the exception.

I always suggest to consider the Circuit Breaker as a proxy. It allows calls if everything works fine. If the consumed sub-system / sub-component seems like malfunctioning then it will prevent further calls to avoid unnecessary load.

Callback functions for debugging

When you define a Circuit Breaker policy then you can specify 3 callbacks:

  • onBreak: When it transitions from Closed or HalfOpen to Open
  • onReset: When it transitions from HalfOpen to Close
  • onHalfOpen: When it transitions from Open to HalfOpen

The amended policy declaration:

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}")
    );
Run Code Online (Sandbox Code Playgroud)

Successive failure count

Let's change the consecutive fail threshold to 1 and let's wrap your ExecuteAsync calls in a 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}")
    );
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)

The output will be the following:

Circuit state before execution: Closed
Break     05.000    : Exception
Circuit state after execution: Open
Exception
Run Code Online (Sandbox Code Playgroud)

As you can see the Circuit Breaker has broken and go from Closed to Open state. It has rethrown your exception.

Combine Retry and Circuit Breaker

In order to easily demonstrate when the CB throws BrokenCircuitException I will use a Retry logic around the CB.

Circuit state before execution: Closed
Break     05.000    : Exception
Circuit state after execution: Open
Exception
Run Code Online (Sandbox Code Playgroud)

This policy will try to re-execute your delegate either when an Exception or when a BrokenCircuitException has been thrown. It does that with 1 second delay between the initial attempt and the first (and only) retry.

Let's combine the two policies and let's amend the ExecuteAsync call:

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}");
        });
Run Code Online (Sandbox Code Playgroud)

The output will be the following:

Circuit state before execution: Closed
Break     05.000    : Exception
Retry     01.000    : Exception
Circuit state after execution: Open
BrokenCircuitException
Run Code Online (Sandbox Code Playgroud)
  1. The initial call fails and it throws an Exception
  2. CB breaks because the threshold has been reached and it re-throws the exception
  3. The combined policy will escalate the problem from the CB to the Retry
  4. Retry handles Exception that's why it waits a second before it tries to re-execute the delegate again
  5. Retry tries to call the delegate again but it fails because the CB is Open that's why a BrokenCircuitException is thrown
  6. Because there is no further retry that's why the retry policy will re-throw its exception (which is now a BrokenCircuitException instance)
  7. That exception is catched by our catch block.

Fine-tuned example

Let's amend the parameters of these policies a bit:

  • CB's durationOfBreak from 5 seconds to 1.5
  • Retry's retryCount from 1 to 2
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);
}
Run Code Online (Sandbox Code Playgroud)

The output will be the following:

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
Run Code Online (Sandbox Code Playgroud)

I hope this little demo app helped you to better understand how does Circuit Breaker work.