Path: redb-route/redb.Route/Processors/SamplingProcessor.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/SamplingProcessor.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Processors;
///
/// Samples messages from the stream, allowing only a subset to pass through.
/// Supports two modes: count-based (every Nth message) and time-based (at most one per period).
/// Non-sampled messages are stopped via .
/// Thread-safe via lock-free atomic operations.
///
public class SamplingProcessor : IProcessor
{
private readonly long _messageFrequency;
private readonly long _periodTicks;
private long _counter;
private long _lastSampledTicks;
/// Creates a count-based sampler that passes every Nth message.
/// Pass every Nth message (1 = all, 5 = every 5th).
public SamplingProcessor(long messageFrequency)
{
if (messageFrequency < 1)
throw new ArgumentOutOfRangeException(nameof(messageFrequency), "Must be >= 1.");
_messageFrequency = messageFrequency;
}
/// Creates a time-based sampler that passes at most one message per period.
/// Minimum interval between sampled messages.
public SamplingProcessor(TimeSpan period)
{
if (period <= TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(period), "Must be positive.");
_periodTicks = period.Ticks;
}
///
public Task Process(IExchange exchange, CancellationToken ct = default)
{
if (_messageFrequency > 0)
ProcessCountBased(exchange);
else
ProcessTimeBased(exchange);
return Task.CompletedTask;
}
private void ProcessCountBased(IExchange exchange)
{
var count = Interlocked.Increment(ref _counter);
// Pass first message and every Nth after that: 1, 1+N, 1+2N, ...
if ((count - 1) % _messageFrequency != 0)
exchange.Stop();
}
private void ProcessTimeBased(IExchange exchange)
{
var now = DateTime.UtcNow.Ticks;
var last = Interlocked.Read(ref _lastSampledTicks);
if (now - last < _periodTicks)
{
exchange.Stop();
return;
}
// CAS: only one thread wins the race to sample
if (Interlocked.CompareExchange(ref _lastSampledTicks, now, last) != last)
exchange.Stop();
}
}