Path: redb-route/redb.Route/Definitions/ThrottleDebounceDefinition.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/ThrottleDebounceDefinition.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Processors; namespace redb.Route.Definitions; /// /// Scope-opener definition for the Throttle EIP. /// Limits the rate at which the downstream pipeline processes exchanges. /// Inherits the leaf DSL from ; /// child steps build the downstream pipeline. /// Close with . /// public class ThrottleDefinition : RouteDefinitionBase, IRouteScope { private readonly Func _maxPerPeriod; private TimeSpan? _period; private bool _rejectOnOverflow; internal ThrottleDefinition(int maxPerPeriod) : this(_ => maxPerPeriod) { if (maxPerPeriod <= 0) throw new ArgumentOutOfRangeException(nameof(maxPerPeriod), "Must be > 0."); } /// Creates a throttle whose limit is computed per exchange by . internal ThrottleDefinition(Func maxPerPeriodFactory) { _maxPerPeriod = maxPerPeriodFactory ?? throw new ArgumentNullException(nameof(maxPerPeriodFactory)); } // ── Options ───────────────────────────────────────────────────────────────── /// Sets the time period for the rate limit (default: 1 second). public ThrottleDefinition Period(TimeSpan period) { _period = period; return this; } /// /// Opt into RFC 6585 §4 rejection semantics: overflow exchanges are short-circuited with /// HTTP 429 + Retry-After (RFC 7231 §7.1.3) instead of semaphore-waiting until a /// slot frees. Recommended for any HTTP-facing endpoint — silent waits look like a hung /// server to clients and break liveness probes. Default is false (legacy wait). /// public ThrottleDefinition RejectOnOverflow(bool reject = true) { _rejectOnOverflow = reject; return this; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes the Throttle scope and returns the parent route definition. public IRouteDefinition EndThrottle() => (IRouteDefinition)(Parent ?? throw new InvalidOperationException( "EndThrottle() called without a parent route.")); /// public IRouteDefinition End() => EndThrottle(); // ── IProcessorDefinition ─────────────────────────────────────────────────── /// public override IProcessor CreateProcessor(IRouteContext context) { IProcessor downstream = BuildPipeline(Outputs, context); var loggerFactory = context.GetService(); var logger = loggerFactory?.CreateLogger(); return new ThrottleProcessor(downstream, _maxPerPeriod, _period, _rejectOnOverflow, logger); } private static IProcessor BuildPipeline(IList outputs, IRouteContext context) => NodePipeline.Body(context, outputs); } /// /// Scope-opener definition for the Debounce EIP. /// Suppresses rapid-fire exchanges per key, forwarding only the last exchange after /// a configurable quiet period. /// Inherits the leaf DSL from ; /// child steps build the downstream pipeline. /// Close with . /// public class DebounceDefinition : RouteDefinitionBase, IRouteScope { private readonly Func _keyExtractor; private readonly TimeSpan _quietPeriod; /// Gets the key extractor function used to correlate exchanges. public Func KeyExtractor => _keyExtractor; /// Gets the quiet period after which the last buffered exchange is forwarded. public TimeSpan QuietPeriod => _quietPeriod; internal DebounceDefinition(Func keyExtractor, TimeSpan quietPeriod) { _keyExtractor = keyExtractor ?? throw new ArgumentNullException(nameof(keyExtractor)); if (quietPeriod <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(quietPeriod), "Must be positive."); _quietPeriod = quietPeriod; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes the Debounce scope and returns the parent route definition. public IRouteDefinition EndDebounce() => (IRouteDefinition)(Parent ?? throw new InvalidOperationException( "EndDebounce() called without a parent route.")); /// public IRouteDefinition End() => EndDebounce(); // ── IProcessorDefinition ─────────────────────────────────────────────────── /// public override IProcessor CreateProcessor(IRouteContext context) { IProcessor downstream = BuildPipeline(Outputs, context); // The body only ever runs on a flush, after the exchange that fed it has left the route's error wrappers: it is // wrapped by the route's error scope (its OnException, its builder's, the context's handlers). downstream = (context as Core.RouteContext)?.DetachedErrorScope?.WrapDetached(downstream) ?? downstream; var loggerFactory = context.GetService(); var logger = loggerFactory?.CreateLogger(); return new DebounceProcessor(downstream, _keyExtractor, _quietPeriod, logger); } private static IProcessor BuildPipeline(IList outputs, IRouteContext context) => NodePipeline.Body(context, outputs); } /// /// Scope-opener definition for the keyed (per-key) Throttle EIP. /// Limits the rate at which each unique key can enter the downstream pipeline. /// Inherits the leaf DSL from . /// Close with . /// public class KeyedThrottleDefinition : RouteDefinitionBase, IRouteScope { private readonly Func _keyExtractor; private readonly int _maxPerPeriod; private readonly TimeSpan? _period; private bool _rejectOnOverflow; /// Gets the key extractor function. public Func KeyExtractor => _keyExtractor; /// Gets the max exchanges per period per key. public int MaxPerPeriod => _maxPerPeriod; /// Gets the time period for the rate limit (null = 1 second default). public TimeSpan? Period => _period; internal KeyedThrottleDefinition(Func keyExtractor, int maxPerPeriod, TimeSpan? period) { _keyExtractor = keyExtractor ?? throw new ArgumentNullException(nameof(keyExtractor)); if (maxPerPeriod <= 0) throw new ArgumentOutOfRangeException(nameof(maxPerPeriod), "Must be > 0."); _maxPerPeriod = maxPerPeriod; MaxPerPeriodFactory = _ => maxPerPeriod; _period = period; } internal KeyedThrottleDefinition(Func keyExtractor, Func maxPerPeriodFactory, TimeSpan? period) { _keyExtractor = keyExtractor ?? throw new ArgumentNullException(nameof(keyExtractor)); MaxPerPeriodFactory = maxPerPeriodFactory ?? throw new ArgumentNullException(nameof(maxPerPeriodFactory)); _maxPerPeriod = 0; // dynamic: read per message _period = period; } /// /// Limit per key evaluated on each message (key and limit come from the message: /// "gold customers 100 per second, others 10, each under its own key"). The fixed int /// form is the constant factory; is 0 when the limit is dynamic. /// public Func MaxPerPeriodFactory { get; } // ── Options ───────────────────────────────────────────────────────────────── /// /// Opt into RFC 6585 §4 rejection semantics: overflow exchanges are short-circuited with /// HTTP 429 + Retry-After (RFC 7231 §7.1.3) instead of semaphore-waiting until a /// slot frees. Recommended for any HTTP-facing endpoint — silent waits look like a hung /// server to clients and break liveness probes. Default is false (legacy wait). /// public KeyedThrottleDefinition RejectOnOverflow(bool reject = true) { _rejectOnOverflow = reject; return this; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes the KeyedThrottle scope and returns the parent route definition. public IRouteDefinition EndKeyedThrottle() => (IRouteDefinition)(Parent ?? throw new InvalidOperationException( "EndKeyedThrottle() called without a parent route.")); /// public IRouteDefinition End() => EndKeyedThrottle(); // ── IProcessorDefinition ─────────────────────────────────────────────────── /// public override IProcessor CreateProcessor(IRouteContext context) { IProcessor downstream = BuildPipeline(Outputs, context); var loggerFactory = context.GetService(); var logger = loggerFactory?.CreateLogger(); return new KeyedThrottleProcessor(downstream, _keyExtractor, MaxPerPeriodFactory, _period, _rejectOnOverflow, logger); } private static IProcessor BuildPipeline(IList outputs, IRouteContext context) => NodePipeline.Body(context, outputs); }