Path: redb-route/redb.Route/Definitions/StopDelayDefinition.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/StopDelayDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Leaf definition that stops exchange processing (sets exchange.Stop()).
///
public sealed class StopDefinition : ProcessorDefinition
{
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange => exchange.Stop());
}
///
/// Leaf definition that delays processing by a fixed .
///
public sealed class DelayDefinition : ProcessorDefinition
{
private readonly TimeSpan _duration;
/// Creates a delay definition with a fixed duration.
public DelayDefinition(TimeSpan duration) { _duration = duration; }
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelayProcessor(_duration);
}
///
/// Leaf definition that delays processing by a duration computed from the exchange.
///
public sealed class DelayFactoryDefinition : ProcessorDefinition
{
private readonly Func _durationFactory;
/// Creates a delay definition using a factory.
public DelayFactoryDefinition(Func durationFactory)
{
ArgumentNullException.ThrowIfNull(durationFactory);
_durationFactory = durationFactory;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(async (exchange, ct) =>
{
var delay = _durationFactory(exchange);
if (delay > TimeSpan.Zero)
await Task.Delay(delay, ct).ConfigureAwait(false);
});
}
///
/// Leaf definition that delays processing by a duration resolved from a string expression template.
///
public sealed class DelayExpressionDefinition : ProcessorDefinition
{
private readonly string _expression;
/// Creates a delay definition from a string expression template.
public DelayExpressionDefinition(string expression)
{
ArgumentException.ThrowIfNullOrWhiteSpace(expression);
_expression = expression;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(async (exchange, ct) =>
{
var raw = Expressions.ExpressionResolver.ProcessTemplate(_expression, exchange);
var delay = ConvertToTimeSpan(raw);
if (delay > TimeSpan.Zero)
await Task.Delay(delay, ct).ConfigureAwait(false);
});
/// Delay value → : a , a number of milliseconds, or hh:mm:ss text.
internal static TimeSpan ConvertToTimeSpan(object? value) => value switch
{
TimeSpan ts => ts,
int ms => TimeSpan.FromMilliseconds(ms),
long ms => TimeSpan.FromMilliseconds(ms),
double ms => TimeSpan.FromMilliseconds(ms),
string s when double.TryParse(s, System.Globalization.CultureInfo.InvariantCulture, out var ms) => TimeSpan.FromMilliseconds(ms),
string s when TimeSpan.TryParse(s, out var ts) => ts,
_ => throw new InvalidOperationException($"Cannot convert '{value}' to TimeSpan for delay.")
};
}
///
/// Leaf definition that samples messages by passing every N-th message (count-based).
///
public sealed class SampleCountDefinition : ProcessorDefinition
{
private readonly long _messageFrequency;
/// Gets the message frequency — every N-th message passes.
public long MessageFrequency => _messageFrequency;
/// Creates a sampling definition that allows every Nth message through.
public SampleCountDefinition(long messageFrequency)
{
if (messageFrequency <= 0)
throw new ArgumentOutOfRangeException(nameof(messageFrequency), "Must be > 0.");
_messageFrequency = messageFrequency;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new SamplingProcessor(_messageFrequency);
}
///
/// Leaf definition that samples messages by period (at most one message per period).
///
public sealed class SamplePeriodDefinition : ProcessorDefinition
{
private readonly TimeSpan _period;
/// Gets the minimum period between forwarded messages.
public TimeSpan Period => _period;
/// Creates a sampling definition with a period gate.
public SamplePeriodDefinition(TimeSpan period)
{
if (period <= TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(period), "Must be positive.");
_period = period;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new SamplingProcessor(_period);
}