Path: redb-route/redb.Route.Http.Hosting/ConcurrencyLimitOptions.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.Http.Hosting/ConcurrencyLimitOptions.cs
----
using System.Threading.RateLimiting;
namespace redb.Route.Http;
///
/// Per-registration admission limit for one route on the shared Kestrel host. Kestrel itself
/// accepts an unbounded number of concurrent requests; this caps how many of them may EXECUTE
/// a given route's handler at once, sheds the overflow with an immediate status reply
/// (load shedding, not backpressure — the industry norm: ASP.NET ConcurrencyLimiter policies,
/// nginx limit_conn, Envoy max_concurrent_requests), and leaves every other registration on the
/// same listener untouched.
///
/// Backed by : up to permits,
/// an optional FIFO wait queue of , everything beyond answered with
/// + Retry-After before any pipeline work happens.
///
///
public sealed class ConcurrencyLimitOptions
{
/// Maximum concurrent executions of the route handler. Must be at least 1.
public required int MaxConcurrentRequests { get; init; }
///
/// How many requests over the limit WAIT for a permit (FIFO) instead of being rejected.
/// 0 (default) = reject immediately once the permits are taken.
///
public int RequestQueueLimit { get; init; }
/// Status code for a shed request. Default 429 Too Many Requests.
public int RejectStatusCode { get; init; } = 429;
/// Value of the Retry-After header on a shed request; 0 = do not send it. Default 1.
public int RetryAfterSeconds { get; init; } = 1;
///
/// Invoked once per shed request, before the reject response is written. The hosting layer
/// deliberately knows nothing about redb.Route endpoint statistics — a connector passes
/// endpoint.RecordRejected here.
///
public Action? OnRejected { get; init; }
internal void Validate()
{
if (MaxConcurrentRequests < 1)
throw new ArgumentOutOfRangeException(nameof(MaxConcurrentRequests), MaxConcurrentRequests,
"maxConcurrentRequests must be at least 1 (omit the limit entirely for unlimited).");
if (RequestQueueLimit < 0)
throw new ArgumentOutOfRangeException(nameof(RequestQueueLimit), RequestQueueLimit,
"requestQueueLimit cannot be negative.");
if (RejectStatusCode is < 400 or > 599)
throw new ArgumentOutOfRangeException(nameof(RejectStatusCode), RejectStatusCode,
"rejectStatusCode must be a 4xx or 5xx status code.");
if (RetryAfterSeconds < 0)
throw new ArgumentOutOfRangeException(nameof(RetryAfterSeconds), RetryAfterSeconds,
"retryAfterSeconds cannot be negative (0 = do not send the header).");
}
internal ConcurrencyLimiter CreateLimiter() => new(new ConcurrencyLimiterOptions
{
PermitLimit = MaxConcurrentRequests,
QueueLimit = RequestQueueLimit,
QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
});
///
/// Validates the option quartet as it appears on an ENDPOINT, where 0 for
/// means "feature off" (unlike this class, whose
/// instances always describe an active limit). One validator for every connector that
/// exposes the options, so the rules cannot drift apart.
///
public static void ValidateShape(int maxConcurrentRequests, int requestQueueLimit,
int rejectStatusCode, int retryAfterSeconds)
{
if (maxConcurrentRequests < 0)
throw new ArgumentException("maxConcurrentRequests must be >= 0 (0 = unlimited).");
if (requestQueueLimit < 0)
throw new ArgumentException("requestQueueLimit must be >= 0.");
if (maxConcurrentRequests == 0 && requestQueueLimit > 0)
throw new ArgumentException(
"requestQueueLimit without maxConcurrentRequests is meaningless: there is no limit to queue behind. Set maxConcurrentRequests >= 1.");
if (rejectStatusCode is < 400 or > 599)
throw new ArgumentException("rejectStatusCode must be a 4xx or 5xx status code.");
if (retryAfterSeconds < 0)
throw new ArgumentException("retryAfterSeconds must be >= 0 (0 = do not send the header).");
}
///
/// Builds the registration-level limit from endpoint options, or null when the feature is off
/// ( = 0). is the
/// connector's statistics hook (endpoint.RecordRejected).
///
public static ConcurrencyLimitOptions? FromEndpoint(int maxConcurrentRequests, int requestQueueLimit,
int rejectStatusCode, int retryAfterSeconds, Action? onRejected)
=> maxConcurrentRequests <= 0
? null
: new ConcurrencyLimitOptions
{
MaxConcurrentRequests = maxConcurrentRequests,
RequestQueueLimit = requestQueueLimit,
RejectStatusCode = rejectStatusCode,
RetryAfterSeconds = retryAfterSeconds,
OnRejected = onRejected,
};
}