Path: redb-route/redb.Route/Batching/BatchOptions.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/Batching/BatchOptions.cs
----
namespace redb.Route.Batching;
///
/// How a batch consumer collects a batch, one contract for every broker, with Apache Camel's names: a batch is sent on
/// once it holds messages, once no message came for (the idle
/// timeout, restarted by every message), or once has passed since its first message (the hard
/// limit), whichever comes first.
///
public sealed record BatchOptions
{
/// Endpoint option that switches batch mode on; no other option does.
public const string BatchingOption = "batching";
/// Endpoint option for .
public const string MaxBatchMessagesOption = "maxBatchMessages";
/// Endpoint option for , in milliseconds.
public const string PollTimeoutMsOption = "pollTimeoutMs";
/// Endpoint option for , in milliseconds.
public const string BatchingIntervalMsOption = "batchingIntervalMs";
/// Largest number of messages in one batch.
public int MaxMessages { get; }
/// A batch with messages is sent on when no further message came for this long.
public TimeSpan PollTimeout { get; }
/// A batch is sent on when this much time has passed since its first message; null: no limit.
public TimeSpan? Interval { get; }
/// Creates batch options; values out of range throw.
public BatchOptions(int maxMessages, TimeSpan pollTimeout, TimeSpan? interval = null)
{
if (Problem(maxMessages, pollTimeout, interval) is { } problem)
throw new ArgumentOutOfRangeException(null, problem);
MaxMessages = maxMessages;
PollTimeout = pollTimeout;
Interval = interval;
}
///
/// The batch options of an endpoint from its option values, or null when batch mode is off. A batch option
/// set without batching=true means nothing, so it fails the start, naming the endpoint, as does a value out of
/// range.
///
/// The endpoint, for the message (sanitized by the caller).
/// The batching option.
/// The maxBatchMessages option, null when not set.
/// The pollTimeoutMs option of the connector, its default applied.
/// The batchingIntervalMs option, null when not set.
/// The connector's default for maxBatchMessages.
public static BatchOptions? Resolve(string endpoint, bool batching, int? maxBatchMessages, int pollTimeoutMs,
int? batchingIntervalMs, int defaultMaxBatchMessages)
{
if (!batching)
{
var stray = maxBatchMessages is not null ? MaxBatchMessagesOption
: batchingIntervalMs is not null ? BatchingIntervalMsOption
: null;
if (stray is not null)
throw new InvalidOperationException(
$"Endpoint '{endpoint}' sets {stray} without {BatchingOption}=true: a batch option without batch mode " +
$"means nothing. Add {BatchingOption}=true, or remove {stray}.");
return null;
}
var max = maxBatchMessages ?? defaultMaxBatchMessages;
var poll = TimeSpan.FromMilliseconds(pollTimeoutMs);
TimeSpan? interval = batchingIntervalMs is { } ms ? TimeSpan.FromMilliseconds(ms) : null;
if (Problem(max, poll, interval) is { } problem)
throw new InvalidOperationException($"Endpoint '{endpoint}': {problem}");
return new BatchOptions(max, poll, interval);
}
private static string? Problem(int maxMessages, TimeSpan pollTimeout, TimeSpan? interval) =>
maxMessages < 1 ? $"{MaxBatchMessagesOption} must be at least 1, not {maxMessages}."
: pollTimeout <= TimeSpan.Zero ? $"{PollTimeoutMsOption} must be positive, not {pollTimeout.TotalMilliseconds}."
: interval is { } i && i <= TimeSpan.Zero ? $"{BatchingIntervalMsOption} must be positive, not {i.TotalMilliseconds}."
: null;
}