Código
Basado en el Reloj de Cobranza que diseñé en Totalplay
Motor de enrutamiento de cobranza
Un millón de cuentas por ciclo, en paralelo y escalado horizontalmente.
14 archivos710 líneas de código 12/12 pruebas
using System.Diagnostics;
using System.Threading.Channels;
using CollectionRouter.Domain;
using CollectionRouter.Infrastructure;
using CollectionRouter.Rules;
namespace CollectionRouter.Pipeline;
public sealed record RouterOptions(
string CycleId,
int DegreeOfParallelism = 32,
int BatchSize = 500,
int ChannelCapacity = 5_000);
public sealed record RouterResult(long Read, long Owned, long Routed, TimeSpan Elapsed)
{
public double PerSecond => Elapsed.TotalSeconds > 0 ? Routed / Elapsed.TotalSeconds : 0;
}
/// <summary>
/// Pipeline productor/consumidor con canales acotados:
///
/// origen ──▶ [canal de cuentas] ──▶ N workers (enriquecer + reglas) ──▶ [canal de decisiones] ──▶ escritor por lotes
///
/// - Los canales acotados aplican contrapresión: si el destino es lento, el origen espera
/// en lugar de llenar la memoria.
/// - Cada instancia procesa solo su partición (<see cref="Partitioner"/>).
/// - El escritor agrupa en lotes para minimizar viajes de red al destino.
/// </summary>
public sealed class ParallelRouter
{
private readonly IAccountSource _source;
private readonly IAccountEnricher _enricher;
private readonly IDecisionSink _sink;
private readonly Partitioner _partitioner;
private readonly RouterOptions _options;
public ParallelRouter(IAccountSource source, IAccountEnricher enricher, IDecisionSink sink,
Partitioner partitioner, RouterOptions options)
{
_source = source;
_enricher = enricher;
_sink = sink;
_partitioner = partitioner;
_options = options;
}
public async Task<RouterResult> RunAsync(CancellationToken cancellationToken = default)
{
var stopwatch = Stopwatch.StartNew();
long read = 0, owned = 0, routed = 0;
var accounts = Channel.CreateBounded<Account>(new BoundedChannelOptions(_options.ChannelCapacity)
{
SingleWriter = true,
FullMode = BoundedChannelFullMode.Wait,
});
var decisions = Channel.CreateBounded<RoutingDecision>(new BoundedChannelOptions(_options.ChannelCapacity)
{
SingleReader = true,
FullMode = BoundedChannelFullMode.Wait,
});
// Productor: lee el origen y descarta lo que no pertenece a esta instancia.
var producer = Task.Run(async () =>
{
try
{
await foreach (var account in _source.ReadAsync(cancellationToken))
{
Interlocked.Increment(ref read);
if (!_partitioner.Owns(account.Id)) continue;
Interlocked.Increment(ref owned);
await accounts.Writer.WriteAsync(account, cancellationToken);
}
}
finally
{
accounts.Writer.Complete();
}
}, cancellationToken);
// Workers: enriquecen (E/S) y aplican las reglas (CPU) en paralelo.
var workers = Enumerable.Range(0, _options.DegreeOfParallelism).Select(_ => Task.Run(async () =>
{
await foreach (var account in accounts.Reader.ReadAllAsync(cancellationToken))
{
var enriched = await _enricher.EnrichAsync(account, cancellationToken);
await decisions.Writer.WriteAsync(RoutingRules.Route(enriched, _options.CycleId), cancellationToken);
}
}, cancellationToken)).ToArray();
var workersDone = Task.WhenAll(workers).ContinueWith(
t => decisions.Writer.Complete(t.Exception), TaskScheduler.Default);
// Escritor: un solo consumidor que agrupa decisiones en lotes.
var writer = Task.Run(async () =>
{
var batch = new List<RoutingDecision>(_options.BatchSize);
await foreach (var decision in decisions.Reader.ReadAllAsync(cancellationToken))
{
batch.Add(decision);
if (batch.Count < _options.BatchSize) continue;
await _sink.WriteBatchAsync(batch.ToArray(), cancellationToken);
Interlocked.Add(ref routed, batch.Count);
batch.Clear();
}
if (batch.Count > 0)
{
await _sink.WriteBatchAsync(batch.ToArray(), cancellationToken);
Interlocked.Add(ref routed, batch.Count);
}
}, cancellationToken);
await Task.WhenAll(producer, workersDone, writer);
await Task.WhenAll(workers); // propaga excepciones de los workers, si las hubo
return new RouterResult(read, owned, routed, stopwatch.Elapsed);
}
}