Path: redb-route/redb.Route/Components/DirectVmComponent.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/DirectVmComponent.cs ---- using System.Collections.Concurrent; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.Components; /// /// Cross-context synchronous component. Scheme: "direct-vm". /// Like Direct but the processor registry is shared across all RouteContexts /// via (DI singleton). /// A consumer in context A registers its processor; a producer in context B /// can invoke it directly — synchronous, zero-copy, cross-context call. /// public class DirectVmComponent : ComponentBase { private readonly ConcurrentDictionary _endpoints = new(StringComparer.OrdinalIgnoreCase); /// public override string Scheme => "direct-vm"; /// public override IEndpoint CreateEndpoint(EndpointUri uri) { ArgumentNullException.ThrowIfNull(uri); var options = new DirectVmEndpointOptions(); options.BindFromUri(uri.RawParameters); options.Validate(); // Keyed with the parameters: a producer's headerFilterStrategy must not be lost to, or leak into, a consumer of // the same name in this context. The call itself finds its target by name (Uri.BaseKey) in the shared registry. return _endpoints.GetOrAdd(uri.NormalizedKey, _ => new DirectVmEndpoint(uri, this, options)); } /// Resolves the shared registry from DI. Returns null if not registered. internal SharedVmRegistry? GetRegistry() { return Context?.GetServiceProvider()?.GetService(); } } /// /// Options for direct-vm endpoints. /// public class DirectVmEndpointOptions : EndpointOptions { /// /// Producer only, as in Apache Camel 3: a registry reference (#name) to an /// for the call, in both directions. With it the called context works on a /// copy of the exchange that carries the headers the strategy lets out; when the call returns, the caller keeps /// its own headers and takes the reply's body and the reply headers the strategy lets in. Without it (the /// default) the called context works on the caller's own exchange and every header crosses both ways. /// [EndpointRole(EndpointRole.Producer)] [RegistryReference(typeof(IHeaderFilterStrategy))] public string? HeaderFilterStrategy { get; set; } /// public override void Validate() { } } /// /// Direct-VM endpoint. Unlike DirectEndpoint, does not hold a local processor reference — /// it delegates to for cross-context lookup. /// public class DirectVmEndpoint : EndpointBase { internal new DirectVmComponent Component => (DirectVmComponent)base.Component; /// The logical name used as key in the shared registry (the URI host). internal string Name => Uri.BaseKey; /// Creates a new direct-vm endpoint. public DirectVmEndpoint(EndpointUri uri, DirectVmComponent component, DirectVmEndpointOptions options) : base(uri, component, options) { _headerFilter = new EndpointHeaderFilter(this, options.HeaderFilterStrategy); } private readonly EndpointHeaderFilter _headerFilter; /// /// The strategy headerFilterStrategy=#name names, or null: direct-vm has no default strategy. A name /// the registry does not hold fails when the producer is built. /// internal IHeaderFilterStrategy? HeaderFilterStrategy => _headerFilter.Configured; /// public override IProducer CreateProducer() => new DirectVmProducer(this); /// public override IConsumer CreateConsumer(IProcessor processor) { ArgumentNullException.ThrowIfNull(processor); var written = EndpointOptions.WrittenForOtherRole(Options.GetType(), Uri, EndpointRole.Consumer); if (written.Count > 0) throw new InvalidOperationException( $"direct-vm consumer '{EndpointUri.Sanitize(Uri.NormalizedKey)}' sets {string.Join(", ", written)}, " + "which applies to the calling side (.To), as in Apache Camel; a consumer has nothing to filter. " + "Move it to the producer URI."); return new DirectVmConsumer(this, processor); } } /// /// Direct-VM producer. Looks up the processor from the shared registry and invokes it synchronously. /// public class DirectVmProducer : IProducer { private readonly DirectVmEndpoint _endpoint; private readonly IHeaderFilterStrategy? _headerFilter; private ILogger? _logger; /// Creates a direct-vm producer. public DirectVmProducer(DirectVmEndpoint endpoint) { _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); _logger = (_endpoint.Component as ComponentBase)?.Logger; _headerFilter = endpoint.HeaderFilterStrategy; } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { var registry = _endpoint.Component.GetRegistry() ?? throw new InvalidOperationException( $"SharedVmRegistry is not available. Register it in DI to use direct-vm endpoints."); var processor = registry.GetProcessor(_endpoint.Name) ?? throw new InvalidOperationException( $"No consumer registered for direct-vm endpoint '{_endpoint.Name}'. " + "Ensure the consumer route is started before sending."); if (_headerFilter is not null) { await ProcessFiltered(processor, exchange, _headerFilter, ct).ConfigureAwait(false); return; } // direct-vm: runs the target route — in another context — 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; } } /// /// The call under a header filter strategy, Apache Camel 3's DirectVmProducer: the called context works on a /// copy, so dropping a header there never takes it from the caller. The copy shares the caller's DI scope, unit of /// work and properties (), as the zero-copy call does. When it returns, the /// caller keeps its own headers and takes the reply's body, content type and the reply headers the strategy lets /// in, plus the copy's properties, exception and stop, which the zero-copy call would have left on it. /// private static async Task ProcessFiltered(IProcessor processor, IExchange exchange, IHeaderFilterStrategy headerFilter, CancellationToken ct) { var submitted = exchange.CloneLinked(); try { foreach (var (name, value) in submitted.In.Headers.ToList()) { if (headerFilter.FilterOutgoing(name, value, submitted)) submitted.In.Headers.Remove(name); } try { await processor.Process(submitted, ct).ConfigureAwait(false); } finally { var reply = submitted.HasOut ? submitted.Out! : submitted.In; exchange.In.Body = reply.Body; exchange.In.ContentType = reply.ContentType; foreach (var (name, value) in reply.Headers) { if (!headerFilter.FilterIncoming(name, value, submitted)) exchange.In.Headers[name] = value; } foreach (var (key, value) in submitted.Properties) { if (!ExchangeResources.IsOwnedByExchange(key)) exchange.Properties[key] = value; } if (submitted.Exception is not null) { exchange.Exception = submitted.Exception; exchange.ExceptionHandled = submitted.ExceptionHandled; } if (submitted.IsStopped) exchange.Stop(); } } finally { // A streamed reply body now on the caller keeps its connection until the caller ends, as with Enrich. ExchangeResources.HandOver(submitted, exchange); await submitted.DisposeAsync().ConfigureAwait(false); } } /// public Task Start(CancellationToken ct = default) { _logger ??= (_endpoint.Component as ComponentBase)?.Logger; _logger?.LogInformation("DirectVm producer started: {Name}", _endpoint.Name); return Task.CompletedTask; } /// public Task Stop(CancellationToken ct = default) { _logger ??= (_endpoint.Component as ComponentBase)?.Logger; _logger?.LogInformation("DirectVm producer stopped: {Name}", _endpoint.Name); return Task.CompletedTask; } } /// /// Direct-VM consumer. Registers the processor in the shared registry on Start, /// unregisters on Stop. Only one consumer per name allowed globally. /// public class DirectVmConsumer : IConsumer { private readonly DirectVmEndpoint _endpoint; public IEndpoint Endpoint => _endpoint; private readonly IProcessor _processor; private ILogger? _logger; /// Creates a direct-vm consumer. public DirectVmConsumer(DirectVmEndpoint 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) { var registry = _endpoint.Component.GetRegistry() ?? throw new InvalidOperationException( "SharedVmRegistry is not available. Register it in DI to use direct-vm endpoints."); if (!registry.TryRegisterProcessor(_endpoint.Name, _processor)) throw new InvalidOperationException( $"A consumer is already registered for direct-vm endpoint '{_endpoint.Name}'. " + "Only one consumer per name is allowed across all contexts."); _logger ??= (_endpoint.Component as ComponentBase)?.Logger; _logger?.LogInformation("DirectVm consumer started: {Name}", _endpoint.Name); return Task.CompletedTask; } /// public Task Stop(CancellationToken ct = default) { var registry = _endpoint.Component.GetRegistry(); registry?.TryUnregisterProcessor(_endpoint.Name, _processor); _logger ??= (_endpoint.Component as ComponentBase)?.Logger; _logger?.LogInformation("DirectVm consumer stopped: {Name}", _endpoint.Name); return Task.CompletedTask; } }