Path: redb-route/redb.Route/Core/ConcurrencyOption.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/Core/ConcurrencyOption.cs
----
using System.Globalization;
namespace redb.Route.Core;
///
/// One resolver for every consumer-concurrency option that accepts auto. The industry default
/// for broker consumers is 1 — ordering is preserved and handlers need not be thread-safe (Camel
/// JMS/SQS, Spring, the Azure SDK all ship 1) — so the default stays 1 and parallelism is an explicit
/// opt-in: a number, or auto = max(CPU count, 2), the NServiceBus formula.
///
/// The option is a string because it accepts the word auto beside a number, which an int
/// property cannot hold. The string binds verbatim and this resolver fails loud on anything that is
/// neither a positive integer nor auto.
///
///
public static class ConcurrencyOption
{
/// The value auto resolves to: max(logical CPU count, 2).
public static int Auto => Math.Max(Environment.ProcessorCount, 2);
///
/// Resolves a raw option value: null/empty → , an integer ≥ 1 →
/// itself, auto (case-insensitive) → ; anything else throws, naming
/// the option and the valid values.
///
public static int Resolve(string? raw, string optionName, int defaultValue = 1)
{
if (string.IsNullOrWhiteSpace(raw)) return defaultValue;
var value = raw.Trim();
if (value.Equals("auto", StringComparison.OrdinalIgnoreCase))
return Auto;
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) && n >= 1)
return n;
throw new ArgumentException(
$"Invalid value '{raw}' for option '{optionName}'. " +
"Valid values: an integer >= 1, or 'auto' (= max(CPU count, 2)).");
}
}