Path: redb-route/redb.Route.Grpc/GrpcEndpointOptions.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.Grpc/GrpcEndpointOptions.cs
----
using redb.Route.Abstractions;
using redb.Route.Http;
using redb.Route.Core;
namespace redb.Route.Grpc;
/// How the consumer interprets the request body and shapes the reply.
public enum GrpcEnvelopeMode
{
///
/// Envelope for the built-in generic method, raw bytes for any other address. Lets the shipped
/// RedbService keep its headers-plus-payload contract while a typed .proto served on
/// its own address receives exactly the bytes its client sent.
///
Auto,
/// Always decode the body as a RedbMessage envelope (payload + string headers).
Message,
/// Never decode: the exchange body is the raw protobuf message the caller sent.
Raw,
}
/// Message compression codec (gRPC grpc-encoding).
public enum GrpcCompression
{
/// Send replies uncompressed. Default. Compressed requests are still accepted.
None,
/// Gzip replies, but only when the caller advertises gzip in grpc-accept-encoding.
Gzip,
}
/// Whether the consumer requires, allows, or ignores a client certificate (mTLS).
public enum GrpcClientCertificateMode
{
/// No client certificate is requested. Default.
NoCertificate,
/// A certificate is requested; the call proceeds when the client presents none.
AllowCertificate,
/// A certificate is required; the handshake fails without one.
RequireCertificate,
}
///
/// Options for the gRPC endpoint. Shared by both producer and consumer.
/// Use URI parameters to configure: grpc:host:port/service/method?deadline=30000
///
public class GrpcEndpointOptions : EndpointOptions
{
/// The built-in generic unary method served when the URI carries no method address.
public const string DefaultMethodPath = "/redb.route.grpc.RedbService/Process";
/// The built-in generic server-streaming method.
public const string DefaultStreamMethodPath = "/redb.route.grpc.RedbService/ProcessStream";
// ── Common ──────────────────────────────────────────
///
/// A registry reference (#name) to an that
/// decides which headers this endpoint carries between the exchange and call metadata or envelope headers (a
/// call, a request, a reply both ways), in place of .
///
[RegistryReference(typeof(IHeaderFilterStrategy))]
public string? HeaderFilterStrategy { get; set; }
/// Deadline (timeout) in milliseconds. Default: 30000 (30s). 0 = no deadline.
public int Deadline { get; set; } = 30_000;
/// Expression override for deadline (resolves to int milliseconds).
public string? DeadlineExpression { get; set; }
///
/// Full method address this endpoint serves or calls, /package.Service/Method. Taken from the
/// URI path after host:port; defaults to . This is the route
/// key on the consumer side, so many gRPC methods live on one port as ordinary path routes.
///
public string MethodPath { get; set; } = DefaultMethodPath;
/// Body handling for the consumer. Default: .
public GrpcEnvelopeMode Envelope { get; set; } = GrpcEnvelopeMode.Auto;
///
/// Fully-qualified service name, e.g. identity.v1.Identity. Parity with camel-grpc, which
/// addresses a service in the URI path and the method as a parameter. Combined with
/// it forms .
///
public string? Service { get; set; }
///
/// Method name within , e.g. Token. Parity with camel-grpc's
/// method parameter.
///
public string? Method { get; set; }
///
/// Single knob for all three size limits (send, receive, server request), mirroring camel-grpc's
/// maxMessageSize. An explicit per-direction value in the URI wins over it.
///
public int MaxMessageSize { get; set; }
///
/// Channel security, mirroring camel-grpc's negotiationType: PLAINTEXT or TLS.
/// A convenience over / , which stay authoritative when set
/// explicitly.
///
public string? NegotiationType { get; set; }
// ── Producer (GrpcChannel) ──────────────────────────
/// Use plaintext (HTTP/2 without TLS). Default: true. Set to false for TLS.
public bool Plaintext { get; set; } = true;
/// Max send message size in bytes. Default: 4 MB. 0 = unlimited.
public int MaxSendMessageSize { get; set; } = 4 * 1024 * 1024;
/// Max receive message size in bytes. Default: 4 MB. 0 = unlimited.
public int MaxReceiveMessageSize { get; set; } = 4 * 1024 * 1024;
///
/// Throw when the call fails, so .OnException(...), retry and dead-letter see it — the same
/// contract as the HTTP and SOAP producers. Default: true. Set false for the legacy behaviour where
/// the failure was only recorded on the exchange and the pipeline carried on.
///
public bool ThrowOnError { get; set; } = true;
/// Path to a PFX client certificate presented to the server (mTLS, producer side).
public string? ClientCertPath { get; set; }
/// Password for .
[Sensitive]
public string? ClientCertPassword { get; set; }
// ── Consumer (Kestrel, shared host) ─────────────────
/// Bind host for the gRPC server. Default: 0.0.0.0.
public string Host { get; set; } = "0.0.0.0";
/// Bind port for the gRPC server. Default: 50051.
public int Port { get; set; } = 50051;
/// Enable TLS for the consumer. Default: false (plaintext HTTP/2, i.e. h2c).
public bool Ssl { get; set; }
/// Path to PFX certificate file for TLS. Required when ssl=true.
public string? SslCertPath { get; set; }
/// Password for the PFX certificate.
[Sensitive]
public string? SslCertPassword { get; set; }
/// Client-certificate policy (mTLS). Requires .
public GrpcClientCertificateMode ClientCertificateMode { get; set; } = GrpcClientCertificateMode.NoCertificate;
///
/// Comma-separated thumbprints of accepted client certificates. When set, a presented certificate
/// whose thumbprint is not listed is rejected even if its chain validates.
///
public string? AllowedClientThumbprints { get; set; }
///
/// Named from the route registry. Lets the TLS certificate
/// password live in the registry instead of the endpoint URI, so it never reaches logs
/// or dashboards.
///
[RegistryReference(typeof(GrpcConnectionFactory))]
public string? ConnectionFactory { get; set; }
/// Max request message size in bytes for the server. Default: 4 MB. 0 = unlimited.
public int MaxRequestMessageSize { get; set; } = 4 * 1024 * 1024;
///
/// If true, the consumer returns the exchange Out body as the gRPC response (InOut pattern).
/// If false, returns an empty response (InOnly pattern). Default: true for gRPC.
///
public bool InOut { get; set; } = true;
///
/// Serve this address as a server-streaming method: the route's reply may carry many messages and
/// each is written as its own frame. Defaults to true for .
///
public bool Streaming { get; set; }
///
/// Mirror the resolved client address into redbHttp.RemoteAddress as well, so processors
/// written against the HTTP transport (rate limiting, lockout, device metadata) work unchanged
/// behind a gRPC facade. Default: false.
///
public bool EmitHttpCompatHeaders { get; set; }
///
/// Accept caller-supplied headers that carry a transport-reserved prefix (redbGrpc.,
/// redbHttp., …). Default: false — a client must not be able to forge the metadata that
/// upstream processors trust. Turning this on is logged.
///
public bool AllowClientReservedHeaders { get; set; }
///
/// Do not translate a route's status.code into a gRPC status; always answer OK and let
/// the caller read the error out of the body. Escape hatch for clients written against the old
/// behaviour. Default: false.
///
public bool SuppressStatusMapping { get; set; }
///
/// Also serve grpc.health.v1.Health/Check on this host:port, answering SERVING while the
/// route is running. Standard probe for Kubernetes, Consul and Envoy. Default: false.
///
public bool Health { get; set; }
///
/// Reply compression. Requests are decompressed regardless of this setting (we advertise
/// identity,gzip); this only decides whether we compress what we send, and even then only when
/// the caller advertised gzip. Default: .
///
public GrpcCompression Compression { get; set; } = GrpcCompression.None;
// ── Admission limit (HTTP_CONCURRENCY_LIMITS_PLAN) ──
///
/// Maximum concurrent executions of this consumer's pipeline for UNARY calls. 0 (default) =
/// unlimited. Streaming methods are deliberately not counted — a long-lived stream would hold
/// a permit for its whole life and starve the limit (a dedicated maxConcurrentStreams may come
/// later); the health method is not counted either, so orchestrator probes never flap under
/// load. Overflow beyond the limit and is shed with
/// before any pipeline work.
///
public int MaxConcurrentRequests { get; set; }
/// Requests over the limit that WAIT for a permit (FIFO). 0 (default) = reject immediately.
public int RequestQueueLimit { get; set; }
/// HTTP status code for a shed request. Default 429 (gRPC clients see an HTTP-level error).
public int RejectStatusCode { get; set; } = 429;
/// Value of the Retry-After header on a shed request; 0 = do not send it. Default 1.
public int RetryAfterSeconds { get; set; } = 1;
///
public override void Validate()
{
ConcurrencyLimitOptions.ValidateShape(MaxConcurrentRequests, RequestQueueLimit, RejectStatusCode, RetryAfterSeconds);
if (Deadline < 0)
throw new ArgumentException("Deadline must be >= 0.");
if (Port is < 0 or > 65535)
throw new ArgumentException("Port must be between 0 and 65535.");
// No SslCertPath check here on purpose: the certificate may come from a named connection
// factory or from the host default (HttpHostingOptions.Tls). The invariant "TLS asked for
// and no certificate resolves" is enforced once, at the bind, by SharedHttpServerManager.
if (ClientCertificateMode != GrpcClientCertificateMode.NoCertificate && !Ssl)
throw new ArgumentException("clientCertificateMode requires ssl=true — mTLS needs a TLS handshake.");
if (!string.IsNullOrEmpty(MethodPath) && !MethodPath.StartsWith('/'))
MethodPath = "/" + MethodPath;
}
///
/// True when the URI carried no method address, i.e. this endpoint is the built-in generic service.
/// Such a consumer serves both Process and ProcessStream, as it always has.
///
internal bool DefaultMethodAddress { get; set; } = true;
/// True when the body should be decoded as a RedbMessage envelope.
internal bool UseEnvelope => Envelope switch
{
GrpcEnvelopeMode.Message => true,
GrpcEnvelopeMode.Raw => false,
_ => string.Equals(MethodPath, DefaultMethodPath, StringComparison.OrdinalIgnoreCase)
|| string.Equals(MethodPath, DefaultStreamMethodPath, StringComparison.OrdinalIgnoreCase),
};
}