Path: redb-route/redb.Route/Processors/CircuitBreakerProcessor.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Processors/CircuitBreakerProcessor.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Telemetry;
namespace redb.Route.Processors;
///
/// Circuit breaker states following the standard pattern.
///
public enum CircuitState
{
/// Normal operation — requests pass through.
Closed,
/// Circuit is tripped — requests go to fallback.
Open,
/// Probing state — limited requests allowed to test recovery.
HalfOpen
}
///
/// Implements the Circuit Breaker pattern. When consecutive failures exceed a threshold,
/// the circuit "opens" and routes exchanges to a fallback processor. After a reset timeout,
/// the circuit enters a half-open state allowing limited probing calls.
/// All state transitions happen under a single lock to avoid TOCTOU races.
/// Thread-safe for concurrent pipeline usage.
///
public sealed class CircuitBreakerProcessor : IProcessor
{
private readonly IProcessor _next;
private readonly IProcessor? _fallback;
private readonly int _failureThreshold;
private readonly TimeSpan _resetTimeout;
private readonly int _halfOpenMaxCalls;
private readonly ILogger? _logger;
private readonly object _lock = new();
private CircuitState _state = CircuitState.Closed;
private int _consecutiveFailures;
private int _halfOpenCallCount;
private int _halfOpenSuccessCount;
private DateTimeOffset _openedAt;
/// Gets the current circuit state.
public CircuitState State
{
get { lock (_lock) return GetEffectiveStateLocked(); }
}
/// Creates a circuit breaker processor.
/// The protected processor.
/// Number of consecutive failures before opening.
/// Time to wait before transitioning from Open to HalfOpen.
/// Max probe calls allowed in HalfOpen state.
/// Optional fallback processor when circuit is open.
/// Optional logger.
public CircuitBreakerProcessor(
IProcessor next,
int failureThreshold = 5,
TimeSpan? resetTimeout = null,
int halfOpenMaxCalls = 1,
IProcessor? fallback = null,
ILogger? logger = null)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
if (failureThreshold <= 0) throw new ArgumentOutOfRangeException(nameof(failureThreshold));
_failureThreshold = failureThreshold;
_resetTimeout = resetTimeout ?? TimeSpan.FromSeconds(30);
_halfOpenMaxCalls = halfOpenMaxCalls > 0 ? halfOpenMaxCalls : 1;
_fallback = fallback;
_logger = logger;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
// Acquire state atomically — decide whether to let this call through
CircuitState stateForThisCall;
lock (_lock)
{
stateForThisCall = GetEffectiveStateLocked();
if (stateForThisCall == CircuitState.Open)
{
// Don't release lock with pending work — just flag and handle below
}
else if (stateForThisCall == CircuitState.HalfOpen)
{
if (_halfOpenCallCount >= _halfOpenMaxCalls)
{
// Probe limit reached — treat as open
stateForThisCall = CircuitState.Open;
}
else
{
_halfOpenCallCount++;
}
}
}
if (stateForThisCall == CircuitState.Open)
{
ProcessorMetrics.CircuitBreakerRejected.Add(1);
if (_fallback != null)
{
await _fallback.Process(exchange, ct).ConfigureAwait(false);
return;
}
exchange.Exception = new CircuitBreakerOpenException(
$"Circuit breaker is open. Will retry after {_resetTimeout.TotalSeconds}s.");
return;
}
// stateForThisCall is Closed or HalfOpen — execute protected processor
try
{
await _next.Process(exchange, ct).ConfigureAwait(false);
if (exchange.Exception != null)
{
OnFailure();
}
else
{
OnSuccess(stateForThisCall);
}
}
catch (Exception ex)
{
OnFailure();
exchange.Exception = ex;
}
}
///
/// Evaluates the effective state. Must be called under .
/// Handles the time-based Open → HalfOpen transition.
///
private CircuitState GetEffectiveStateLocked()
{
if (_state == CircuitState.Open && DateTimeOffset.UtcNow - _openedAt >= _resetTimeout)
{
_state = CircuitState.HalfOpen;
_halfOpenCallCount = 0;
_halfOpenSuccessCount = 0;
}
return _state;
}
private void OnSuccess(CircuitState stateAtEntry)
{
lock (_lock)
{
_consecutiveFailures = 0;
if (stateAtEntry == CircuitState.HalfOpen)
{
_halfOpenSuccessCount++;
// Close only when ALL probe calls succeeded
if (_halfOpenSuccessCount >= _halfOpenMaxCalls)
{
_state = CircuitState.Closed;
}
}
else
{
// Closed state — just reset failures (already done above)
}
}
}
private void OnFailure()
{
lock (_lock)
{
_consecutiveFailures++;
if (_state == CircuitState.HalfOpen)
{
// Any failure in HalfOpen → Open immediately
_state = CircuitState.Open;
_openedAt = DateTimeOffset.UtcNow;
ProcessorMetrics.CircuitBreakerTripped.Add(1);
_logger?.LogWarning("Circuit breaker tripped to Open (HalfOpen probe failed).");
}
else if (_consecutiveFailures >= _failureThreshold)
{
_state = CircuitState.Open;
_openedAt = DateTimeOffset.UtcNow;
ProcessorMetrics.CircuitBreakerTripped.Add(1);
_logger?.LogWarning("Circuit breaker tripped to Open after {Failures} consecutive failures.", _consecutiveFailures);
}
}
}
}
///
/// Exception thrown when the circuit breaker is in Open state and no fallback is configured.
///
public sealed class CircuitBreakerOpenException : Exception
{
/// Creates a new circuit breaker open exception.
/// Exception message.
public CircuitBreakerOpenException(string message) : base(message) { }
/// Creates a new circuit breaker open exception with inner exception.
/// Exception message.
/// Inner exception.
public CircuitBreakerOpenException(string message, Exception inner) : base(message, inner) { }
}