fix(grpc): configure Kestrel HTTP/2 keepalive (60 s ping, 10 s timeout) under MxGateway:Protocol (SES-005, #253)
This commit is contained in:
@@ -714,6 +714,19 @@ public sealed class GatewayOptionsValidator : OptionsValidatorBase<GatewayOption
|
||||
builder.Add(
|
||||
$"MxGateway:Protocol:MaxGrpcMessageBytes must be between {MinimumMaxMessageBytes} and {MaximumMaxMessageBytes}.");
|
||||
}
|
||||
|
||||
// Kestrel rejects a non-positive ping delay or timeout, so 0 is the explicit "off" for the
|
||||
// delay only; the timeout must always be a real bound.
|
||||
if (options.Http2KeepAlivePingDelaySeconds is < 0 or > 86400)
|
||||
{
|
||||
builder.Add(
|
||||
"MxGateway:Protocol:Http2KeepAlivePingDelaySeconds must be between 0 (disabled) and 86400.");
|
||||
}
|
||||
|
||||
if (options.Http2KeepAlivePingTimeoutSeconds is < 1 or > 3600)
|
||||
{
|
||||
builder.Add("MxGateway:Protocol:Http2KeepAlivePingTimeoutSeconds must be between 1 and 3600.");
|
||||
}
|
||||
}
|
||||
|
||||
// The worker-frame (pipe) maximum must stay above the public gRPC cap by the envelope-overhead
|
||||
|
||||
@@ -3,7 +3,7 @@ using ZB.MOM.WW.MxGateway.Contracts;
|
||||
namespace ZB.MOM.WW.MxGateway.Server.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration options for the worker protocol version.
|
||||
/// Configuration options for the worker protocol version and the public gRPC (HTTP/2) transport.
|
||||
/// </summary>
|
||||
public sealed class ProtocolOptions
|
||||
{
|
||||
@@ -14,4 +14,21 @@ public sealed class ProtocolOptions
|
||||
|
||||
/// <summary>Gets or sets the maximum gRPC message size in bytes.</summary>
|
||||
public int MaxGrpcMessageBytes { get; init; } = 16 * 1024 * 1024;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how long an HTTP/2 connection may be idle (no frames received) before the
|
||||
/// gateway sends a keepalive PING, in seconds. <c>0</c> disables server pings. Maps to
|
||||
/// Kestrel's <c>Limits.Http2.KeepAlivePingDelay</c>. Without it a client that vanished
|
||||
/// without a FIN or RST (power loss, a firewall or NAT idle drop) holds its
|
||||
/// <c>StreamEvents</c> call, its session and its worker until a write times out, which on a
|
||||
/// quiet session may be never.
|
||||
/// </summary>
|
||||
public int Http2KeepAlivePingDelaySeconds { get; init; } = 60;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets how long the gateway waits for any frame after sending a keepalive PING
|
||||
/// before it closes the connection, in seconds. Maps to Kestrel's
|
||||
/// <c>Limits.Http2.KeepAlivePingTimeout</c>.
|
||||
/// </summary>
|
||||
public int Http2KeepAlivePingTimeoutSeconds { get; init; } = 10;
|
||||
}
|
||||
|
||||
@@ -110,6 +110,9 @@ public static class GatewayApplication
|
||||
builder.AddZbSerilog(o => o.ServiceName = "mxgateway");
|
||||
|
||||
builder.Services.AddGatewayConfiguration(builder.Configuration);
|
||||
builder.Services.AddSingleton<
|
||||
Microsoft.Extensions.Options.IConfigureOptions<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>,
|
||||
GatewayKestrelKeepAliveConfiguration>();
|
||||
builder.Services.AddZbSecrets(builder.Configuration, "Secrets");
|
||||
builder.Services.AddSqliteAuthStore(builder.Configuration);
|
||||
builder.Services.AddGatewayGrpcAuthorization();
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Server.Grpc;
|
||||
|
||||
/// <summary>
|
||||
/// Applies the gateway's HTTP/2 keepalive policy (<c>MxGateway:Protocol:Http2KeepAlive*</c>) to
|
||||
/// Kestrel (SES-005).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Kestrel's HTTP/2 keepalive ping is off unless configured. A client that disappears
|
||||
/// without closing its connection (host power loss, a firewall or NAT idle drop, a pulled
|
||||
/// cable) is otherwise noticed only when a write times out at the TCP layer, and a quiet
|
||||
/// session writes nothing: its <c>StreamEvents</c> call stays open, the session's single
|
||||
/// subscriber slot stays taken — so the real client's resume is refused with
|
||||
/// <c>EventSubscriberAlreadyActive</c> — and the session and its x86 worker are never
|
||||
/// swept. With the ping on, a dead peer is detected within the delay plus the timeout,
|
||||
/// the call's token fires, and the subscriber detaches.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Kestrel has no minimum-client-ping-interval policy (no equivalent of grpc-core's
|
||||
/// <c>GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS</c>): it answers every client
|
||||
/// PING and never sends <c>GOAWAY</c>/<c>ENHANCE_YOUR_CALM</c> for ping rate, with or without
|
||||
/// an active call. A received PING also resets the connection's idle keep-alive timeout,
|
||||
/// so client keepalive pings (the official clients send one every 30 s, also with no call
|
||||
/// active) keep an otherwise idle connection open. There is therefore nothing to configure
|
||||
/// on the server side for client pings.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The section is bound straight from configuration rather than through
|
||||
/// <c>IOptions<GatewayOptions></c>: Kestrel resolves its options while the server is
|
||||
/// constructed, and reading the validated gateway options there would surface any
|
||||
/// configuration error from inside Kestrel's construction instead of as the
|
||||
/// <c>OptionsValidationException</c> startup validation reports.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="configuration">The application configuration holding <c>MxGateway:Protocol</c>.</param>
|
||||
public sealed class GatewayKestrelKeepAliveConfiguration(IConfiguration configuration)
|
||||
: IConfigureOptions<KestrelServerOptions>
|
||||
{
|
||||
/// <summary>The configuration section the keepalive options bind from.</summary>
|
||||
public const string SectionName = "MxGateway:Protocol";
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Configure(KestrelServerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ProtocolOptions protocol = configuration.GetSection(SectionName).Get<ProtocolOptions>() ?? new ProtocolOptions();
|
||||
|
||||
// Out-of-range values are rejected by GatewayOptionsValidator at startup; the guards here
|
||||
// only keep Kestrel's own setters from throwing before that validation reports.
|
||||
options.Limits.Http2.KeepAlivePingDelay = protocol.Http2KeepAlivePingDelaySeconds > 0
|
||||
? TimeSpan.FromSeconds(protocol.Http2KeepAlivePingDelaySeconds)
|
||||
: TimeSpan.MaxValue;
|
||||
if (protocol.Http2KeepAlivePingTimeoutSeconds > 0)
|
||||
{
|
||||
options.Limits.Http2.KeepAlivePingTimeout = TimeSpan.FromSeconds(protocol.Http2KeepAlivePingTimeoutSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,7 +90,9 @@
|
||||
},
|
||||
"Protocol": {
|
||||
"WorkerProtocolVersion": 1,
|
||||
"MaxGrpcMessageBytes": 16777216
|
||||
"MaxGrpcMessageBytes": 16777216,
|
||||
"Http2KeepAlivePingDelaySeconds": 60,
|
||||
"Http2KeepAlivePingTimeoutSeconds": 10
|
||||
},
|
||||
"Galaxy": {
|
||||
"ConnectionString": "Server=localhost;Database=ZB;Integrated Security=True;TrustServerCertificate=True;Encrypt=False;",
|
||||
|
||||
Reference in New Issue
Block a user