Path: redb-route/redb.Route/Processors/ThrottleRejection.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/ThrottleRejection.cs
----
using System.Globalization;
using System.Text;
using System.Text.Json;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Processors;
///
/// Applies an RFC 6585 §4 "429 Too Many Requests" short-circuit to an exchange. Shared
/// by and when their
/// rejectOnOverflow flag is set.
///
/// Writes a small OAuth-style JSON body (error / error_description /
/// retry_after), a Retry-After response header (RFC 7231 §7.1.3 delta-seconds
/// form), and the redbHttp.ResponseCode bridge header used by the redb.Route HTTP
/// consumer. The exchange is stopped via so no downstream
/// processor runs (no WireTap, no tx commit, no idempotency capture).
///
///
internal static class ThrottleRejection
{
/// Constant error code returned in the 429 body.
public const string ErrorCode = "rate_limit_exceeded";
///
/// Writes the 429 short-circuit on . The retry-after value
/// is the configured throttle period rounded up to whole seconds (per RFC 7231 §7.1.3
/// delta-seconds), clamped to a minimum of 1 second so the header is never absent or
/// degenerate when the period is sub-second.
///
public static void Apply(IExchange exchange, TimeSpan period)
{
var retryAfter = (int)Math.Max(1, Math.Ceiling(period.TotalSeconds));
var retryAfterStr = retryAfter.ToString(CultureInfo.InvariantCulture);
var body = JsonSerializer.SerializeToUtf8Bytes(new Dictionary
{
["error"] = ErrorCode,
["error_description"] = $"Rate limit exceeded. Retry after {retryAfterStr} second(s).",
["retry_after"] = retryAfter,
});
var msg = new Message(body)
{
ContentType = "application/json",
};
msg.Headers["redbHttp.ResponseCode"] = 429;
msg.Headers["redbHttp.ResponseContentType"] = "application/json";
msg.Headers["Retry-After"] = retryAfterStr;
exchange.Out = msg;
exchange.ExceptionHandled = true;
exchange.Stop();
}
}