<PackageReference Include="Polly" Version="4.2.4" />

ConsecutiveCountCircuitController

using Polly.Utilities; using System; namespace Polly.CircuitBreaker { internal class ConsecutiveCountCircuitController : CircuitStateController { private readonly int _exceptionsAllowedBeforeBreaking; private int _count; public ConsecutiveCountCircuitController(int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen) : base(durationOfBreak, onBreak, onReset, onHalfOpen) { _exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking; } public override void OnCircuitReset(Context context) { using (TimedLock.Lock(_lock)) { _count = 0; ResetInternal_NeedsLock(context); } } public override void OnActionSuccess(Context context) { using (TimedLock.Lock(_lock)) { if (_circuitState == CircuitState.HalfOpen) OnCircuitReset(context); } } public override void OnActionFailure(Exception ex, Context context) { using (TimedLock.Lock(_lock)) { _lastException = ex; if (_circuitState == CircuitState.HalfOpen) Break_NeedsLock(context); else { _count++; if (_count >= _exceptionsAllowedBeforeBreaking) Break_NeedsLock(context); } } } } }