Path: redb-route/redb.Route/Definitions/CircuitBreakerDefinition.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/Definitions/CircuitBreakerDefinition.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Processors; namespace redb.Route.Definitions; /// /// Scope-opener definition for the CircuitBreaker EIP. /// Wraps the downstream pipeline with circuit-breaker protection. /// Optionally define a fallback branch with . /// Inherits the leaf DSL from ; /// child steps build the main downstream pipeline. /// Close with . /// public class CircuitBreakerDefinition : RouteDefinitionBase, IRouteScope, IBranchingDefinition { private int _failureThreshold = 5; private TimeSpan? _resetTimeout; private int _halfOpenMaxCalls = 1; internal FallbackDefinition? FallbackBlock; /// /// The fallback body — a logical child outside (which holds the main /// pipeline), exposed so a generic tree-walk (validation, AdviceWith) reaches steps nested in it. /// public IEnumerable Branches { get { if (FallbackBlock is not null) yield return FallbackBlock; } } internal CircuitBreakerDefinition() { } // ── Options ───────────────────────────────────────────────────────────────── /// Sets the number of consecutive failures before the circuit opens (default: 5). public CircuitBreakerDefinition FailureThreshold(int threshold) { if (threshold <= 0) throw new ArgumentOutOfRangeException(nameof(threshold)); _failureThreshold = threshold; return this; } /// Sets the time to wait in Open state before probing (default: 30 s). public CircuitBreakerDefinition ResetTimeout(TimeSpan timeout) { _resetTimeout = timeout; return this; } /// Sets the maximum probe calls allowed in HalfOpen state (default: 1). public CircuitBreakerDefinition HalfOpenMaxCalls(int count) { if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count)); _halfOpenMaxCalls = count; return this; } /// Alias for matching the Apache Camel DSL name. public CircuitBreakerDefinition Threshold(int threshold) => FailureThreshold(threshold); // ── Fallback scope ───────────────────────────────────────────────────────── /// /// Opens the fallback scope. Build fallback steps via leaf methods, /// then call or /// . /// /// Thrown if OnFallback was already called. public FallbackDefinition OnFallback() { if (FallbackBlock != null) throw new InvalidOperationException("OnFallback() can only be called once per CircuitBreaker scope."); FallbackBlock = new FallbackDefinition(this); return FallbackBlock; } /// /// Apache Camel nested-lambda style: opens the fallback scope, runs /// against it, and returns the parent so the chain may continue. /// public CircuitBreakerDefinition FallBack(Action configure) { ArgumentNullException.ThrowIfNull(configure); var fb = OnFallback(); configure(fb); return this; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes the CircuitBreaker scope and returns the parent route definition. public IRouteDefinition EndCircuitBreaker() => (IRouteDefinition)(Parent ?? throw new InvalidOperationException( "EndCircuitBreaker() called without a parent route.")); /// public IRouteDefinition End() => EndCircuitBreaker(); // ── IProcessorDefinition ─────────────────────────────────────────────────── /// public override IProcessor CreateProcessor(IRouteContext context) { IProcessor downstream = BuildPipeline(Outputs, context); IProcessor? fallback = FallbackBlock != null ? BuildPipeline(FallbackBlock.Outputs, context) : null; var loggerFactory = context.GetService(); var logger = loggerFactory?.CreateLogger(); return new CircuitBreakerProcessor(downstream, _failureThreshold, _resetTimeout, _halfOpenMaxCalls, fallback, logger); } internal static IProcessor BuildPipeline(IList outputs, IRouteContext context) => NodePipeline.Body(context, outputs); } /// /// The fallback branch inside a . /// Inherits the leaf DSL from ; child steps /// build the fallback pipeline executed when the circuit is open. /// Close with or . /// public class FallbackDefinition : RouteDefinitionBase, IRouteScope { private readonly CircuitBreakerDefinition _circuitBreaker; internal FallbackDefinition(CircuitBreakerDefinition circuitBreaker) { _circuitBreaker = circuitBreaker; Parent = circuitBreaker; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes the fallback branch and returns to the parent CircuitBreaker definition. public CircuitBreakerDefinition EndFallback() => _circuitBreaker; /// Closes the entire CircuitBreaker scope. public IRouteDefinition EndCircuitBreaker() => _circuitBreaker.EndCircuitBreaker(); /// public IRouteDefinition End() => EndCircuitBreaker(); /// public override IProcessor CreateProcessor(IRouteContext context) => throw new InvalidOperationException( "FallbackDefinition is compiled via its parent CircuitBreakerDefinition.CreateProcessor."); }