Path: redb-route/redb.Route.SignalR/SignalRConsumer.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.SignalR/SignalRConsumer.cs
----
using System.Diagnostics;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http;
using redb.Route.Telemetry;
namespace redb.Route.SignalR;
///
/// SignalR consumer. Embedded Kestrel-based SignalR Hub server that accepts connections,
/// receives method invocations, and dispatches them into the route processor pipeline.
/// Supports JSON and MessagePack protocols, group management, InOut exchange pattern,
/// and lifecycle events (Connected/Disconnected).
///
public class SignalRConsumer : IConsumer
{
private readonly SignalREndpoint _endpoint;
///
public IEndpoint Endpoint => _endpoint;
private readonly IProcessor _processor;
private readonly SignalREndpointOptions _options;
private SharedHttpServerManager? _serverManager;
private readonly InflightDrainGuard _drain = new();
private long _processedCount;
private ILogger? _logger;
/// Creates a SignalR consumer.
public SignalRConsumer(SignalREndpoint endpoint, IProcessor processor, SignalREndpointOptions options)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_processor = processor ?? throw new ArgumentNullException(nameof(processor));
_options = options ?? throw new ArgumentNullException(nameof(options));
_logger = (endpoint.Component as ComponentBase)?.Logger;
}
/// Number of invocations successfully processed.
public long ProcessedCount => Interlocked.Read(ref _processedCount);
// ── Connection admission ───────────────────────────────────────────
private int _connections;
///
/// Claims a connection slot against maxConnections. A refusal is counted in the
/// endpoint's Rejected; the hub aborts the connection before the Connected lifecycle event
/// can reach the pipeline. Always 'true' when the limit is off (0).
///
internal bool TryAcquireConnection()
{
if (_options.MaxConnections <= 0) return true;
var now = Interlocked.Increment(ref _connections);
if (now <= _options.MaxConnections) return true;
Interlocked.Decrement(ref _connections);
_endpoint.RecordRejected();
_logger?.LogWarning(
"SignalR hub {HubPath}: connection refused - maxConnections={Max} reached",
HubPath, _options.MaxConnections);
return false;
}
/// Releases a slot claimed by .
internal void ReleaseConnection()
{
if (_options.MaxConnections > 0)
Interlocked.Decrement(ref _connections);
}
/// The base URL the server is listening on. Available after Start().
public string? BaseUrl { get; private set; }
/// The hub path for this consumer.
public string HubPath => _endpoint.HubPath;
/// The endpoint options, exposed for the bridge hub.
internal SignalREndpointOptions EndpointOptions => _options;
/// The service scope factory from the endpoint, for creating scoped exchanges.
internal IServiceScopeFactory? ScopeFactory => _endpoint.ScopeFactory;
///
/// The hub's own container (see ). The server-mode producer resolves
/// IHubContext from here, so it reaches the clients of this hub and of no other.
///
internal IServiceProvider? HubServices => _endpoint.HubServices;
///
/// Processes an exchange through the pipeline. Called by .
///
/// The invocation or lifecycle event.
/// The connection's abort token.
///
/// The trace context the client sent with its connection request, or default. A hub message carries no
/// headers of its own, so the span of each one is a root that links this context; the connection may live for
/// hours and is never the parent.
///
internal async Task ProcessExchange(IExchange exchange, CancellationToken ct, ActivityContext handshake = default)
{
using var span = RouteTelemetryExtensions.StartConsumerSpan