Path: redb-route/redb.Route/Definitions/ThreadsDefinition.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/ThreadsDefinition.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Processors; namespace redb.Route.Definitions; /// /// Scope-opener definition for the Threads EIP (Camel-style concurrency stage). /// Wraps the child pipeline () and runs it on a bounded pool /// of poolSize persistent workers via , so a serial polling /// consumer can process up to poolSize exchanges concurrently. /// Inherits the leaf DSL from ; close with . /// /// Adaptive by exchange pattern: InOnly is a fire-and-forget hand-off (the exchange is cloned and /// run on the worker pool — a transaction boundary, like .To("seda://")); InOut runs the /// body inline on the same exchange under a SemaphoreSlim gate (≤ poolSize concurrent), so /// the reply — on Out or In — is preserved losslessly and request/reply works across it /// (InOut is NOT a transaction boundary — the ambient transaction flows into the inline body). /// Ordering is not preserved when poolSize > 1. /// /// public class ThreadsDefinition : RouteDefinitionBase, IRouteScope { private readonly int _poolSize; private int _maxQueueSize; // 0 = default (== poolSize) private TimeSpan? _enqueueTimeout; // null = wait indefinitely for a free slot internal ThreadsDefinition(int poolSize) { if (poolSize < 1) throw new ArgumentOutOfRangeException(nameof(poolSize), poolSize, "poolSize must be at least 1."); _poolSize = poolSize; } // ── Options ──────────────────────────────────────────────────────────────── /// /// Sets the bounded hand-off queue capacity (backpressure). 0 = default (== pool size). /// A larger queue lets the poll loop run further ahead of the pool before it throttles. /// public ThreadsDefinition MaxQueueSize(int value) { if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), value, "maxQueueSize cannot be negative."); _maxQueueSize = value; return this; } /// /// Bounds how long a caller waits for a free worker slot when the pool + queue are saturated. /// Default (unset) = wait indefinitely (backpressure). When set and the window elapses, the enqueue /// fails with ; for an InOut/RPC exchange that becomes the reply fault /// (surfaced to the caller) instead of an unbounded wait. /// public ThreadsDefinition EnqueueTimeout(TimeSpan value) { if (value <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(value), value, "enqueueTimeout must be positive."); _enqueueTimeout = value; return this; } // ── Navigation ───────────────────────────────────────────────────────────── /// Closes this threads scope and returns the parent route definition. public IRouteDefinition EndThreads() => (IRouteDefinition)(Parent ?? throw new InvalidOperationException( "EndThreads() called without a parent route.")); /// public IRouteDefinition End() => EndThreads(); // ── IProcessorDefinition ─────────────────────────────────────────────────── /// public override IProcessor CreateProcessor(IRouteContext context) { IProcessor body = NodePipeline.Body(context, Outputs); // An in-only exchange runs on a pool worker after its caller has returned, outside the route's error handlers: // that body is wrapped by the route's whole error scope. In-out runs inline, inside them, as it is. var detachedBody = (context as RouteContext)?.DetachedErrorScope?.WrapDetached(body) ?? body; var logger = context.GetService()?.CreateLogger(); return new ThreadsProcessor(body, detachedBody, _poolSize, _maxQueueSize, context, logger) { EnqueueTimeout = _enqueueTimeout }; } }