Path: redb-route/redb.Route/Components/DirectComponent.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/Components/DirectComponent.cs
----
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Components;
///
/// In-memory synchronous component. Scheme: "direct".
/// When a producer sends an exchange to a direct endpoint, the consumer's processor
/// is invoked synchronously (in-process). No threading, no queuing — direct call.
/// Ideal for testing and for connecting route segments within the same process.
///
public class DirectComponent : ComponentBase
{
private readonly ConcurrentDictionary _endpoints = new(StringComparer.OrdinalIgnoreCase);
///
public override string Scheme => "direct";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
return _endpoints.GetOrAdd(uri.BaseKey, _ => new DirectEndpoint(uri, this));
}
}
///
/// Options for direct endpoints. Currently no parameters needed.
///
public class DirectEndpointOptions : EndpointOptions
{
///
public override void Validate() { }
}
///
/// Direct endpoint. Holds a reference to the consumer processor so producers can invoke it.
///
public class DirectEndpoint : EndpointBase
{
private volatile IProcessor? _consumerProcessor;
/// Creates a new direct endpoint.
public DirectEndpoint(EndpointUri uri, DirectComponent component)
: base(uri, component, new DirectEndpointOptions())
{
}
/// Gets the consumer processor (null if no consumer registered).
internal IProcessor? ConsumerProcessor => _consumerProcessor;
///
public override IProducer CreateProducer() => new DirectProducer(this);
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
return new DirectConsumer(this, processor);
}
/// Registers the consumer processor (called by DirectConsumer.Start).
internal void RegisterConsumerProcessor(IProcessor processor)
{
_consumerProcessor = processor;
}
/// Unregisters the consumer processor (called by DirectConsumer.Stop).
internal void UnregisterConsumerProcessor()
{
_consumerProcessor = null;
}
}
///
/// Direct producer. Sends exchanges by invoking the endpoint's consumer processor synchronously.
///
public class DirectProducer : IProducer
{
private readonly DirectEndpoint _endpoint;
private ILogger? _logger;
/// Creates a direct producer.
public DirectProducer(DirectEndpoint endpoint)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_logger = (endpoint.Component as ComponentBase)?.Logger;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
var processor = _endpoint.ConsumerProcessor
?? throw new InvalidOperationException(
$"No consumer registered for direct endpoint '{_endpoint.Uri.NormalizedKey}'. " +
"Ensure the consumer route is started before sending.");
// direct: runs the target route on THIS exchange, and the target's first step stamps
// exchange.RouteId; nothing restores it, so after the call the caller would carry the callee's id
// and its own route span and step metrics would be attributed to the callee. Save ours and put it
// back once the target returns — on failure too, before the caller's own error handler runs.
var callerRouteId = exchange.RouteId;
try
{
await processor.Process(exchange, ct).ConfigureAwait(false);
}
finally
{
exchange.RouteId = callerRouteId;
}
}
///
public Task Start(CancellationToken ct = default)
{
_logger?.LogInformation("Direct producer started: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger));
return Task.CompletedTask;
}
///
public Task Stop(CancellationToken ct = default)
{
_logger?.LogInformation("Direct producer stopped: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger));
return Task.CompletedTask;
}
}
///
/// Direct consumer. Registers the processor on the endpoint so producers can call it.
/// Does not poll or subscribe — just holds a reference.
///
public class DirectConsumer : IConsumer
{
private readonly DirectEndpoint _endpoint;
public IEndpoint Endpoint => _endpoint;
private readonly IProcessor _processor;
private ILogger? _logger;
/// Creates a direct consumer.
public DirectConsumer(DirectEndpoint endpoint, IProcessor processor)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_processor = processor ?? throw new ArgumentNullException(nameof(processor));
_logger = (endpoint.Component as ComponentBase)?.Logger;
}
///
public Task Start(CancellationToken ct = default)
{
_endpoint.RegisterConsumerProcessor(_processor);
_logger ??= (_endpoint.Component as ComponentBase)?.Logger;
_logger?.LogInformation("Direct consumer started: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger));
return Task.CompletedTask;
}
///
public Task Stop(CancellationToken ct = default)
{
_endpoint.UnregisterConsumerProcessor();
_logger ??= (_endpoint.Component as ComponentBase)?.Logger;
_logger?.LogInformation("Direct consumer stopped: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger));
return Task.CompletedTask;
}
}