Path: redb-route/redb.Route/Processors/DynamicEndpointResolver.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/DynamicEndpointResolver.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
namespace redb.Route.Processors;
///
/// Per-instance cache that resolves an endpoint URI per exchange (via a
/// ${...} template, an , or a factory delegate),
/// then looks up the matching component, creates a producer for the resolved URI,
/// starts it, caches it by resolved URI string, and reuses it on subsequent calls.
///
/// Used by dynamic-endpoint processors (, dynamic
/// WireTap/Enrich/PollEnrich/RecipientList) so producers are not recreated on every
/// message. Producers are also registered with so they
/// participate in the standard graceful-shutdown flow.
///
///
public sealed class DynamicEndpointResolver
{
private readonly IRouteContext _context;
private readonly Func _uriFactory;
// Endpoint rides along so the sender can record producer-side statistics (CountedSend).
private readonly ConcurrentDictionary>> _producers = new();
/// Creates a resolver that calls per exchange.
public DynamicEndpointResolver(IRouteContext context, Func uriFactory)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_uriFactory = uriFactory ?? throw new ArgumentNullException(nameof(uriFactory));
}
/// Creates a resolver that resolves a ${...} template per exchange.
public static DynamicEndpointResolver FromTemplate(IRouteContext context, string template)
{
ArgumentException.ThrowIfNullOrWhiteSpace(template);
return new DynamicEndpointResolver(context,
ex => ExpressionResolver.ProcessTemplate(template, ex));
}
/// Creates a resolver that evaluates an per exchange.
public static DynamicEndpointResolver FromExpression(IRouteContext context, IExpression expression)
{
ArgumentNullException.ThrowIfNull(expression);
return new DynamicEndpointResolver(context,
ex => expression.Evaluate(ex)
?? throw new InvalidOperationException(
"Dynamic endpoint expression evaluated to null."));
}
// One evaluation per message even when interception wraps the send: the wrapper resolves the URI
// and parks it on the exchange under a key private to this resolver; the send that follows takes
// it from there instead of evaluating the template again.
private readonly string _parkedUriKey = "__redb_tod:" + Guid.NewGuid().ToString("N");
/// Resolves the URI for and returns a (cached) started producer.
public async Task ResolveProducerAsync(IExchange exchange, CancellationToken ct)
=> (await ResolvePairAsync(exchange, ct).ConfigureAwait(false)).Producer;
/// Same as , with the endpoint for statistics recording.
internal async Task<(IEndpoint Endpoint, IProducer Producer)> ResolvePairAsync(IExchange exchange, CancellationToken ct)
{
string resolved;
if (exchange.Properties.TryGetValue(_parkedUriKey, out var parked) && parked is string parkedUri)
{
resolved = parkedUri;
exchange.Properties.Remove(_parkedUriKey);
}
else
{
resolved = ResolveUri(exchange);
}
return await GetOrCreatePairAsync(resolved, ct).ConfigureAwait(false);
}
/// Returns the started producer for an already-resolved URI, creating and caching it on first use.
public async Task GetOrCreateProducerAsync(string resolvedUri, CancellationToken ct)
=> (await GetOrCreatePairAsync(resolvedUri, ct).ConfigureAwait(false)).Producer;
/// Same as , with the endpoint for statistics recording.
internal async Task<(IEndpoint Endpoint, IProducer Producer)> GetOrCreatePairAsync(string resolvedUri, CancellationToken ct)
{
// Cache key is the fully-resolved URI; one producer per distinct URI.
var lazy = _producers.GetOrAdd(resolvedUri,
uri => new Lazy>(() => CreateAsync(uri, ct)));
try
{
return await lazy.Value.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// The first caller's token was cancelled while its producer started; drop the entry so the
// next message creates the producer instead of awaiting a cancelled task forever.
_producers.TryRemove(new KeyValuePair>>(resolvedUri, lazy));
throw;
}
}
///
/// Resolves the URI and parks it for the send that follows on this exchange;
/// tells whether this call parked it (the owner clears it with , so a skipped
/// send never leaves a stale value for a later pass through the same node).
///
internal string ResolveAndPark(IExchange exchange, out bool parked)
{
if (exchange.Properties.TryGetValue(_parkedUriKey, out var existing) && existing is string existingUri)
{
parked = false;
return existingUri;
}
var resolved = ResolveUri(exchange);
exchange.Properties[_parkedUriKey] = resolved;
parked = true;
return resolved;
}
/// Removes the parked URI (see ).
internal void Unpark(IExchange exchange) => exchange.Properties.Remove(_parkedUriKey);
/// Resolves the target URI for this exchange without creating a producer (used by InterceptSendToEndpoint).
public string ResolveUri(IExchange exchange)
{
var resolved = _uriFactory(exchange);
if (string.IsNullOrWhiteSpace(resolved))
throw new InvalidOperationException(
"Dynamic endpoint URI resolved to an empty value.");
return resolved;
}
private async Task<(IEndpoint, IProducer)> CreateAsync(string resolvedUri, CancellationToken ct)
{
var endpoint = _context.GetEndpoint(resolvedUri);
var producer = endpoint.CreateProducerOnItsSide();
await producer.Start(ct).ConfigureAwait(false);
(_context as RouteContext)?.TrackProducer(producer);
return (endpoint, producer);
}
}