diff --git a/docs/configuration.md b/docs/configuration.md index 0f072dc9..253065ad 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -54,8 +54,9 @@ Nested groups: - `BackFiller:Logging` - `BackFiller:ArticleRetention` — `MaximumRetainedPayloadGigabytes` default 4 (must be > 0 and within the truncated 80% physical-memory ceiling; see Host physical memory), `RetentionTtlSeconds` default 60 and range 1–60, `MaxOpenableRequestIdsPerArticle` default 16 and range 1–256, `SweepIntervalSeconds` default 1 and range 1–60 - `BackFiller:Shutdown` — `GracePeriodSeconds` default 30, range 5–600; `DrainQueuedWork` default true; `FinishActiveArticles` default true -- `BackFiller:Listener` — `TlsHandshakeTimeoutSeconds` default 30, range 1–300, copied into the runtime snapshot and used for the handshake. `IoProgressTimeoutSeconds` default 60, range 1–600, copied and used as the no-progress deadline for one listener read or one non-empty listener write. A completed call starts a new deadline for the next call. The deadline is not restarted for each 32 KiB of one buffer. `MaxActiveConnections` default 1024, minimum 1, and the listener rejects further accepts at that count. `SendBufferBytes` default 2097152 (2 MiB) and `ReceiveBufferBytes` default 1048576 (1 MiB) are copied and applied to each accepted socket before TLS. They are independent. The range for each is 16384 through 16777216. Nagle is disabled on those sockets and is not a setting. The listening socket does not carry these options. At the default connection cap the default buffers are a 3 GiB kernel-buffer ceiling (1024 × 3 MiB), not memory reserved at bind, and not a measured TCP window. The operating system may round, clamp, or double the stored limit. `MaxQueuedFoundPayloadBytes` default 67108864, minimum 1, is copied into the snapshot; the cache listener does not read it. `ParserAccumulationMaxBytes` default 262144, minimum 32768, and `AwaitingReceiptAckTimeoutSeconds` default 30, range 1–300, are validated and are not copied into `BackFillerListenerRuntimeOptions`. +- `BackFiller:Listener` — `TlsHandshakeTimeoutSeconds` default 30, range 1–300, copied into the runtime snapshot and used for the handshake. `IoProgressTimeoutSeconds` default 60, range 1–600, copied and used as the no-progress deadline for one listener read or one non-empty listener write. A completed call starts a new deadline for the next call. The deadline is not restarted for each 32 KiB of one buffer. `MaxActiveConnections` default 1024, minimum 1, and the listener rejects further accepts at that count. `SendBufferBytes` default 2097152 (2 MiB) and `ReceiveBufferBytes` default 1048576 (1 MiB) are copied and applied to each accepted socket before TLS. They are independent. The range for each is 16384 through 16777216. Nagle is disabled on those sockets and is not a setting. TCP keepalive for accepted sockets comes from `BackFiller:TcpKeepAlive`, not this section. The listening socket does not carry buffer or keepalive options. At the default connection cap the default buffers are a 3 GiB kernel-buffer ceiling (1024 × 3 MiB), not memory reserved at bind, and not a measured TCP window. The operating system may round, clamp, or double the stored limit. `MaxQueuedFoundPayloadBytes` default 67108864, minimum 1, is copied into the snapshot; the cache listener does not read it. `ParserAccumulationMaxBytes` default 262144, minimum 32768, and `AwaitingReceiptAckTimeoutSeconds` default 30, range 1–300, are validated and are not copied into `BackFillerListenerRuntimeOptions`. - `BackFiller:Nntp` — `MaxConcurrentSessionEstablishments` default 8, range 1–1024. Zero is invalid. This does not replace per-provider `MaxSessions` from MySQL. `SendBufferBytes` default 1048576 (1 MiB) and `ReceiveBufferBytes` default 2097152 (2 MiB) are the upstream TCP buffer limits. Each is validated from 16384 through 16777216. They are independent of each other and of the 64 KiB managed reader buffer. Nagle is disabled in code. One upstream session at the defaults requests 3 MiB of kernel buffering. Steady-state session count remains the sum of per-provider `MaxSessions`. These buffers do not raise the establishment gate. The effective window still depends on the operating system, autotuning, TLS, and the path. +- `BackFiller:TcpKeepAlive` — TCP `SO_KEEPALIVE` for every upstream NNTP socket and every accepted cache-listener socket. `TimeSeconds` default 10 (range 1–7200) is idle time before the first probe. `IntervalSeconds` default 5 (range 1–300) is the gap between probes. `RetryCount` default 3 (range 1–20) is unanswered probes before the stack drops the connection. At the defaults, dead-path detection completes in about 25 s, before the default NNTP command timeout (30 s) and well before the listener I/O-progress timeout (60 s). Independent of per-provider MySQL `keepalive` (NNTP `DATE`). Nagle and buffer settings are unchanged. Platforms that cannot set a probe option still enable `SO_KEEPALIVE` and keep OS defaults for that option; enabling keepalive itself must succeed or the socket is rejected. - `BackFiller:Systemd` — notify and watchdog enablement ## RabbitMQ section diff --git a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptions.cs b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptions.cs index 02603e63..0c410c73 100644 --- a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptions.cs +++ b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptions.cs @@ -209,6 +209,15 @@ ServerId is { } serverId /// Required. The runtime snapshot rejects a null section. public BackFillerNntpOptions Nntp { get; set; } = new(); + /// + /// Gets or sets TCP keepalive probe timings for upstream NNTP sockets and accepted listener sockets. + /// + /// + /// Required. Defaults and ranges live on . + /// Independent of per-provider MySQL keepalive (NNTP DATE). + /// + public BackFillerTcpKeepAliveOptions TcpKeepAlive { get; set; } = new(); + /// /// Gets or sets Linux systemd notify/watchdog options. /// @@ -480,4 +489,46 @@ internal sealed class BackFillerArticleRetentionOptions /// Default 1. Validation accepts 1–60. The snapshot stores a . public int SweepIntervalSeconds { get; set; } = 1; } + + /// + /// TCP keepalive timings for upstream provider sockets and accepted cache-listener sockets. + /// + /// + /// Applied by before NNTP or TLS traffic. + /// Does not replace application-level NNTP DATE keepalive from MySQL keepalive. + /// Defaults match . + /// + internal sealed class BackFillerTcpKeepAliveOptions + { + /// + /// Idle seconds before the first TCP keepalive probe. + /// + /// + /// Default . + /// Validation accepts – + /// . + /// + public int TimeSeconds { get; set; } = Networking.TcpKeepAliveSettings.DefaultTimeSeconds; + + /// + /// Seconds between consecutive TCP keepalive probes. + /// + /// + /// Default . + /// Validation accepts – + /// . + /// + public int IntervalSeconds { get; set; } = Networking.TcpKeepAliveSettings.DefaultIntervalSeconds; + + /// + /// Unanswered probe count before the stack drops the connection. + /// + /// + /// Default . + /// Validation accepts – + /// . + /// Platforms that cannot set retry count keep OS defaults after enabling SO_KEEPALIVE. + /// + public int RetryCount { get; set; } = Networking.TcpKeepAliveSettings.DefaultRetryCount; + } } diff --git a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptionsValidator.cs b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptionsValidator.cs index 08c25ae0..2667cbba 100644 --- a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptionsValidator.cs +++ b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerOptionsValidator.cs @@ -64,6 +64,7 @@ public ValidateOptionsResult Validate(string? name, BackFillerOptions options) ValidateShutdown(options, failures); ValidateListener(options, failures); ValidateNntp(options, failures); + ValidateTcpKeepAlive(options, failures); ValidateAccountRefresh(options, failures); ValidateArticleRetention(options, failures); ValidateSystemd(options, failures); @@ -308,6 +309,43 @@ private static void ValidateNntp(BackFillerOptions options, List failure ValidateBufferSize("BackFiller:Nntp:ReceiveBufferBytes", nntp.ReceiveBufferBytes, failures); } + /// + /// Appends failures when TCP keepalive timings are outside the ranges shared with + /// . + /// + /// Options whose section is checked. + /// Failure list to append. Not cleared. + private static void ValidateTcpKeepAlive(BackFillerOptions options, List failures) + { + var keepAlive = options.TcpKeepAlive; + if (keepAlive.TimeSeconds + is < TcpKeepAliveSettings.MinimumPositive + or > TcpKeepAliveSettings.MaximumTimeSeconds) + { + failures.Add( + "BackFiller:TcpKeepAlive:TimeSeconds must be between " + + $"{TcpKeepAliveSettings.MinimumPositive} and {TcpKeepAliveSettings.MaximumTimeSeconds}."); + } + + if (keepAlive.IntervalSeconds + is < TcpKeepAliveSettings.MinimumPositive + or > TcpKeepAliveSettings.MaximumIntervalSeconds) + { + failures.Add( + "BackFiller:TcpKeepAlive:IntervalSeconds must be between " + + $"{TcpKeepAliveSettings.MinimumPositive} and {TcpKeepAliveSettings.MaximumIntervalSeconds}."); + } + + if (keepAlive.RetryCount + is < TcpKeepAliveSettings.MinimumPositive + or > TcpKeepAliveSettings.MaximumRetryCount) + { + failures.Add( + "BackFiller:TcpKeepAlive:RetryCount must be between " + + $"{TcpKeepAliveSettings.MinimumPositive} and {TcpKeepAliveSettings.MaximumRetryCount}."); + } + } + /// Appends a failure when a TCP buffer limit is outside 16 KiB through 16 MiB. /// Configuration key included in the failure text. /// Requested buffer size. diff --git a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptions.cs b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptions.cs index 6d215399..399ba903 100644 --- a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptions.cs +++ b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptions.cs @@ -35,6 +35,7 @@ namespace VectorNNTPD.BackFillerService.Configuration /// NntpDB connection projection. is secret. /// MySQL account-refresh poll interval. /// Validated outbound NNTP provider session bounds. + /// Validated TCP keepalive timings for upstream and accepted listener sockets. internal sealed record BackFillerRuntimeOptions( int ServerId, BackFillerRuntimeDnsIdentity DnsIdentity, @@ -48,7 +49,8 @@ internal sealed record BackFillerRuntimeOptions( BackFillerRabbitMqRuntimeOptions RabbitMq, NntpDbRuntimeOptions NntpDb, TimeSpan AccountRefreshInterval, - BackFillerNntpRuntimeOptions Nntp) + BackFillerNntpRuntimeOptions Nntp, + Networking.TcpKeepAliveSettings TcpKeepAlive) { /// Canonical DNS suffix. Updated by . internal string DnsSuffix => DnsIdentity.DnsSuffix; diff --git a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptionsFactory.cs b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptionsFactory.cs index 52cfb20f..fe7ec643 100644 --- a/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptionsFactory.cs +++ b/src/VectorNNTPD.BackFillerService/Configuration/BackFillerRuntimeOptionsFactory.cs @@ -1,5 +1,6 @@ using MySqlConnector; using VectorNNTPD.BackFillerService.Acme; +using VectorNNTPD.BackFillerService.Networking; using VectorNNTPD.Common.Configuration; using VectorNNTPD.Common.Messaging.RabbitMq; using VectorNNTPD.Common.Acme; @@ -154,6 +155,7 @@ internal static BackFillerRuntimeOptions Create( var listener = options.Listener ?? throw new InvalidOperationException("BackFiller:Listener is required."); var shutdown = options.Shutdown ?? throw new InvalidOperationException("BackFiller:Shutdown is required."); var nntp = options.Nntp ?? throw new InvalidOperationException("BackFiller:Nntp is required."); + var tcpKeepAlive = options.TcpKeepAlive ?? throw new InvalidOperationException("BackFiller:TcpKeepAlive is required."); return new BackFillerRuntimeOptions( ServerId: serverId, @@ -193,7 +195,11 @@ internal static BackFillerRuntimeOptions Create( Nntp: new BackFillerNntpRuntimeOptions( nntp.MaxConcurrentSessionEstablishments, nntp.SendBufferBytes, - nntp.ReceiveBufferBytes)); + nntp.ReceiveBufferBytes), + TcpKeepAlive: new TcpKeepAliveSettings( + tcpKeepAlive.TimeSeconds, + tcpKeepAlive.IntervalSeconds, + tcpKeepAlive.RetryCount)); } /// Resolves the file-log directory, or returns empty when that directory is blank. diff --git a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs index 08abc322..6759766c 100644 --- a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs +++ b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs @@ -852,7 +852,8 @@ private bool TryConfigureAcceptedSocket(Socket accepted, string remote) TcpSocketOptions.Apply( accepted, _runtime.Listener.SendBufferBytes, - _runtime.Listener.ReceiveBufferBytes); + _runtime.Listener.ReceiveBufferBytes, + _runtime.TcpKeepAlive); } catch (Exception ex) when (ex is SocketException or ObjectDisposedException or ArgumentOutOfRangeException) { diff --git a/src/VectorNNTPD.BackFillerService/Networking/TcpKeepAliveSettings.cs b/src/VectorNNTPD.BackFillerService/Networking/TcpKeepAliveSettings.cs new file mode 100644 index 00000000..bf76cdc0 --- /dev/null +++ b/src/VectorNNTPD.BackFillerService/Networking/TcpKeepAliveSettings.cs @@ -0,0 +1,60 @@ +namespace VectorNNTPD.BackFillerService.Networking +{ + /// + /// TCP keepalive probe timings applied with SO_KEEPALIVE. + /// + /// + /// Defaults are chosen so dead-path detection begins and progresses before the default + /// NNTP CommandTimeout (30 s) and listener IoProgressTimeout (60 s): + /// idle , then up to probes + /// spaced by (about 25 s worst case to drop). + /// Values are seconds, matching the .NET TcpKeepAliveTime, TcpKeepAliveInterval, + /// and TcpKeepAliveRetryCount socket options. + /// + /// Idle seconds before the first keepalive probe. + /// Seconds between consecutive probes. + /// Number of unanswered probes before the stack drops the connection. + internal readonly record struct TcpKeepAliveSettings(int TimeSeconds, int IntervalSeconds, int RetryCount) + { + /// Default idle seconds before the first probe. + internal const int DefaultTimeSeconds = 10; + + /// Default seconds between probes. + internal const int DefaultIntervalSeconds = 5; + + /// Default unanswered probe count before the connection is dropped. + internal const int DefaultRetryCount = 3; + + /// Smallest accepted idle, interval, or retry value. + internal const int MinimumPositive = 1; + + /// Largest accepted idle time in seconds (two hours). + internal const int MaximumTimeSeconds = 7200; + + /// Largest accepted probe interval in seconds. + internal const int MaximumIntervalSeconds = 300; + + /// Largest accepted retry count. + internal const int MaximumRetryCount = 20; + + /// Production and configuration default timings. + internal static TcpKeepAliveSettings Default { get; } = new( + DefaultTimeSeconds, + DefaultIntervalSeconds, + DefaultRetryCount); + + /// + /// Throws when any field is outside the accepted ranges used by configuration validation. + /// + /// A field is outside its documented range. + internal void Validate() + { + ArgumentOutOfRangeException.ThrowIfLessThan(TimeSeconds, MinimumPositive); + ArgumentOutOfRangeException.ThrowIfGreaterThan(TimeSeconds, MaximumTimeSeconds); + ArgumentOutOfRangeException.ThrowIfLessThan(IntervalSeconds, MinimumPositive); + ArgumentOutOfRangeException.ThrowIfGreaterThan(IntervalSeconds, MaximumIntervalSeconds); + ArgumentOutOfRangeException.ThrowIfLessThan(RetryCount, MinimumPositive); + ArgumentOutOfRangeException.ThrowIfGreaterThan(RetryCount, MaximumRetryCount); + } + } +} diff --git a/src/VectorNNTPD.BackFillerService/Networking/TcpSocketOptions.cs b/src/VectorNNTPD.BackFillerService/Networking/TcpSocketOptions.cs index 77cc6326..c39b18fb 100644 --- a/src/VectorNNTPD.BackFillerService/Networking/TcpSocketOptions.cs +++ b/src/VectorNNTPD.BackFillerService/Networking/TcpSocketOptions.cs @@ -3,7 +3,8 @@ namespace VectorNNTPD.BackFillerService.Networking { /// - /// Applies and the TCP send and receive buffer limits to one connected socket. + /// Applies , TCP send and receive buffer limits, and TCP keepalive + /// to one connected or not-yet-connected socket. /// /// /// @@ -19,6 +20,13 @@ namespace VectorNNTPD.BackFillerService.Networking /// receive autotuning once is set. The managed /// NntpStreamReader buffer is a separate allocation and is not taken from these sizes. /// + /// + /// TCP keepalive is enabled independently of application-level NNTP DATE probes. + /// must succeed. Probe timings use + /// , , + /// and when the platform supports them; + /// an unsupported probe option leaves OS defaults in place and does not undo keepalive. + /// /// internal static class TcpSocketOptions { @@ -35,14 +43,18 @@ internal static class TcpSocketOptions internal const int MaximumBufferBytes = 16 * 1024 * 1024; /// - /// Sets , then the send buffer, then the receive buffer. + /// Sets , the send and receive buffers, and TCP keepalive. /// /// Connected or not-yet-connected TCP socket that will carry NNTP bytes. /// Requested . Must already be inside the validated range. /// Requested . Must already be inside the validated range. + /// Idle, interval, and retry settings. Validated before any option is written. /// is null. - /// Either size is outside through . - /// The operating system rejected one of the options. + /// + /// A buffer size is outside through , + /// or fails . + /// + /// The operating system rejected a required option. /// is disposed. /// /// Call this before the first application or TLS write on . @@ -50,17 +62,113 @@ internal static class TcpSocketOptions /// that will connect, so the handshake can advertise the receive window. A later call on the /// socket that connect actually used covers a runtime that replaced the socket during name resolution. /// - internal static void Apply(Socket socket, int sendBufferBytes, int receiveBufferBytes) + internal static void Apply( + Socket socket, + int sendBufferBytes, + int receiveBufferBytes, + TcpKeepAliveSettings keepAlive) { ArgumentNullException.ThrowIfNull(socket); ArgumentOutOfRangeException.ThrowIfLessThan(sendBufferBytes, MinimumBufferBytes); ArgumentOutOfRangeException.ThrowIfGreaterThan(sendBufferBytes, MaximumBufferBytes); ArgumentOutOfRangeException.ThrowIfLessThan(receiveBufferBytes, MinimumBufferBytes); ArgumentOutOfRangeException.ThrowIfGreaterThan(receiveBufferBytes, MaximumBufferBytes); + keepAlive.Validate(); socket.NoDelay = true; socket.SendBufferSize = sendBufferBytes; socket.ReceiveBufferSize = receiveBufferBytes; + ApplyKeepAlive(socket, keepAlive); } + + /// + /// Returns whether is enabled on . + /// + /// Socket to inspect. + /// when the option is non-zero. + internal static bool IsKeepAliveEnabled(Socket socket) + { + ArgumentNullException.ThrowIfNull(socket); + var value = socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive); + return value switch + { + bool enabled => enabled, + int flag => flag != 0, + _ => Convert.ToBoolean(value), + }; + } + + /// + /// Reads a TCP keepalive probe option when the platform exposes it. + /// + /// Configured socket. + /// + /// One of , + /// , or + /// . + /// + /// Option value in seconds or retry count when the read succeeds. + /// + /// when the option was read; when the platform + /// does not support reading that option (distinct from a failed required keepalive enable). + /// + /// + /// Thrown for unexpected socket errors that are not classified as unsupported options. + /// + internal static bool TryGetTcpKeepAliveProbeOption(Socket socket, SocketOptionName name, out int value) + { + ArgumentNullException.ThrowIfNull(socket); + try + { + var raw = socket.GetSocketOption(SocketOptionLevel.Tcp, name); + value = Convert.ToInt32(raw); + return true; + } + catch (SocketException ex) when (IsUnsupportedKeepAliveProbeError(ex)) + { + value = 0; + return false; + } + } + + /// Enables and best-effort probe timings. + /// Socket that already passed buffer and range checks. + /// Validated probe timings. + private static void ApplyKeepAlive(Socket socket, TcpKeepAliveSettings keepAlive) + { + socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); + TrySetTcpKeepAliveProbeOption(socket, SocketOptionName.TcpKeepAliveTime, keepAlive.TimeSeconds); + TrySetTcpKeepAliveProbeOption(socket, SocketOptionName.TcpKeepAliveInterval, keepAlive.IntervalSeconds); + TrySetTcpKeepAliveProbeOption(socket, SocketOptionName.TcpKeepAliveRetryCount, keepAlive.RetryCount); + } + + /// + /// Sets one TCP keepalive probe option, treating platform non-support as a skipped option. + /// + /// Socket with keepalive already enabled. + /// Probe option name. + /// Seconds or retry count. + /// Thrown when the failure is not classified as unsupported. + private static void TrySetTcpKeepAliveProbeOption(Socket socket, SocketOptionName name, int value) + { + try + { + socket.SetSocketOption(SocketOptionLevel.Tcp, name, value); + } + catch (SocketException ex) when (IsUnsupportedKeepAliveProbeError(ex)) + { + // SO_KEEPALIVE remains enabled; the stack keeps its probe defaults for this option. + } + } + + /// + /// True when indicates the TCP keepalive probe option is unavailable + /// on this platform, not that the socket is broken or the value is out of OS range after validation. + /// + /// Socket failure from set or get of a probe option. + private static bool IsUnsupportedKeepAliveProbeError(SocketException ex) => + ex.SocketErrorCode is SocketError.ProtocolOption + or SocketError.OperationNotSupported + or SocketError.InvalidArgument; } } diff --git a/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderRegistry.cs b/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderRegistry.cs index fd3c1ab5..7bafad7c 100644 --- a/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderRegistry.cs +++ b/src/VectorNNTPD.BackFillerService/Nntp/NntpProviderRegistry.cs @@ -85,6 +85,7 @@ NntpSessionOptions.Default with { TcpSendBufferBytes = runtime.Nntp.SendBufferBytes, TcpReceiveBufferBytes = runtime.Nntp.ReceiveBufferBytes, + TcpKeepAlive = runtime.TcpKeepAlive, }, runtime.Shutdown.GracePeriod, establishment, diff --git a/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionOptions.cs b/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionOptions.cs index c1306930..219e3a76 100644 --- a/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionOptions.cs +++ b/src/VectorNNTPD.BackFillerService/Nntp/NntpSessionOptions.cs @@ -1,3 +1,5 @@ +using VectorNNTPD.BackFillerService.Networking; + namespace VectorNNTPD.BackFillerService.Nntp { /// @@ -18,6 +20,9 @@ namespace VectorNNTPD.BackFillerService.Nntp /// /// Requested TCP receive-buffer limit for the upstream socket. Article bodies arrive on this window. /// + /// + /// TCP keepalive timings for each upstream socket. Independent of provider MySQL keepalive (DATE). + /// /// /// Optional TLS certificate callback. uses platform validation /// (production default). Tests may supply a callback; this is not a configuration setting. @@ -31,11 +36,12 @@ internal sealed record NntpSessionOptions( TimeSpan ReceiveTimeout, int TcpSendBufferBytes, int TcpReceiveBufferBytes, + TcpKeepAliveSettings TcpKeepAlive = default, System.Net.Security.RemoteCertificateValidationCallback? ServerCertificateValidationCallback = null) { /// /// Reader buffer 64 KiB, TCP send 1 MiB, TCP receive 2 MiB, 16 KiB status lines, 5 MiB articles, - /// 30 s connect/command, 2 min receive. + /// 30 s connect/command, 2 min receive, and keepalive. /// /// /// The TCP sizes match defaults. @@ -50,6 +56,18 @@ internal sealed record NntpSessionOptions( CommandTimeout: TimeSpan.FromSeconds(30), ReceiveTimeout: TimeSpan.FromMinutes(2), TcpSendBufferBytes: Configuration.BackFillerNntpOptions.DefaultSendBufferBytes, - TcpReceiveBufferBytes: Configuration.BackFillerNntpOptions.DefaultReceiveBufferBytes); + TcpReceiveBufferBytes: Configuration.BackFillerNntpOptions.DefaultReceiveBufferBytes, + TcpKeepAlive: TcpKeepAliveSettings.Default); + + /// + /// Effective keepalive timings. A default-constructed option (all zeros) resolves to + /// so older test constructors stay valid. + /// + internal TcpKeepAliveSettings EffectiveTcpKeepAlive => + TcpKeepAlive.TimeSeconds == 0 + && TcpKeepAlive.IntervalSeconds == 0 + && TcpKeepAlive.RetryCount == 0 + ? TcpKeepAliveSettings.Default + : TcpKeepAlive; } } diff --git a/src/VectorNNTPD.BackFillerService/Nntp/TcpNntpTransportFactory.cs b/src/VectorNNTPD.BackFillerService/Nntp/TcpNntpTransportFactory.cs index 5887e3d4..eea18e76 100644 --- a/src/VectorNNTPD.BackFillerService/Nntp/TcpNntpTransportFactory.cs +++ b/src/VectorNNTPD.BackFillerService/Nntp/TcpNntpTransportFactory.cs @@ -27,6 +27,8 @@ internal sealed class TcpNntpTransportFactory : INntpTransportFactory /// Send and receive limits come from and /// . /// is the managed reader size and is not copied onto the socket. Nagle is disabled. + /// TCP keepalive uses and does not consult + /// . /// The TCP connect uses linked with . /// Implicit TLS then gets another full , linked only with . /// Any failure, including , disposes the client and rethrows. @@ -45,13 +47,22 @@ public async Task ConnectAsync( try { - TcpSocketOptions.Apply(client.Client, options.TcpSendBufferBytes, options.TcpReceiveBufferBytes); + var keepAlive = options.EffectiveTcpKeepAlive; + TcpSocketOptions.Apply( + client.Client, + options.TcpSendBufferBytes, + options.TcpReceiveBufferBytes, + keepAlive); using var connectCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); connectCts.CancelAfter(options.ConnectTimeout); await client.ConnectAsync(provider.Host, provider.Port, connectCts.Token).ConfigureAwait(false); // ConnectAsync(string) can replace the socket when the resolved address family differs. // Re-apply on the connected socket before the first TLS or NNTP byte. - TcpSocketOptions.Apply(client.Client, options.TcpSendBufferBytes, options.TcpReceiveBufferBytes); + TcpSocketOptions.Apply( + client.Client, + options.TcpSendBufferBytes, + options.TcpReceiveBufferBytes, + keepAlive); Stream stream = client.GetStream(); if (!provider.UseTls) { diff --git a/src/VectorNNTPD.BackFillerService/VectorNNTPD.BackFillerService.json b/src/VectorNNTPD.BackFillerService/VectorNNTPD.BackFillerService.json index f549d3a8..8d57e2c6 100644 --- a/src/VectorNNTPD.BackFillerService/VectorNNTPD.BackFillerService.json +++ b/src/VectorNNTPD.BackFillerService/VectorNNTPD.BackFillerService.json @@ -28,6 +28,11 @@ "ReceiveBufferBytes": 2097152, "SendBufferBytes": 1048576 }, + "TcpKeepAlive": { + "TimeSeconds": 10, + "IntervalSeconds": 5, + "RetryCount": 3 + }, "Logging": { "LogLevel": "Information", "LogRetentionDays": 14, diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Configuration/BackFillerOptionsValidatorTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Configuration/BackFillerOptionsValidatorTests.cs index 3d3885c0..8b3c33d7 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Configuration/BackFillerOptionsValidatorTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Configuration/BackFillerOptionsValidatorTests.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Options; using VectorNNTPD.BackFillerService.Configuration; using VectorNNTPD.BackFillerService.Hosting; +using VectorNNTPD.BackFillerService.Networking; using VectorNNTPD.BackFillerService.Nntp; using VectorNNTPD.BackFillerService.Tests.Fixtures; using VectorNNTPD.Common.Configuration; @@ -533,6 +534,45 @@ public void Validate_uses_independent_default_tcp_buffers_when_omitted() Assert.True(result.Succeeded); } + /// Issue #191: omitted TcpKeepAlive keys keep the documented defaults. + [Fact] + public void Validate_uses_default_tcp_keepalive_when_omitted() + { + var options = BackFillerTestOptions.CreateValid(); + Assert.Equal(TcpKeepAliveSettings.DefaultTimeSeconds, options.TcpKeepAlive.TimeSeconds); + Assert.Equal(TcpKeepAliveSettings.DefaultIntervalSeconds, options.TcpKeepAlive.IntervalSeconds); + Assert.Equal(TcpKeepAliveSettings.DefaultRetryCount, options.TcpKeepAlive.RetryCount); + + var result = BackFillerTestOptions.CreateValidator().Validate(null, options); + Assert.True(result.Succeeded); + + var runtime = BackFillerRuntimeOptionsFactory.Create( + options, + BackFillerTestOptions.CreateValidNntpDb()); + Assert.Equal(TcpKeepAliveSettings.Default, runtime.TcpKeepAlive); + } + + /// Issue #191: out-of-range TcpKeepAlive values fail validation. + [Fact] + public void Validate_rejects_out_of_range_tcp_keepalive() + { + var options = BackFillerTestOptions.CreateValid(); + options.TcpKeepAlive.TimeSeconds = 0; + options.TcpKeepAlive.IntervalSeconds = 0; + options.TcpKeepAlive.RetryCount = 0; + var result = BackFillerTestOptions.CreateValidator().Validate(null, options); + Assert.False(result.Succeeded); + Assert.Contains( + result.Failures!, + static f => f.Contains("BackFiller:TcpKeepAlive:TimeSeconds", StringComparison.Ordinal)); + Assert.Contains( + result.Failures!, + static f => f.Contains("BackFiller:TcpKeepAlive:IntervalSeconds", StringComparison.Ordinal)); + Assert.Contains( + result.Failures!, + static f => f.Contains("BackFiller:TcpKeepAlive:RetryCount", StringComparison.Ordinal)); + } + /// Runtime snapshot copies each buffer without coupling send to receive or upstream to the listener. [Fact] public void Runtime_options_project_independent_tcp_buffers() @@ -544,6 +584,9 @@ public void Runtime_options_project_independent_tcp_buffers() options.Listener.ReceiveBufferBytes = 256 * 1024; options.Nntp.MaxConcurrentSessionEstablishments = BackFillerNntpOptions.DefaultMaxConcurrentSessionEstablishments; options.Listener.MaxActiveConnections = 1024; + options.TcpKeepAlive.TimeSeconds = 11; + options.TcpKeepAlive.IntervalSeconds = 4; + options.TcpKeepAlive.RetryCount = 5; var runtime = BackFillerRuntimeOptionsFactory.Create( options, @@ -554,6 +597,7 @@ public void Runtime_options_project_independent_tcp_buffers() Assert.Equal(128 * 1024, runtime.Listener.SendBufferBytes); Assert.Equal(256 * 1024, runtime.Listener.ReceiveBufferBytes); Assert.Equal(1024, runtime.Listener.MaxActiveConnections); + Assert.Equal(new TcpKeepAliveSettings(11, 4, 5), runtime.TcpKeepAlive); Assert.Equal( BackFillerNntpOptions.DefaultMaxConcurrentSessionEstablishments, runtime.Nntp.MaxConcurrentSessionEstablishments); diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Listener/CacheListenerServiceTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Listener/CacheListenerServiceTests.cs index 6ccf1b78..2596d6e0 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Listener/CacheListenerServiceTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Listener/CacheListenerServiceTests.cs @@ -1060,7 +1060,7 @@ private static BackFillerRuntimeOptions CreateRuntime(int port, int maxConnectio } /// - /// Asserts Nagle is off and that the OS-reported buffers are at least the requested sizes and are not the same value. + /// Asserts Nagle is off, TCP keepalive is on, and the OS-reported buffers are at least the requested sizes and are not the same value. /// /// Accepted socket under test. /// Requested send-buffer limit. @@ -1068,6 +1068,7 @@ private static BackFillerRuntimeOptions CreateRuntime(int port, int maxConnectio private static void AssertRequestedBuffers(Socket socket, int send, int receive) { Assert.True(socket.NoDelay); + Assert.True(TcpSocketOptions.IsKeepAliveEnabled(socket)); Assert.True(socket.SendBufferSize >= send, $"send buffer {socket.SendBufferSize} is below {send}"); Assert.True(socket.ReceiveBufferSize >= receive, $"receive buffer {socket.ReceiveBufferSize} is below {receive}"); Assert.NotEqual(socket.SendBufferSize, socket.ReceiveBufferSize); diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Networking/TcpSocketOptionsTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Networking/TcpSocketOptionsTests.cs index 8cbd7c21..bba2a4b2 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Networking/TcpSocketOptionsTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Networking/TcpSocketOptionsTests.cs @@ -7,6 +7,47 @@ namespace VectorNNTPD.BackFillerService.Tests.Networking /// Applies TCP options on real accepted sockets before any application write. public sealed class TcpSocketOptionsTests { + /// Issue #191: keepalive and probe timings are set on an accepted socket. + [Fact] + public async Task Apply_enables_keepalive_and_probe_timings_when_supported() + { + using var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + using var client = new TcpClient(); + await client.ConnectAsync(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port); + using var accepted = await listener.AcceptSocketAsync(); + var settings = new TcpKeepAliveSettings(12, 4, 6); + + TcpSocketOptions.Apply(accepted, 128 * 1024, 256 * 1024, settings); + + Assert.True(TcpSocketOptions.IsKeepAliveEnabled(accepted)); + AssertProbeOption(accepted, SocketOptionName.TcpKeepAliveTime, settings.TimeSeconds); + AssertProbeOption(accepted, SocketOptionName.TcpKeepAliveInterval, settings.IntervalSeconds); + AssertProbeOption(accepted, SocketOptionName.TcpKeepAliveRetryCount, settings.RetryCount); + } + + /// Out-of-range keepalive settings fail before the socket is mutated. + [Fact] + public async Task Apply_rejects_invalid_keepalive_settings_before_mutating_the_socket() + { + using var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + using var client = new TcpClient(); + await client.ConnectAsync(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port); + using var accepted = await listener.AcceptSocketAsync(); + Assert.False(accepted.NoDelay); + Assert.False(TcpSocketOptions.IsKeepAliveEnabled(accepted)); + + Assert.Throws( + () => TcpSocketOptions.Apply( + accepted, + 128 * 1024, + 256 * 1024, + new TcpKeepAliveSettings(0, 5, 3))); + Assert.False(accepted.NoDelay); + Assert.False(TcpSocketOptions.IsKeepAliveEnabled(accepted)); + } + /// An IPv4 accepted socket gets independent buffers and Nagle off before the first byte. [Fact] public async Task Apply_configures_an_ipv4_accepted_socket_before_any_write() @@ -48,8 +89,8 @@ await Task.WhenAll( using var first = await listener.AcceptSocketAsync(); using var second = await listener.AcceptSocketAsync(); - TcpSocketOptions.Apply(first, 128 * 1024, 256 * 1024); - TcpSocketOptions.Apply(second, 64 * 1024, 512 * 1024); + TcpSocketOptions.Apply(first, 128 * 1024, 256 * 1024, TcpKeepAliveSettings.Default); + TcpSocketOptions.Apply(second, 64 * 1024, 512 * 1024, TcpKeepAliveSettings.Default); AssertRequested(first, 128 * 1024, 256 * 1024); AssertRequested(second, 64 * 1024, 512 * 1024); @@ -68,11 +109,13 @@ public async Task Apply_rejects_an_invalid_size_and_a_disposed_socket() using var accepted = await listener.AcceptSocketAsync(); Assert.False(accepted.NoDelay); - Assert.Throws(() => TcpSocketOptions.Apply(accepted, 1024, 128 * 1024)); + Assert.Throws( + () => TcpSocketOptions.Apply(accepted, 1024, 128 * 1024, TcpKeepAliveSettings.Default)); Assert.False(accepted.NoDelay); accepted.Dispose(); - Assert.Throws(() => TcpSocketOptions.Apply(accepted, 128 * 1024, 256 * 1024)); + Assert.Throws( + () => TcpSocketOptions.Apply(accepted, 128 * 1024, 256 * 1024, TcpKeepAliveSettings.Default)); } /// Connects one client, applies distinct buffers, checks them, then writes one byte. @@ -88,8 +131,9 @@ private static async Task AssertAcceptedSocketAsync(AddressFamily family, IPAddr using var accepted = await listen.AcceptAsync(); Assert.False(accepted.NoDelay); - TcpSocketOptions.Apply(accepted, 128 * 1024, 256 * 1024); + TcpSocketOptions.Apply(accepted, 128 * 1024, 256 * 1024, TcpKeepAliveSettings.Default); AssertRequested(accepted, 128 * 1024, 256 * 1024); + Assert.True(TcpSocketOptions.IsKeepAliveEnabled(accepted)); await accepted.SendAsync(new byte[] { 9 }); var received = new byte[1]; @@ -109,5 +153,16 @@ private static void AssertRequested(Socket socket, int send, int receive) Assert.True(socket.ReceiveBufferSize >= receive, $"receive buffer {socket.ReceiveBufferSize} is below {receive}"); Assert.NotEqual(socket.SendBufferSize, socket.ReceiveBufferSize); } + + /// + /// Asserts a probe option when the platform can read it; otherwise requires only that the read was classified unsupported. + /// + private static void AssertProbeOption(Socket socket, SocketOptionName name, int expected) + { + if (TcpSocketOptions.TryGetTcpKeepAliveProbeOption(socket, name, out var actual)) + { + Assert.Equal(expected, actual); + } + } } } diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/TcpNntpTransportFactoryTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/TcpNntpTransportFactoryTests.cs index b3f41728..b9aa1e7d 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/Nntp/TcpNntpTransportFactoryTests.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/Nntp/TcpNntpTransportFactoryTests.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Sockets; using System.Reflection; +using VectorNNTPD.BackFillerService.Networking; using VectorNNTPD.BackFillerService.Nntp; namespace VectorNNTPD.BackFillerService.Tests.Nntp @@ -9,7 +10,7 @@ namespace VectorNNTPD.BackFillerService.Tests.Nntp public sealed class TcpNntpTransportFactoryTests { /// - /// The connected socket has independent buffers and Nagle off before the first write, and the write still completes. + /// The connected socket has independent buffers, Nagle off, and TCP keepalive before the first write. /// [Fact] public async Task Connect_applies_independent_buffers_and_no_delay_before_the_first_write() @@ -33,6 +34,7 @@ public async Task Connect_applies_independent_buffers_and_no_delay_before_the_fi Assert.True(client.Client.ReceiveBufferSize >= 128 * 1024); Assert.NotEqual(client.Client.SendBufferSize, client.Client.ReceiveBufferSize); Assert.Equal(64 * 1024, options.ReceiveBufferBytes); + Assert.True(TcpSocketOptions.IsKeepAliveEnabled(client.Client)); await stream.WriteAsync(new byte[] { 7 }); var received = new byte[1]; @@ -41,6 +43,46 @@ public async Task Connect_applies_independent_buffers_and_no_delay_before_the_fi Assert.True(client.Client.NoDelay); } + /// + /// Issue #191: TCP keepalive is enabled even when provider DATE keepalive is disabled. + /// + [Fact] + public async Task Connect_enables_tcp_keepalive_when_provider_date_keepalive_is_disabled() + { + using var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + var port = ((IPEndPoint)listener.LocalEndpoint).Port; + var accepted = listener.AcceptTcpClientAsync(); + var factory = new TcpNntpTransportFactory(); + var provider = new BackFillerProviderDefinition( + "loopback", + "127.0.0.1", + port, + false, + null, + null, + 0, + 1, + KeepAliveSeconds: 0); + Assert.False(provider.DateKeepAliveEnabled); + + await using var stream = await factory.ConnectAsync( + provider, + NntpSessionOptions.Default, + CancellationToken.None); + using var server = await accepted; + var client = ClientOf(stream); + + Assert.True(TcpSocketOptions.IsKeepAliveEnabled(client.Client)); + if (TcpSocketOptions.TryGetTcpKeepAliveProbeOption( + client.Client, + SocketOptionName.TcpKeepAliveTime, + out var time)) + { + Assert.Equal(TcpKeepAliveSettings.DefaultTimeSeconds, time); + } + } + /// A second connect gets its own configured socket after the first stream is disposed. [Fact] public async Task Repeated_connect_configures_each_socket() @@ -65,6 +107,7 @@ public async Task Repeated_connect_configures_each_socket() Assert.True(client.Client.SendBufferSize >= 64 * 1024); Assert.True(client.Client.ReceiveBufferSize >= 256 * 1024); Assert.NotEqual(client.Client.SendBufferSize, client.Client.ReceiveBufferSize); + Assert.True(TcpSocketOptions.IsKeepAliveEnabled(client.Client)); } }