From 569fe6d59c5993ed0ec014c8801f4240d593aaf2 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Thu, 29 Oct 2026 19:57:51 +0200 Subject: [PATCH 1/6] Consolidate NNTP listening transport in Common - Extract shared high-performance pipeline listener and connection transport into Common - Migrate Reader, Transit, Storage, and BackFiller listeners to the shared implementation - Fix pipeline stream read, write, and cancellation handling - Improve QUIT completion and command failure logging - Add transport regression tests and update performance documentation - Preserve byte-sensitive INN test fixtures --- .gitattributes | 3 + CONTRIBUTING.md | 117 +- PERFORMANCE.md | 1220 +++++++++++++ README.md | 7 +- docs/README.md | 61 +- docs/common/README.md | 5 +- docs/nntpd-reader-transit.md | 27 +- docs/schema/README.md | 25 + .../CacheListenerAcceptFailureObserver.cs | 31 + .../Listener/CacheListenerEndpoints.cs | 53 - .../Listener/CacheListenerService.cs | 357 ++-- .../Diagnostics/ITcpByteDiagnostics.cs | 18 + .../Listeners/ConnectionAcceptanceLogging.cs | 4 +- .../Listeners/IListenAcceptFailureObserver.cs | 23 + .../Listeners/IListenSocketBinder.cs | 2 +- .../Listeners/SocketAcceptListener.cs | 158 +- .../Proxy/ConnectionClientIdentity.cs | 2 +- .../Networking/Proxy/PrefixedStream.cs | 2 +- .../RateLimiting/IOutboundRateCap.cs | 2 +- .../RateLimiting/OutboundRateLimiter.cs | 2 +- .../Transport/ConnectionByteTransport.cs | 4 +- .../Networking/Transport/INntpConnection.cs | 6 +- .../Networking/Transport/NntpConnection.cs | 18 +- .../Transport/NntpConnectionStream.cs | 240 +++ .../Networking/Transport/NntpDeflateStream.cs | 2 +- .../Networking/Transport/NntpPipeOptions.cs | 2 +- .../Transport/SocketPayloadSender.cs | 2 +- .../Transport/TcpDisconnectReason.cs | 2 +- .../Transport/TlsNegotiationLogging.cs | 2 +- .../Networking/Transport/TransportIoProbe.cs | 2 +- .../Networking/Transport/TransportIoReport.cs | 2 +- .../Transport/TransportIoSession.cs | 2 +- .../Transport/TransportLogMessages.cs | 2 +- .../Transport/TxPipePauseExperiment.cs | 2 +- .../Networking/Transport/TxWriteCoalescer.cs | 2 +- .../Transport/TxWriteGranularityExperiment.cs | 2 +- .../VectorNNTPD.Common.csproj | 2 +- .../ArticleIngestion/ArticleRecordIngress.cs | 2 +- .../ArticleIngestion/InboundArticle.cs | 2 +- .../Authentication/NntpAccountPolicy.cs | 2 +- .../Diagnostics/FeedDiagnosticsService.cs | 2 +- .../Diagnostics/IFeedDiagnostics.cs | 12 +- .../NntpdServiceCollectionExtensions.cs | 5 +- .../Moderation/ModerationSubmission.cs | 2 +- .../Listeners/NntpPlainListenerService.cs | 6 +- .../Listeners/NntpTlsListenerService.cs | 6 +- .../Listeners/SocketAcceptListener.cs | 217 --- .../Networking/Proxy/ProxyPreambleResolver.cs | 2 +- .../Networking/Proxy/ProxyProtocolParser.cs | 2 +- .../PostFilter/IPostFilter.cs | 2 +- .../CommandProcessor/NntpCommandContext.cs | 9 +- .../CommandProcessor/NntpCommandExecution.cs | 7 +- .../CommandProcessor/NntpPeerDisconnect.cs | 2 +- .../Commands/Posting/PostHeaderNormalizer.cs | 2 +- .../Posting/StreamingPostReadResult.cs | 2 +- .../Session/Commands/Quit.cs | 8 + .../Session/Commands/StartTls.cs | 4 +- .../Session/NntpSession.cs | 7 +- .../DistributedSessionStateTracker.cs | 2 +- .../InMemorySessionStateTracker.cs | 2 +- .../RateLimiting/AccountRateAllocator.cs | 2 + .../RateLimiting/IAccountRateAllocator.cs | 2 + .../Transit/TransitConnectionAdmission.cs | 2 +- .../Listener/StorageNntpListenerService.cs | 231 ++- .../ArticleIngestion/ArticleRecordIngress.cs | 2 +- .../ArticleIngestion/InboundArticle.cs | 2 +- .../Authentication/NntpAccountPolicy.cs | 2 +- .../Diagnostics/FeedDiagnosticsService.cs | 2 +- .../Diagnostics/IFeedDiagnostics.cs | 12 +- .../NntpdServiceCollectionExtensions.cs | 5 +- .../Moderation/ModerationSubmission.cs | 2 +- .../Listeners/ConnectionAcceptanceLogging.cs | 94 - .../Listeners/IListenSocketBinder.cs | 41 - .../Listeners/NntpPlainListenerService.cs | 6 +- .../Listeners/NntpTlsListenerService.cs | 6 +- .../Proxy/ConnectionClientIdentity.cs | 94 - .../Networking/Proxy/PrefixedStream.cs | 137 -- .../Networking/Proxy/ProxyPreambleResolver.cs | 2 +- .../Networking/Proxy/ProxyProtocolParser.cs | 2 +- .../Transport/ConnectionByteTransport.cs | 748 -------- .../Networking/Transport/INntpConnection.cs | 187 -- .../Networking/Transport/NntpConnection.cs | 1522 ----------------- .../Networking/Transport/NntpDeflateStream.cs | 148 -- .../Networking/Transport/NntpPipeOptions.cs | 57 - .../Transport/SocketPayloadSender.cs | 62 - .../Transport/TcpDisconnectReason.cs | 39 - .../Transport/TlsNegotiationLogging.cs | 33 - .../Networking/Transport/TransportIoProbe.cs | 87 - .../Networking/Transport/TransportIoReport.cs | 294 ---- .../Transport/TransportIoSession.cs | 680 -------- .../Transport/TransportLogMessages.cs | 32 - .../Transport/TxPipePauseExperiment.cs | 207 --- .../Networking/Transport/TxWriteCoalescer.cs | 70 - .../Transport/TxWriteGranularityExperiment.cs | 234 --- .../PostFilter/IPostFilter.cs | 2 +- .../CommandProcessor/NntpCommandContext.cs | 9 +- .../CommandProcessor/NntpCommandExecution.cs | 7 +- .../CommandProcessor/NntpPeerDisconnect.cs | 2 +- .../Commands/Posting/PostHeaderNormalizer.cs | 2 +- .../Posting/StreamingPostReadResult.cs | 2 +- .../Session/Commands/Quit.cs | 8 + .../Session/Commands/StartTls.cs | 4 +- .../Session/NntpSession.cs | 7 +- .../DistributedSessionStateTracker.cs | 2 +- .../InMemorySessionStateTracker.cs | 2 +- .../RateLimiting/AccountRateAllocator.cs | 2 + .../RateLimiting/IAccountRateAllocator.cs | 2 + .../RateLimiting/IOutboundRateCap.cs | 15 - .../RateLimiting/OutboundRateLimiter.cs | 199 --- .../Transit/TransitConnectionAdmission.cs | 2 +- .../Transport/NntpConnectionStreamTests.cs | 288 ++++ ...AcceptListenerAndPipelineTransportTests.cs | 221 +++ .../AccountByteExhaustionTests.cs | 7 +- .../ArticleRecordIngressTests.cs | 2 +- .../IhaveArticleInterpreterTests.cs | 2 +- .../IncomingSpoolNewsLogTests.cs | 2 +- .../IncomingSpoolPathSurveyTests.cs | 2 +- .../AccountPolicyMatrixTests.cs | 4 +- .../AuthenticatedSessionTeardownTests.cs | 2 +- .../NntpReaderAuthenticationTests.cs | 4 +- .../Fixtures/CanonicalArticleText.cs | 2 +- .../EmailModerationSubmissionServiceTests.cs | 2 +- .../ConnectionAcceptanceLoggingTests.cs | 5 +- .../Networking/Proxy/ProxyProtocolTests.cs | 4 +- .../ConnectionByteTransportQuiescenceTests.cs | 14 +- .../Transport/DeflateUpgradeTransportTests.cs | 2 +- ...NntpConnectionInputReaderOwnershipTests.cs | 2 +- ...NntpConnectionInputWriterOwnershipTests.cs | 2 +- ...ntpConnectionOutputReaderOwnershipTests.cs | 2 +- .../Transport/NntpDeflateStreamTests.cs | 18 +- .../Transport/NntpTransportTests.cs | 2 +- .../Transport/SendPipeAdvancementTests.cs | 2 +- .../Transport/SocketPayloadSenderTests.cs | 2 +- .../TcpConnectionDisconnectLoggingTests.cs | 4 +- .../Transport/TlsUpgradeTransportTests.cs | 4 +- .../Transport/TransportIoSessionTests.cs | 2 +- .../Transport/TxPipePauseExperimentTests.cs | 2 +- .../Transport/TxWriteCoalescerTests.cs | 2 +- .../TxWriteGranularityConnectionTests.cs | 2 +- .../TxWriteGranularityExperimentTests.cs | 2 +- .../PostFilter/PostFilterEvaluatorTests.cs | 2 +- .../PostFilterLiveClusterQuotaTests.cs | 4 +- .../PostFilter/PostFilterPostCommandTests.cs | 4 +- .../PostFilter/PostFilterPostHarness.cs | 4 +- .../PostFilterSpamAssassinEligibilityTests.cs | 2 +- .../Session/ArticleRetrievalCommandTests.cs | 4 +- .../ArticleRetrievalVatpCommandTests.cs | 4 +- .../Session/BenchItCommandTests.cs | 4 +- .../Commands/NntpPhaseCResponseTests.cs | 4 +- .../Session/GroupCommandTests.cs | 4 +- .../Session/HelpCommandTests.cs | 4 +- .../Session/IHaveCommandTests.cs | 4 +- .../Session/ListCommandTests.cs | 4 +- .../Session/ModeratedPostCommandTests.cs | 4 +- .../NewNewsAndNewGroupsRemovedTests.cs | 4 +- .../Session/NntpAuthinfoTests.cs | 4 +- .../Session/NntpCommandInventoryTests.cs | 4 +- .../Session/NntpCommandLineLengthTests.cs | 2 +- .../Session/NntpCommandLoggingTests.cs | 4 +- .../Session/NntpCommandParserTests.cs | 4 +- .../NntpCommandTxStatusLoggingTests.cs | 4 +- .../Session/NntpCompressTests.cs | 8 +- .../Session/NntpSessionFoundationTests.cs | 4 +- .../Session/NntpSessionIdleTimeoutTests.cs | 4 +- .../NntpSessionInputReaderLifecycleTests.cs | 2 +- .../Session/NntpStartTlsTests.cs | 2 +- .../Session/PostArticleValidationTests.cs | 2 +- .../Session/PostCommandTests.cs | 4 +- .../Session/PostNewsgroupCatalogueTests.cs | 4 +- .../Session/QuitCommandTests.cs | 10 +- .../StreamingPostArticleReaderTests.cs | 2 +- .../AccountRateAllocatorInvariantTests.cs | 4 +- .../RateLimiting/AccountRateFormulaTests.cs | 4 +- .../RateLimiting/OutboundRateLimiterTests.cs | 2 +- .../SessionStateRateLimitingRedisTests.cs | 4 +- .../SessionStateRateLimitingTests.cs | 4 +- .../SessionStateBytesAccountingTests.cs | 2 +- .../Transit/TransitAuthinfoAndCommandTests.cs | 4 +- .../TransitDeferOnDuplicateCommandTests.cs | 4 +- .../Transit/TransitHotReloadTests.cs | 4 +- .../TransitPeerIdentificationAndLimitTests.cs | 4 +- .../TransitPeerStateLifecycleTests.cs | 4 +- .../TransitPeerStateTrackerTests.cs | 4 +- ...sitReaderAdmissionReclassificationTests.cs | 4 +- .../TransitReceivePolicyCommandTests.cs | 4 +- .../TransitReceiveSizeAccountingTests.cs | 4 +- .../ArticleRecordIngressTests.cs | 2 +- .../IhaveArticleInterpreterTests.cs | 2 +- .../IncomingSpoolNewsLogTests.cs | 2 +- .../IncomingSpoolPathSurveyTests.cs | 2 +- .../Diagnostics/FeedDiagnosticsTests.cs | 4 +- .../Fixtures/CanonicalArticleText.cs | 2 +- .../History/HistoryCheckCommandTests.cs | 4 +- .../EmailModerationSubmissionServiceTests.cs | 2 +- .../ConnectionAcceptanceLoggingTests.cs | 5 +- .../Networking/Proxy/ProxyProtocolTests.cs | 4 +- .../ConnectionByteTransportQuiescenceTests.cs | 14 +- .../Transport/DeflateUpgradeTransportTests.cs | 2 +- ...NntpConnectionInputReaderOwnershipTests.cs | 2 +- ...NntpConnectionInputWriterOwnershipTests.cs | 2 +- ...ntpConnectionOutputReaderOwnershipTests.cs | 2 +- .../Transport/NntpDeflateStreamTests.cs | 18 +- .../Transport/NntpTransportTests.cs | 2 +- .../Transport/SendPipeAdvancementTests.cs | 2 +- .../Transport/SocketPayloadSenderTests.cs | 2 +- .../TcpConnectionDisconnectLoggingTests.cs | 4 +- .../Transport/TlsUpgradeTransportTests.cs | 4 +- .../Transport/TransportIoSessionTests.cs | 2 +- .../Transport/TxPipePauseExperimentTests.cs | 2 +- .../Transport/TxWriteCoalescerTests.cs | 2 +- .../TxWriteGranularityConnectionTests.cs | 2 +- .../TxWriteGranularityExperimentTests.cs | 2 +- .../Session/BenchItCommandTests.cs | 4 +- .../Session/CheckCommandTests.cs | 4 +- .../Session/CheckPipelineTcpTests.cs | 4 +- .../Session/CheckPipelineTests.cs | 4 +- .../Commands/NntpPhaseCResponseTests.cs | 4 +- .../Session/HelpCommandTests.cs | 4 +- .../Session/IHaveCommandTests.cs | 4 +- .../Session/IHaveTcpTests.cs | 2 +- .../NewNewsAndNewGroupsRemovedTests.cs | 4 +- .../Session/NntpAuthinfoTests.cs | 4 +- .../Session/NntpCommandInventoryTests.cs | 4 +- .../Session/NntpCommandLineLengthTests.cs | 2 +- .../Session/NntpCommandLoggingTests.cs | 4 +- .../Session/NntpCommandParserTests.cs | 4 +- .../NntpCommandTxStatusLoggingTests.cs | 4 +- .../Session/NntpCompressTests.cs | 8 +- .../Session/NntpSessionFoundationTests.cs | 4 +- .../Session/NntpSessionIdleTimeoutTests.cs | 4 +- .../NntpSessionInputReaderLifecycleTests.cs | 2 +- .../Session/NntpStartTlsTests.cs | 2 +- .../Session/PostArticleValidationTests.cs | 2 +- .../Session/QuitCommandTests.cs | 10 +- .../SpeedTest/SpeedTestCommandTests.cs | 4 +- .../StreamingPostArticleReaderTests.cs | 2 +- .../Session/TakeThisCommandTests.cs | 4 +- .../Session/TakeThisPipelineTests.cs | 4 +- .../Session/TransitPeerAuthorizationTests.cs | 4 +- .../TransitReaderCommandIsolationTests.cs | 4 +- .../Telemetry/ApplicationTelemetryTests.cs | 4 +- .../Transit/TransitAuthinfoAndCommandTests.cs | 4 +- .../TransitDeferOnDuplicateCommandTests.cs | 4 +- .../Transit/TransitHotReloadTests.cs | 4 +- .../TransitPeerIdentificationAndLimitTests.cs | 4 +- .../TransitPeerStateLifecycleTests.cs | 4 +- .../TransitPeerStateTrackerTests.cs | 4 +- ...sitReaderAdmissionReclassificationTests.cs | 4 +- .../TransitReceivePolicyCommandTests.cs | 4 +- .../TransitReceiveSizeAccountingTests.cs | 4 +- 250 files changed, 3041 insertions(+), 6022 deletions(-) create mode 100644 PERFORMANCE.md create mode 100644 docs/schema/README.md create mode 100644 src/VectorNNTPD.BackFillerService/Listener/CacheListenerAcceptFailureObserver.cs delete mode 100644 src/VectorNNTPD.BackFillerService/Listener/CacheListenerEndpoints.cs create mode 100644 src/VectorNNTPD.Common/Networking/Diagnostics/ITcpByteDiagnostics.cs rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Listeners/ConnectionAcceptanceLogging.cs (96%) create mode 100644 src/VectorNNTPD.Common/Networking/Listeners/IListenAcceptFailureObserver.cs rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Listeners/IListenSocketBinder.cs (96%) rename src/{VectorNNTPD.TransitService => VectorNNTPD.Common}/Networking/Listeners/SocketAcceptListener.cs (51%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Proxy/ConnectionClientIdentity.cs (98%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Proxy/PrefixedStream.cs (98%) rename src/{VectorNNTPD.ReaderService/SessionState => VectorNNTPD.Common/Networking}/RateLimiting/IOutboundRateCap.cs (89%) rename src/{VectorNNTPD.ReaderService/SessionState => VectorNNTPD.Common/Networking}/RateLimiting/OutboundRateLimiter.cs (99%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/ConnectionByteTransport.cs (99%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/INntpConnection.cs (98%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/NntpConnection.cs (99%) create mode 100644 src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/NntpDeflateStream.cs (99%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/NntpPipeOptions.cs (97%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/SocketPayloadSender.cs (97%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TcpDisconnectReason.cs (96%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TlsNegotiationLogging.cs (96%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TransportIoProbe.cs (98%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TransportIoReport.cs (99%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TransportIoSession.cs (99%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TransportLogMessages.cs (95%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TxPipePauseExperiment.cs (99%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TxWriteCoalescer.cs (97%) rename src/{VectorNNTPD.ReaderService => VectorNNTPD.Common}/Networking/Transport/TxWriteGranularityExperiment.cs (99%) delete mode 100644 src/VectorNNTPD.ReaderService/Networking/Listeners/SocketAcceptListener.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Listeners/ConnectionAcceptanceLogging.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Listeners/IListenSocketBinder.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Proxy/ConnectionClientIdentity.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Proxy/PrefixedStream.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/ConnectionByteTransport.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/INntpConnection.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/NntpConnection.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/NntpDeflateStream.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/NntpPipeOptions.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/SocketPayloadSender.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TcpDisconnectReason.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TlsNegotiationLogging.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TransportIoProbe.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TransportIoReport.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TransportIoSession.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TransportLogMessages.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TxPipePauseExperiment.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TxWriteCoalescer.cs delete mode 100644 src/VectorNNTPD.TransitService/Networking/Transport/TxWriteGranularityExperiment.cs delete mode 100644 src/VectorNNTPD.TransitService/SessionState/RateLimiting/IOutboundRateCap.cs delete mode 100644 src/VectorNNTPD.TransitService/SessionState/RateLimiting/OutboundRateLimiter.cs create mode 100644 tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs create mode 100644 tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs diff --git a/.gitattributes b/.gitattributes index 691d9158..b99a7ae2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,6 @@ # core.autocrlf cannot rewrite LF fixtures to CRLF on Windows checkout. tests/fixtures/articles/** -text tests/fixtures/SabctoolsYEnc/** -text +tests/VectorNNTPD.ReaderService.Tests/Data/InnArticles/** -text +tests/VectorNNTPD.TransitService.Tests/Data/InnArticles/** -text +tests/VectorNNTPD.Common.Tests/Fixtures/SabctoolsYEnc/** -text diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 68eebbdf..483dd204 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -50,6 +50,24 @@ logger.LogInformation("Published certificate state: StateQueue={StateQueue}, Bro | Enum values | `PascalCase` | `Found`, `WaitingToRestart` | | Config section names | `const string` | `public const string SectionName = "..."` | +### RADIUS Protocol Enum Naming Exception (SCREAMING_CASE) + +All enums in the `NNRPD.Radius` namespace that represent **RADIUS Attribute Names**, **Attribute Values**, or **Packet Codes** are an intentional exception to the PascalCase enum convention above. These identifiers use **SCREAMING_CASE** (`UPPER_SNAKE_CASE`) to maintain a 1:1 correspondence with the canonical IANA RADIUS dictionary names and RFC definitions. + +**This applies to:** + +- `RadiusCode` — packet type codes (e.g., `ACCESS_REQUEST`, `ACCOUNTING_RESPONSE`). +- `RadiusAttributeType` — attribute type identifiers (e.g., `USER_NAME`, `NAS_IP_ADDRESS`, `SERVICE_TYPE`). +- Attribute value enums — `SERVICE_TYPE`, `FRAMED_PROTOCOL`, `FRAMED_ROUTING`, `FRAMED_COMPRESSION`, `LOGIN_SERVICE`, `TERMINATION_ACTION`, `ACCT_STATUS_TYPE`, `ACCT_AUTHENTIC`, `ACCT_TERMINATE_CAUSE`, `NAS_PORT_TYPE`, `TUNNEL_TYPE`, `TUNNEL_MEDIUM_TYPE`, `ARAP_ZONE_ACCESS`, `PROMPT`, `INGRESS_FILTERS_VALUE`, `ERROR_CAUSE`, `NAMESPACE_IDENTIFIER`, `LOCATION_PROFILES`, `LOCATION_INFORMATION_SOURCE`, `BASIC_LOCATION_POLICY_RULES`, `LOCATION_CAPABLE`, `REQUESTED_LOCATION_INFO`, `FRAMED_MANAGEMENT_PROTOCOL`, `MANAGEMENT_TRANSPORT_PROTECTION`, `EAP_LOWER_LAYER`, `FRAG_STATUS`. + +**Do not:** + +- Rename these identifiers to PascalCase or any other convention. +- Apply IDE naming suggestions, code-style fixers, or bulk renames to these identifiers. +- Suppress or override the SCREAMING_CASE convention for any new RADIUS enum added to `RadiusEnums.cs`. + +**Rationale:** Renaming would break the direct mapping to the RADIUS protocol specification, make wire-level debugging harder, and invalidate `.ToString()` output used in structured logging and diagnostics. + ### Suffix Conventions for Constants - Time-related constants use a unit suffix: `ms` for milliseconds, `Seconds` for seconds. @@ -115,9 +133,22 @@ Every **public** type, method, property, constant, and field must have a ` private async Task QuitAndDisposeAsync() -### File headers +### File Header Comments + +### File Header Comments + +Each `.cs` file begins with a block comment summarizing the file's purpose, lifecycle, I/O architecture, and key call sequences. This is a plain `//` comment block (not XML doc), separated from the `using` directives by a blank line. + +The first line **must** begin with the filename (with or without the `.cs` extension) followed by an em-dash (`—`) or double-hyphen (`--`) and a brief one-line summary of the file's responsibility: + +// ConnectionHolder.cs — thread-safe singleton that holds the shared RabbitMQ IConnection. -BackFiller does not require a filename banner or a per-file copyright comment. Contract text belongs in XML documentation. Assembly copyright is emitted from `Directory.Build.targets`. Do not add banners to existing files as part of an unrelated change. + +For partial class files, include the partial-file suffix in the filename: + +// NodeIdentity.Logging.cs — Source-generated [LoggerMessage] partial methods for NodeIdentity. + +Subsequent lines provide additional detail: lifecycle, threading model, caller/callee relationships, cross-platform notes, and SIMD applicability. ## Logging Standards @@ -214,4 +245,84 @@ are not emitted to log output. - Distinguish between global shutdown cancellation and individual worker cancellation using linked `CancellationTokenSource`. - Always check `stoppingToken.IsCancellationRequested` in `catch (OperationCanceledException)` to differentiate timeout from shutdown. -Configuration keys and environment variables for this repository are in `docs/configuration.md`. Verification commands are in `AGENTS.md` and `.github/copilot-instructions.md`. Do not use `VectorNNTP.NNTPD.sln`. Builds are x64 only: pass `-p:Platform=x64`. +## Vector.NNTP.Utilities library standards + +[`Vector.NNTP.Utilities/README.md`](Vector.NNTP.Utilities/README.md) is the quick namespace map; this section is the contribution contract for that assembly. + +### Scope and dependencies + +- References **BCL** and **`Microsoft.Extensions.Logging.Abstractions`** only. Do not add RabbitMQ, Certes, or other domain packages. +- Add a new **public** namespace when a folder would exceed ~5 unrelated types or mixes hot-path binary operations with cold diagnostics. +- Add **`Vector.NNTP.Utilities.Internal`** helpers only when a pattern appears in **2+ call sites** or when isolating throws from a hot loop (see Internal folder policy below). + +### Hot path vs cold path + +Every Utilities `.cs` synopsis header must declare one of: + +``` +// HOT PATH: no LINQ; no allocations on success; no closures; no boxing; prefer Span/stackalloc; throws extracted + NoInlining where documented. +// COLD PATH: diagnostics/formatting/startup validation; readability over micro-optimization; allocations acceptable. +``` + +| HOT | COLD | +|-----|------| +| `EncodingUtilities`, `DnsWireFormatUtilities`, `DnsWireQueryBuilder`, `DnsWireNameReader`, `AsciiSpanUtilities`, `LengthLimitedReadStream`, `RetryUtilities`, `EwmaUtilities`, `HostParsingUtilities`, `IPUtilities` | `FormattingUtilities`, `EnvironmentUtilities`, `AssemblyInfoUtilities`, `DnsValidationUtilities`, `CredentialPlaceholderDetector`, `FileIOUtilities`, `DisposalUtilities`, `TaskUtilities` | + +Public types must include `Performance: HOT PATH …` or the cold-path equivalent. + +### Allocation rules + +- **stackalloc** for bounded buffers (DNS label splits, small wire packets). Follow `DnsWireQueryBuilder` — stack when `packetLength <= MaxStackAllocQuerySize`, heap fallback otherwise. +- **ArrayPool** for temporary copy buffers: rent in `try`, return in `finally` via `Internal.PoolingHelpers` (buffers are not cleared on return; callers overwrite via reads). +- Error strings and diagnostic formatting may allocate; keep them off success paths in HOT types. + +### Span and Memory + +- Overload order: **Span/Memory → string → async**; optional parameters last. +- Add Span overloads when callers already hold spans; keep string APIs for configuration and logging. +- Do not introduce extension methods by default. + +### Throw helpers + +- **Cold entry validation:** BCL helpers (`ArgumentNullException.ThrowIfNull`, `ArgumentException.ThrowIfNullOrEmpty`, `ArgumentOutOfRangeException.ThrowIfNegative`). +- **Hot or duplicated throws:** `Vector.NNTP.Utilities.Internal.ThrowHelpers` and `SpanValidationHelpers`. +- Extract throws from hot loops; mark throw methods with `[DoesNotReturn]` and `[MethodImpl(MethodImplOptions.NoInlining)]` when the throw would block inlining of the caller. +- Domain-specific messages (DNS label errors, response limit exceeded) stay in the owning type — do not genericize. + +### Async and cancellation + +- Use `*Async` suffix on asynchronous public APIs. +- Document whether cancellation **propagates** (`OperationCanceledException`) or maps to **return false** (`TaskUtilities.DelayOrCancelledAsync`, swallow-and-return helpers). +- Use `ConfigureAwait(false)` in library code unless a synchronization context is required (see repo-wide async guidance). + +### Thread safety + +- Document thread safety on every public type in XML ``. +- Stateless static helpers: safe for concurrent use. +- Instance types: document single-reader / caller-synchronised contracts explicitly. +- `Random.Shared` and immutable static caches (`FrozenSet`, cached delegates) are thread-safe; document them in type remarks. + +### Logging + +- Use `[LoggerMessage]` partial methods in companion `*.Logging.cs` files (see Logging Standards above). +- ASCII-only message strings in `[LoggerMessage]` attributes. + +### Nullable reference types + +- Follow the repo nullable section above. Do not use `!` without a documented invariant in ``. + +### Benchmarking expectations + +- Changes to **HOT PATH** types require regression checks: `dotnet test VectorNNTP.NNTPD.slnx -p:Platform=x64` plus a PR note when throughput or allocation behavior could change. +- Add BenchmarkDotNet coverage when introducing new SIMD or pooling paths (project TBD). + +### Internal folder policy + +| Add to `Internal/` when | Do not add when | +|-------------------------|-----------------| +| Same throw message in 2+ types | Only one consumer exists | +| Same span-length check in 2+ encode paths | Message or semantics differ by domain | +| ArrayPool rent/return duplicated | A one-line BCL call suffices | +| Disposed guard shared by instance types | A generic `Validate` or catch-all `Guard` would result | + +Current internal types: `ThrowHelpers`, `SpanValidationHelpers`, `GuardUtilities`, `PoolingHelpers`. Defer `AsyncHelpers` / `ExceptionUtilities` until a second consumer appears. diff --git a/PERFORMANCE.md b/PERFORMANCE.md new file mode 100644 index 00000000..1c88bb7e --- /dev/null +++ b/PERFORMANCE.md @@ -0,0 +1,1220 @@ +# VectorNNTP.NNTPD Performance + +## Purpose + +This document records the current baseline performance of the VectorNNTP.NNTPD socket/transport +stack using a real TCP benchmark against the production server implementation. + +The purpose of this benchmark was to answer: + +> Is the production socket/pipe/transport architecture fundamentally limiting NNTP throughput on the target hardware? + +The benchmark was deliberately performed before implementing the remaining heavy NNTP data-plane and +storage functionality so that transport performance could be isolated. + +This document also records a later **TAKETHIS** STREAM ingest benchmark on the same host, using the +.NET client in `tools/VectorNNTP.NNTPD.Bench`. BENCHIT and TAKETHIS exercise different portions of +the server and are not substitutes for each other. + +A later **CHECK** measurement is also recorded here. It is a session/application benchmark +(`NntpSession` + Pipes + fake Redis) of the frozen depth-16 CHECK pipeline. It is **not** a TCP +socket throughput measurement and is not a substitute for BENCHIT or TAKETHIS. + +A later **IHAVE** measurement is the real serialized IHAVE **command** benchmark: TCP to the +production host, production HistoryDB/Redis, and the RFC 3977 two-stage exchange. It is **not** +the IHAVE Pipe-reader microbenchmark documented separately below. + +A later **BackFiller** section records in-process article processing of the real +`.artifacts/Articles` corpus. BackFiller articles/s is not a BENCHIT, TAKETHIS, or IHAVE figure +and is not ranked against those workloads. + +## Test Environment + +| Item | Value | +|------|-------| +| CPU | Intel Core i9-12900KF | +| Cores / threads | 16C / 24T | +| RAM | approximately 32 GB | +| OS | Windows 10.0.26200 | +| Server | VectorNNTP.NNTPD | +| Benchmark client | `tools/VectorNNTP.NNTPD.Bench` | +| Server address | `198.18.0.66` | +| Plain benchmark port | `1199` | +| TLS benchmark port | `5633` | +| TLS | TLS 1.3 | +| Cipher | `TLS_AES_256_GCM_SHA384` | + +The IP address and benchmark ports are test-environment details, not protocol requirements. + +## BENCHIT + +`BENCHIT` is an **internal, intentionally unadvertised benchmark command**. + +- It is not advertised through `CAPABILITIES`. +- It is not advertised through `HELP`. +- It is not included in the public command inventory. +- It exists specifically to provide a stable transport benchmark workload. +- It is retained intentionally for future performance-regression testing. + +Workload characteristics: + +- article payload: `768000` bytes +- equivalent to `750 KiB` +- generated once and held in memory +- immutable static response +- response framing follows NNTP multiline article framing +- response begins with: `220 0 ` +- response terminates with the NNTP multiline terminator +- benchmark is non-pipelined +- normal session/command/transport path is exercised +- response is written through the production `PipeWriter`/transport path +- writes occur in chunks of at most 64 KiB +- approximately 12 flushes per response +- not a per-line flush benchmark + +This is a **transport-capacity workload**, not a realistic model of article-storage or database +performance. + +## Benchmark Methodology + +- Real TCP connections to the running production server (no in-process fake transport). +- Warm-up: 5 seconds (excluded from reported throughput). +- Measurement: 60 seconds per scenario. +- Two runs per scenario. +- Concurrency: 1, 10, and 50 connections. +- Modes: + - plain TCP + - DEFLATE (`COMPRESS DEFLATE`, RFC 8054 raw DEFLATE) + - TLS (implicit TLS listener) + - TLS + DEFLATE +- In-flight requests are allowed to finish cleanly. +- The benchmark does not intentionally cancel responses mid-frame. +- Latency is measured per completed request. +- Logical throughput is based on the 768000-byte article payload × completed requests. +- Wire throughput reflects bytes actually transmitted on the TCP stream after framing, + compression, and encryption as applicable. + +### Client/server CPU contention + +The benchmark client and VectorNNTP.NNTPD server execute on the same physical host and therefore +compete for the same CPU, scheduler, memory, cache, and other host resources. The reported +throughput is consequently an end-to-end measurement of the complete benchmark system rather than +an isolated measurement of server-only capacity. + +This makes the benchmark conservative as a measurement of isolated server capacity: the NNTP server +does not have exclusive access to the test host's compute resources. However, the benchmark does not +establish a linear relationship between available CPU resources and achievable throughput, so the +measured results must not be multiplied by an assumed factor such as 2×. + +The observed CPU utilization across the scenarios demonstrates that the host was not fully +CPU-saturated during the benchmark. However, the benchmark did not independently attribute CPU +consumption between the benchmark client and the NNTP server. Accordingly, this report records the +measured end-to-end throughput and does not extrapolate an unmeasured server-only throughput. + +The same-host arrangement means that some portion of the measured host CPU capacity was necessarily +consumed by the benchmark client, so the benchmark does not represent an isolated server-capacity +ceiling. The available headroom is real, but its effect on server-only throughput was not measured. + +**Logical vs wire throughput:** logical Gbit/s counts the uncompressed article payload volume. +Wire Gbit/s counts socket-level bytes (read + write) during the measurement window. Under DEFLATE, +wire throughput can be far lower than logical throughput. + +## iperf3 Baseline + +| Streams | Aggregate | +| ------: | --------: | +| 1 | 37.5 Gbit/s | +| 10 | 262 Gbit/s | +| 50 | 192 Gbit/s | + +- iperf3 version: 3.21 +- 60-second TCP tests +- target: `198.18.0.66:5201` + +iperf3 is a raw TCP throughput reference. BENCHIT performs actual NNTP request/response processing. +NNTP throughput is **not** expected to equal iperf3. + +## Results + +Complete benchmark table (both runs preserved): + +| Mode | Conn | Req/s | Logical Gbps | Wire Gbps | Comp | p50 ms | p95 ms | p99 ms | CPU % | +| ----------- | ---: | ----------------: | ----------------: | ----------------: | -----: | --------: | ----------: | ----------: | --------: | +| plain | 1 | 2627.3 / 2592.1 | 16.142 / 15.926 | 16.144 / 15.927 | — | 0.37 | 0.43–0.44 | 0.52–0.55 | 4.5 | +| plain | 10 | 20325.3 / 20287.0 | 124.879 / 124.644 | 124.888 / 124.653 | — | 0.49 | 0.56–0.57 | 0.64–0.65 | 41.8–42.5 | +| plain | 50 | 18839.1 / 18847.5 | 115.748 / 115.799 | 115.756 / 115.807 | — | 2.58–2.60 | 3.79–3.83 | 4.67–4.91 | 38.3–39.1 | +| deflate | 1 | 2353.4 / 2392.1 | 14.459 / 14.697 | 0.109 / 0.110 | 133.40 | 0.40–0.41 | 0.48–0.51 | 0.57–0.62 | 5.7–6.4 | +| deflate | 10 | 14604.6 / 14766.0 | 89.731 / 90.723 | 0.674 / 0.681 | 133.40 | 0.66–0.67 | 0.81–0.82 | 0.90 | 48.5–48.8 | +| deflate | 50 | 14546.7 / 14634.0 | 89.375 / 89.911 | 0.671 / 0.675 | 133.40 | 3.35–3.37 | 4.52–4.54 | 7.62–7.99 | 51.1–51.4 | +| tls | 1 | 1777.0 / 1763.9 | 10.918 / 10.837 | 10.934 / 10.853 | — | 0.54 | 0.67–0.68 | 0.81–0.87 | 5.3 | +| tls | 10 | 9189.3 / 9372.9 | 56.459 / 57.587 | 56.541 / 57.671 | — | 1.06–1.07 | 1.25–1.30 | 1.39–1.48 | 44.3–44.7 | +| tls | 50 | 10945.8 / 10691.1 | 67.251 / 65.686 | 67.349 / 65.781 | — | 3.38–3.44 | 12.24–12.60 | 31.42–32.31 | 51.3–51.9 | +| tls+deflate | 1 | 2250.5 / 2230.5 | 13.827 / 13.704 | 0.109 / 0.108 | 127.55 | 0.43 | 0.53–0.55 | 0.64–0.67 | 5.6–5.7 | +| tls+deflate | 10 | 13748.8 / 13766.2 | 84.473 / 84.579 | 0.666 | 127.55 | 0.71 | 0.86–0.87 | 0.95–0.97 | 48.0–48.2 | +| tls+deflate | 50 | 13826.4 / 13852.5 | 84.949 / 85.110 | 0.669 / 0.671 | 127.55 | 3.53–3.55 | 4.78–4.82 | 9.55–11.76 | 52.2–53.3 | + +## Interpretation + +### Plain TCP + +- Peak measured logical throughput: approximately 124.9 Gbit/s at 10 connections. +- 50 connections remain above 115 Gbit/s. +- No orders-of-magnitude collapse was observed. +- The 10→50 connection result plateaus rather than scaling linearly. +- Observed host CPU utilization remains below approximately 40–43% in the measured plain scenarios. + +### DEFLATE + +- Approximately 90 Gbit/s logical throughput at 10 and 50 connections. +- Observed host CPU rises into the ~49–51% range. +- Wire throughput falls to approximately 0.67–0.68 Gbit/s because the synthetic payload is extremely compressible. +- Compression ratio of approximately 133:1 is **not representative of real Usenet articles**. +- It is useful for measuring the CPU/wire behavior of the DEFLATE transport but must not be interpreted as a real-world expected compression ratio. + +### TLS + +- TLS 1.3 with `TLS_AES_256_GCM_SHA384`. +- Approximately 57.6 Gbit/s at 10 connections. +- Approximately 65–67 Gbit/s at 50 connections. +- TLS introduces substantial but bounded overhead compared with plain TCP. +- No catastrophic CPU saturation or throughput collapse was observed. +- The 50-connection latency tail is materially higher than the 10-connection result and should be retained as an observation, not prematurely classified as a defect. + +### TLS + DEFLATE + +- Approximately 84.5 Gbit/s at 10 connections. +- Approximately 85.1 Gbit/s at 50 connections. +- Observed host CPU approximately 48–53%. +- Wire throughput approximately 0.67 Gbit/s. +- On this highly compressible synthetic workload, DEFLATE substantially reduces the amount of data passed through TLS/network transmission and therefore the logical throughput of TLS+DEFLATE can exceed TLS-only throughput. +- This does NOT mean DEFLATE is intrinsically faster than TLS. +- It is an artifact of the benchmark workload's extremely high compressibility combined with logical-throughput accounting. + +## iperf3 vs NNTP + +```text +iperf3, 10 streams: 262 Gbit/s +NNTP plain, 10 clients: 124.9 Gbit/s +``` + +Approximately **48%** of the iperf3 aggregate throughput at the same concurrency level. + +This ratio is a workload comparison, not a transport-efficiency percentage or a required +performance target. iperf3 measures raw TCP bulk throughput, whereas BENCHIT performs complete +NNTP request/response transactions through the production application and transport stack. + +This compares: + +- raw TCP bulk throughput + +with: + +- complete NNTP request/response transactions +- application framing +- command processing +- PipeWriter/PipeReader machinery +- transport pumps +- 64 KiB write/flush cadence +- per-request synchronization +- response parsing +- benchmark client request/response handling + +Equality with iperf3 is neither expected nor required. The remaining gap is not treated as proof of a +specific bottleneck unless established by direct measurement. + +## Transport Verdict + +> **Baseline accepted. No transport stop-ship defect identified.** + +- The production TCP/pipe/transport stack demonstrates >100 Gbit/s logical NNTP throughput under the benchmark workload. +- Plain TCP reaches approximately half of the raw 10-stream iperf3 ceiling while performing actual NNTP work. +- No orders-of-magnitude socket/pipe failure was observed. +- TLS and DEFLATE remain performant under concurrency. +- Observed host CPU does not saturate during the benchmark. +- The transport layer has demonstrated sufficient throughput and concurrency characteristics under this benchmark to proceed with implementation of the remaining NNTP server functionality without further transport optimization work being required by this baseline. + +> **No transport optimization work is planned from this benchmark alone.** + +Future optimization should be driven by realistic workloads and measured evidence. + +## TAKETHIS + +`TAKETHIS` is an RFC 4644 STREAM ingest workload. It is **not** a transport-capacity substitute for +BENCHIT, and BENCHIT is **not** a TAKETHIS ceiling. + +The authoritative client is the .NET workload in `tools/VectorNNTP.NNTPD.Bench` +(`--benchmark TAKETHIS`). The historical Python client (`tools/nntp_takethis.py`) is **not** used +for the numbers in this document. + +Workload characteristics: + +- real TCP to the production `VectorNNTP.NNTPD` host (no in-process fake transport) +- plain TCP only (no DEFLATE, no TLS) +- `MODE STREAM` then pipelined `TAKETHIS` +- article is built once and reused (immutable) +- target body size argument: `768000` bytes (`750 KiB`) +- article body: `767930` bytes +- framed article on the wire: `768054` bytes (headers + body + multiline terminator) +- command + article: `768105` bytes +- TAKETHIS command Message-ID is unique per article +- article-header Message-ID is static so the payload is not rebuilt +- one vectored `Socket.SendAsync` per article (`TAKETHIS \r\n` + article) +- does **not** rebuild the ~768 KiB article per transaction +- does **not** use two separate command/article socket writes +- does **not** use `FlushAsync` on the TAKETHIS hot path +- does **not** use the Python client +- bounded pipeline: `256` outstanding TAKETHIS commands per connection +- real `239` / `439` response processing +- no production-server bypass + +### TAKETHIS methodology + +Comparable timing and concurrency to the BENCHIT baseline: + +- Warm-up: 5 seconds. Warm-up article counts are discarded. +- Measurement: 60 seconds per scenario. +- Two runs per scenario. +- Concurrency: 1, 10, and 50 connections. +- Same host and plain port as BENCHIT: `198.18.0.66:1199`. +- `--server-pid` samples the VectorNNTP.NNTPD process (same sampler as BENCHIT). +- The TAKETHIS client does not record per-article latency; p50/p95/p99 are not reported. +- In-flight TAKETHIS commands are allowed a short drain after the measure window. + +Throughput definitions (as reported by the harness): + +- **TAKETHIS/sec** = articles sent during the measurement window ÷ wall-clock elapsed of the run. + The harness elapsed includes connect, warmup, measure, and drain (observed 65.0–65.2 seconds for + these runs). Warm-up sends are excluded from the count. +- **Logical payload Gbit/s** = framed article bytes (`768054`) × articles sent ÷ elapsed. +- **Wire Gbit/s** = command + article bytes actually sent ÷ elapsed. + +TAKETHIS/sec is **not** the same unit as BENCHIT req/s. Do not compare them as if they were the +same work item. + +The same-host client/server CPU contention described under BENCHIT applies here as well. Reported +throughput is an end-to-end measurement of the complete benchmark system. Isolated server-only +capacity was not measured and must not be extrapolated. + +### TAKETHIS results + +#### A. Historical baseline + +Complete table (both runs preserved). Every cell is from the live production-host run on +`198.18.0.66:1199`. All runs completed with a 100% `239` response ratio and zero `439`, protocol, +connection, and temporary-`400` errors. + +| Mode | Conn | TAKETHIS/s | Logical Gbps | Wire Gbps | Sent | 239 | 439 | Err | CPU % | +| ----- | ---: | --------------: | ---------------: | --------------: | --------------: | --------------: | --: | --: | --------: | +| plain | 1 | 1075.9 / 1072.9 | 6.611 / 6.593 | 6.611 / 6.593 | 70067 / 69784 | 70067 / 69784 | 0 | 0 | 6.1–5.9 | +| plain | 10 | 4374.7 / 4321.4 | 26.880 / 26.553 | 26.882 / 26.554 | 284631 / 281098 | 284631 / 281098 | 0 | 0 | 30.2 | +| plain | 50 | 4152.0 / 4326.5 | 25.511 / 26.584 | 25.513 / 26.586 | 270730 / 282026 | 270730 / 282026 | 0 | 0 | 31.0–30.9 | + +Observed max outstanding TAKETHIS commands per connection was 11–14 (limit 256). + +Raw harness output: `.artifacts/takethis-performance-md/takethis-results.txt`. + +After the Transit queue became byte-budgeted, a 1-connection TAKETHIS regression +(`--connections 1 --warmup-seconds 5 --measure-seconds 60 --runs 2 --pipeline-depth 256`, +PID 23056) measured 1645.0 / 1705.1 TAKETHIS/s (10.108 / 10.477 Gbit/s), 100% `239`. +That is a historical regression check, not a claimed TAKETHIS optimization. Raw output: +`.artifacts/takethis-performance-md/takethis-1conn-byte-budget.txt`. + +#### B. Completed TAKETHIS implementation (1 connection) + +This is the production path after the RX handoff: one session RX task owns `Connection.Input`; +it parses TAKETHIS, starts HistoryDB Peek, frames the article with `IHaveArticleReader` into one +owned stuffed-wire buffer (terminator omitted), attaches that buffer to `TakeThisPipeline` +(depth 16), and returns. Peek / enqueue / Remember / ordered `239` run on pipeline completion +workers. TAKETHIS responses use `EnqueueLineImmediateAsync` (no TAKETHIS coalesce batch). Protocol +semantics are unchanged. + +1-connection cell only (same methodology as above; 10- and 50-connection cells were not re-run). +Host `198.18.0.66:1199`, NNTPD PID **26364**, timing probe **off**, real HistoryDB/Redis, unique +command Message-IDs (``). Command: + +```text +dotnet run -c Release --project tools/VectorNNTP.NNTPD.Bench -- ` + --benchmark TAKETHIS --host 198.18.0.66 --port 1199 ` + --connections 1 --warmup-seconds 5 --measure-seconds 60 --runs 2 ` + --pipeline-depth 256 --server-pid 26364 +``` + +| Mode | Conn | TAKETHIS/s | Logical Gbps | Wire Gbps | Sent | 239 | 439 | Err | CPU % | +| ----- | ---: | --------------: | ---------------: | --------------: | --------------: | --------------: | --: | --: | --------: | +| plain | 1 | 836.8 / 665.3 | 5.142 / 4.088 | 5.142 / 4.088 | 54478 / 43277 | 54478 / 43277 | 0 | 0 | 6.2 / 3.7 | + +Max outstanding: 29 / 31 (client pipeline limit 256). Response ratio 100% `239`. Zero `439`, +temporary `400`, protocol, and connection errors. + +This is **not** a throughput improvement over section A. The measured 1-connection rate is lower +than the historical 1073–1076 TAKETHIS/s cell and lower than the later 1645.0 / 1705.1 +byte-budget check. Those earlier cells were different architecture and HistoryDB occupancy; they +are not a like-for-like optimization delta. + +The remaining receive-interval cost is transport / input-Pipe refill under the production +`NntpPipeOptions.PauseWriterThreshold` of 64 KiB (resume 32 KiB). That is the session Pipe default +for all commands, not a TAKETHIS implementation defect. HistoryDB Peek, enqueue, Remember, and +`239` enqueue were measured on the completed path at approximately microsecond-to-sub-millisecond +scale; they do not dominate this 768 KiB article workload. + +### TAKETHIS interpretation + +Historical (section A): + +- Measured single-connection TAKETHIS throughput: approximately 1073–1076 articles/s + (approximately 6.59–6.61 Gbit/s logical) on this host and workload. +- Measured 10-connection TAKETHIS throughput: approximately 4321–4375 articles/s + (approximately 26.6–26.9 Gbit/s logical). +- Measured 50-connection TAKETHIS throughput: approximately 4152–4327 articles/s + (approximately 25.5–26.6 Gbit/s logical). +- The 10→50 connection result plateaus rather than scaling linearly. +- Server-process CPU as reported by the harness remains approximately 6% at 1 connection and + approximately 30–31% at 10 and 50 connections. + +Completed implementation (section B, 1 connection, PID 26364): + +- Measured 836.8 / 665.3 TAKETHIS/s (5.142 / 4.088 Gbit/s wire) with 100% `239`. +- Client max outstanding 29–31; server window remains `TakeThisPipeline.Depth` = 16. +- Run 2 reused the same command Message-ID sequence on a new connection (established client + methodology), so HistoryDB occupancy differs between the two runs. Both runs are reported. + +These figures are measurements of this STREAM/TAKETHIS ingest path on this host. They are not a +transport ceiling, not a product SLA, and not a claim that VectorNNTP supports a universal Gbit/s +rate. + +## CHECK + +Production CHECK pipeline depth: **16 (fixed)**. Depth is not configurable and is not a benchmark +parameter. + +`CHECK` is a **session/application** benchmark. It exercises the production `NntpSession` CHECK +path (`CheckPipeline`, HistoryDB, response writer) over in-process duplex Pipes with a bench-only +fake Redis. It does **not** open a TCP socket and must not be read as Internet-scale or TCP CHECK +throughput. + +The authoritative client is `tools/VectorNNTP.NNTPD.Bench --benchmark CHECK`. Workload parameters +match the frozen validation session bench (`.artifacts/check-historydb-bench/PipelineSessionBench.cs`). + +### CHECK methodology + +- Benchmark type: session/application (`NntpSession` + Pipes + fake Redis). +- Not measured: TCP socket throughput, TLS, DEFLATE, multi-session concurrency. +- Sessions: one in-process session per workload row. +- Fake Redis: `CheckDelayedRedis` (`Task.Delay` for 1/2/5 ms; optional first-call failure). +- Local HistoryDB: production `HistoryDb` constructed with the public constructor. +- CHECK count: 2000 when Redis delay is 0 ms; 200 when delay is 1, 2, or 5 ms. +- Warm-up: none. `GC.Collect` + `GC.WaitForPendingFinalizers` + `GC.Collect` before each measure + (same as the validation session bench). Allocation B/op is **not** reported: session setup and + GC around this harness are not a trustworthy per-CHECK allocation measure. +- Redis delays: 0 / 1 / 2 / 5 ms. Delayed rows are 1 / 2 / 5 ms only for redis-hit, redis-miss, + and local-hit. Cooldown is 0 ms. +- Concurrency: production CHECK overlap only (`CheckPipeline.Depth` = 16). No extra `Task.Run`. +- Ordering: hard fail if a response Message-ID is not in command send order. A successful run + therefore reports `Ordered = True` for every row. +- Peak in-flight: `CheckPipeline.PeakOccupied`. The run fails if the peak exceeds 16. + +Workload semantics: + +| Workload | Local HistoryDB | Redis EXISTS | Expected response | +| --- | --- | --- | --- | +| redis-hit | miss | true | `438` | +| redis-miss | miss | false | `238` | +| local-hit | hit | must not be consulted | `438` | +| cooldown | miss | first EXISTS throws; remaining CHECKs see cooldown | `431` | + +### CHECK results + +Single Release run on the test host (`Windows 10.0.26200`, Intel Core i9-12900KF). Every row +completed with `Ordered = True`. Allocation figures are omitted. + +| Workload | Redis Delay | CHECKs | CHECK/s | Peak In-Flight | Redis EXISTS | Responses | Ordered | +|---|---:|---:|---:|---:|---:|---|---| +| redis-hit | 0 ms | 2000 | 50691 | 1 | 2000 | 438×2000 | True | +| redis-hit | 1 ms | 200 | 1025 | 16 | 200 | 438×200 | True | +| redis-hit | 2 ms | 200 | 990 | 16 | 200 | 438×200 | True | +| redis-hit | 5 ms | 200 | 986 | 16 | 200 | 438×200 | True | +| redis-miss | 1 ms | 200 | 986 | 16 | 200 | 238×200 | True | +| redis-miss | 2 ms | 200 | 984 | 16 | 200 | 238×200 | True | +| redis-miss | 5 ms | 200 | 983 | 16 | 200 | 238×200 | True | +| local-hit | 0 ms | 2000 | 115079 | 1 | 0 | 438×2000 | True | +| local-hit | 1 ms | 200 | 161577 | 1 | 0 | 438×200 | True | +| local-hit | 2 ms | 200 | 264620 | 1 | 0 | 438×200 | True | +| local-hit | 5 ms | 200 | 157704 | 1 | 0 | 438×200 | True | +| cooldown | 0 ms | 2000 | 226817 | 1 | 1 | 431×2000 | True | + +Elapsed wall times recorded by the harness (same run): redis-hit 0 ms = 39.5 ms; redis-hit +1/2/5 ms = 195.2 / 202.0 / 202.8 ms; redis-miss 1/2/5 ms = 202.9 / 203.2 / 203.4 ms; +local-hit 0 ms = 17.4 ms; local-hit 1/2/5 ms = 1.2 / 0.8 / 1.3 ms; cooldown = 8.8 ms. + +### CHECK interpretation + +- CHECK execution is bounded to 16 outstanding operations per session. Delayed Redis workloads + reached peak in-flight 16. Zero-delay and local-hit workloads completed with peak in-flight 1 + because Redis returned before the next CHECK was admitted. +- Delayed redis-hit and redis-miss throughput on this Windows host was approximately 983–1025 + CHECK/s for 200 commands. The 1 / 2 / 5 ms `Task.Delay` rows did not produce distinct elapsed + times. These are measured session/application numbers with a fake delayed Redis, not TCP + throughput. +- local-hit did not consult Redis (`EXISTS = 0`). The 1 / 2 / 5 ms local-hit rows still ignore + Redis; their CHECK/s variation is from a ~1 ms elapsed window over 200 commands and is not a + Redis-latency result. +- cooldown: first Redis EXISTS failed; the remaining CHECKs returned `431`; EXISTS count stayed at + 1. Redis was not hammered after cooldown. +- Do not extrapolate these figures to production network throughput. + +## IHAVE + +`IHAVE` is the **real serialized IHAVE command benchmark**. It is **not** the +IHAVE Pipe-reader / RAW article-receive microbenchmark. + +The authoritative client is `tools/VectorNNTP.NNTPD.Bench --benchmark IHAVE`. +That flag now runs the command path: + +``` +IHAVE +← 335 + +. +← 235 +``` + +over real TCP to production `VectorNNTP.NNTPD`. HistoryDB is the production +server path (local memory + Redis `198.18.0.70:6379`). IHAVE is not pipelined +(RFC 3977 §6.3.2). Unexpected `435` / `436` / `437` fails the run. + +The earlier receive-only Pipe measure remains below as a forensic baseline. It +is not selected by `--benchmark IHAVE`. + +Workload characteristics: + +- real TCP to the production `VectorNNTP.NNTPD` host (no in-process fake transport) +- plain TCP only (no DEFLATE, no TLS) +- serialized: one in-flight IHAVE per connection (wait for 335, send article, wait for 235) +- production command parser / IHAVE handler / HistoryDB `PeekAsync` + `Remember` +- production raw IHAVE receive (`IHaveArticleReader`), queue admission, downstream destuff +- corpus: `.artifacts/Articles` (10,610 destuffed stored files, catalog order) +- wire preparation: restuff leading dots and append `CRLF . CRLF` (no client destuff) +- prepared in-memory prefix: 368 articles / 268,185,292 wire bytes (256 MiB budget), cycled +- command Message-ID is unique per IHAVE (``) +- TAKETHIS / CHECK / STREAM are not used +- no production-server bypass + +### IHAVE methodology + +Comparable timing to the BENCHIT / TAKETHIS 1-connection cell: + +- Warm-up: 5 seconds. Warm-up article counts are discarded. Sequence continues so HistoryDB + does not see a later duplicate. +- Measurement: 60 seconds. +- Two runs. +- Concurrency: 1 connection (IHAVE is serialized). `--connections` is honoured if supplied; + each connection remains serialized. +- Same host and plain port as BENCHIT/TAKETHIS: `198.18.0.66:1199`. +- HistoryDB / Redis: production `HistoryDb` on the running server (`Redis:Host` `198.18.0.70`, + port 6379; server log at start: `Redis connection established (PING 0 ms)`). +- `--server-pid` samples the VectorNNTP.NNTPD process (same sampler as BENCHIT/TAKETHIS). +- The IHAVE client does not record per-article latency; p50/p95/p99 are not reported. + +Throughput definitions (as reported by the harness, same convention as TAKETHIS): + +- **IHAVE/sec** = `235` accepted during the measurement window ÷ wall-clock elapsed of the run. + The harness elapsed includes connect, warmup, and measure (observed 65.0 seconds). Warm-up + sends are excluded from the count. +- **Logical payload Gbit/s** = article bytes sent ÷ elapsed (command line excluded). +- **Wire Gbit/s** = command + article bytes actually sent ÷ elapsed. + +IHAVE/sec is **not** the same unit as BENCHIT req/s or TAKETHIS/sec. Do not compare them as if +they were the same work item. + +The same-host client/server CPU contention described under BENCHIT applies here as well. + +### IHAVE results + +Complete table (both runs preserved). Live production-host run on `198.18.0.66:1199` on +2026-09-24. Both runs completed with 100% `235` and zero `435` / `436` / `437` / protocol / +connection errors. + +| Mode | Conn | Serialization | IHAVE/s | Logical Gbps | Wire Gbps | Sent | 235 | 435 | Err | CPU % | +| ----- | ---: | ------------ | -----------: | ---------------: | --------------: | --------------: | --------------: | --: | --: | --------: | +| plain | 1 | serialized | 771.9 / 777.3 | 4.500 / 4.532 | 4.501 / 4.532 | 50183 / 50524 | 50183 / 50524 | 0 | 0 | 9.2–9.8 | + +Prepared corpus prefix: 368 of 10,610 catalog articles (268,185,292 wire bytes), cycled. +Command line: 54 bytes. Average prepared article on the wire: 728,764 bytes. + +Raw harness output: `.artifacts/ihave-command-bench/results.txt`. + +### Transit article-queue memory (`TransitQueueMemoryLimit`) + +Transit article buffering is **byte-budgeted** rather than fixed at 256 articles. + +| Setting | Default | This host during the after-run | +| --- | ---: | ---: | +| `Nntpd:TransitQueueMemoryLimit` | `1073741824` (1 GiB) | `4294967296` (4 GiB, `VectorNNTP.NNTPD.json`) | + +The budget is the sum of owned queued article payload lengths +(`InboundArticle.Payload.Length`). It is not process-wide memory. The historical +256-article count bound was only a memory-safety choke and is no longer an +admission limit. A larger queue is intended to absorb bursts between ingress and +downstream workers; it is not a throughput optimization by itself. + +### IHAVE after byte-budgeted queue + +Same command, same corpus, same host, after replacing the 256-article cap with +the byte budget. Server PID 23056. 100% `235`, zero rejects/errors. + +| Mode | Conn | Serialization | IHAVE/s | Logical Gbps | Wire Gbps | Sent | 235 | 435 | Err | CPU % | +| ----- | ---: | ------------ | -----------: | ---------------: | --------------: | --------------: | --------------: | --: | --: | --------: | +| plain | 1 | serialized | 740.8 / 765.3 | 4.319 / 4.462 | 4.319 / 4.462 | 48179 / 49748 | 48179 / 49748 | 0 | 0 | 9.2–9.6 | + +Raw harness output: `.artifacts/ihave-command-bench/results-byte-budget.txt`. + +This is **not** an optimization victory. Serialized 1-connection IHAVE/s did not +improve versus the 256-entry baseline above (771.9 / 777.3). The result is +reported as measured. + +### IHAVE non-blocking queue admission (final) + +IHAVE uses `TransitQueueMemoryLimit` as **non-blocking** backpressure. It does +not wait for queue memory. Temporary inability to accept is `436`. + +- Before `335`: `TryProbeCapacity` (any remaining payload byte; no MaxSize + reservation). +- After receive: `TryAdmit` (atomic reserve of the actual owned payload length). +- Article larger than the entire budget remains `437`. + +Same command, same corpus, same host. Server PID 9520. 100% `235`, zero +`435` / `436` / `437` / errors. + +| Mode | Conn | Serialization | IHAVE/s | Logical Gbps | Wire Gbps | Sent | 235 | 435 | Err | CPU % | +| ----- | ---: | ------------ | -----------: | ---------------: | --------------: | --------------: | --------------: | --: | --: | --------: | +| plain | 1 | serialized | 772.8 / 778.2 | 4.506 / 4.537 | 4.506 / 4.537 | 50262 / 50590 | 50262 / 50590 | 0 | 0 | 9.4–10.1 | + +Raw harness output: `.artifacts/ihave-command-bench/results-nonblocking-admission.txt`. + +This is a regression/validation run, not an IHAVE optimization. IHAVE is +complete at this admission contract. + +### IHAVE interpretation + +- Baseline (256-article queue): approximately 772–777 articles/s + (approximately 4.50–4.53 Gbit/s logical) on this host and workload. +- After byte-budgeted queue: approximately 741–765 articles/s + (approximately 4.32–4.46 Gbit/s logical). Not higher than the baseline. +- After non-blocking IHAVE admission: approximately 773–778 articles/s + (approximately 4.51–4.54 Gbit/s logical). In the same range as the original + 256-entry command-bench baseline. +- Every accepted IHAVE completed the production HistoryDB peek, raw article receive, queue + admission, and `235` response. Duplicate Message-IDs were not used. +- Max outstanding was 1 (serialized). IHAVE was not pipelined. +- Server-process CPU as reported by the harness remains approximately 9–10% at 1 connection. +- These figures are measurements of this serialized IHAVE command path on this host. They are + not a transport ceiling, not a product SLA, and not interchangeable with TAKETHIS/sec or + the forensic IHAVE Pipe-reader MB/s numbers. + +### IHAVE Serialized Command Timing — Diagnostic + +This section is **not** the authoritative IHAVE throughput result. It does not replace +the 771.9 / 777.3 IHAVE/s table above. `--benchmark IHAVE --timing` samples client-visible +phases with `Stopwatch.GetTimestamp`. Production IHAVE / HistoryDB / Redis were not +instrumented. `--timing` is off by default and is not used by the duration throughput path. + +Configuration (2026-09-24, same host as the command benchmark): + +- `--benchmark IHAVE --timing --samples 4000 --host 198.18.0.66 --port 1199 --connections 1 --warmup-seconds 5 --server-pid 29136` +- Real TCP to production `VectorNNTP.NNTPD`; production HistoryDB + Redis `198.18.0.70:6379` +- Same prepared `.artifacts/Articles` prefix (368 articles, restuffed wire in memory) +- Serialized; 5 s warmup not sampled; 4000 completed `235` samples; 0 unexpected rejects +- Clock: `Stopwatch.GetTimestamp` (monotonic) +- Raw output: `.artifacts/ihave-command-bench/timing.txt`, `timing.csv` + +**What `235` means:** `IHave.ExecuteAsync` writes `235` after `HistoryDb.PeekAsync`, the +`335` write, `IHaveArticleReader.ReadAsync` (frame + own stuffed wire), `EnqueueAsync`, +and `Remember`. `IhaveArticleInterpreter` destuff/`Article` construction runs later in +`IncomingSpoolWriterService` and is **not** on the `235` path. + +Phase timings (microseconds), last clean 4000-sample run: + +| Phase | Mean | P50 | P90 | P95 | P99 | +| --- | ---: | ---: | ---: | ---: | ---: | +| command send | 18.8 | 16.0 | 34.0 | 40.0 | 51.0 | +| IHAVE sent → 335 | 273.1 | 228.0 | 359.0 | 425.0 | 590.0 | +| 335 → article sent | 153.4 | 147.0 | 174.0 | 206.0 | 289.0 | +| article sent → 235 | 1086.6 | 567.0 | 1086.4 | 2915.6 | 14979.1 | +| transaction (IHAVE→235) | 1533.3 | 991.5 | 1657.5 | 4284.4 | 15518.2 | + +Average sampled article: 728,575 wire bytes. Mean client article send: 38.0 Gbit/s +(729 KiB in 153 µs). That is not the 1.29 ms budget. + +An earlier 4000-sample repeat in the same session (also 100% `235`) measured mean +transaction 1233.3 µs (P50 921.0). Tails on `article sent → 235` were already present +(P99 ≈ 12 ms). + +Interpretation (measured, not predicted): + +- **Most of the serialized transaction is `article sent → 235`**, not HistoryDB. + That interval is server receive/frame/own + `EnqueueAsync` + `Remember` + `235` write + + one RTT. P50 is ~0.5–0.6 ms; the mean is pulled up by a long tail (P99 12–15 ms), + consistent with occasional queue wait (`EnqueueAsync` waits when the 256-deep queue is + full). Worker destuff is after `235` but can still delay the next `EnqueueAsync`. +- **`IHAVE sent → 335` is ~0.23–0.27 ms mean** (P99 < 0.6 ms). That is the entire + command parse/dispatch + HistoryDB `PeekAsync` (local miss then Redis `EXISTS`) + + `335` write + RTT. Isolated Redis latency was **not** measured; there is no + production HistoryDB timing seam. On this host Redis PING at server start was 0 ms. + Calling this path “1 ms Redis” is not supported by the client-visible offer time. +- Client article transfer is ~0.15 ms mean and is not the dominant term. +- Command send is ~0.02 ms. + +Reconciliation with ~777 IHAVE/s: + +- Published 777.3 IHAVE/s uses wall elapsed including 5 s warmup (65.0 s), so + 1e6/777.3 = **1286.5 µs**. Measure-window-only for that run is 50183/60 = + **836.4 IHAVE/s** → **1196 µs**. +- Diagnostic mean 1233 µs (earlier repeat) implies 811 IHAVE/s; P50 921 µs implies + 1086 IHAVE/s. Those sit next to the measure-window 836/s figure. The published + 777/s is lower because warmup is in the denominator. +- The last timing run’s mean 1533 µs (652 IHAVE/s) is slower than the duration + table; its P99 tail is heavier. That is reported, not discarded. + +Limitations: + +- Client-side only. HistoryDB Peek vs `335` write vs RTT are not split. +- Redis `EXISTS` distribution was not recorded inside `HistoryDb`. +- No HistoryDB-bypass control: the live host has no bench-only seam, and + `history is null` is a production unconfigured path, not a diagnostic switch. +- Sampling 4000 timed transactions adds `Stopwatch` reads on that mode only. +- After this diagnostic work, a duration IHAVE rerun (same flags, **no** `--timing`) + measured 739.9 / 675.6 IHAVE/s. The default path still has no per-article timers. + That after-result is a check, not a replacement of the published table. + +Candidate follow-up (not implemented): measure `EnqueueAsync` wait vs reader time +vs worker destuff rate; optional off-by-default HistoryDB/Redis probe if a later +task needs a true EXISTS distribution. + +## IHAVE RAW receive microbenchmark (forensic) + +This section is a **forensic** session/application Pipe-reader measure of production +`IHaveArticleReader` (frame `CRLF . CRLF`, own stuffed wire; destuff is +downstream in `IhaveArticleInterpreter`). It is **not** the IHAVE command benchmark +and is **not** selected by `--benchmark IHAVE`. It is **not** a TCP socket throughput +measurement. One `PipeReader.ReadAsync` is not one socket read. + +The report remains at `.artifacts/ihave-corpus-bench/REPORT.md`. +Input is the complete real corpus at `.artifacts/Articles` (10,610 destuffed +stored articles; the harness restuffs leading dots and appends the NNTP +terminator plus a leftover `DATE` command). File list: +`.artifacts/ihave-corpus-bench/used-files.txt`. Raw report: +`.artifacts/ihave-corpus-bench/REPORT.md`. + +TAKETHIS production code was not modified. The same corpus, chunk sizes, Pipe +options, and leftover check were passed through the existing unmodified STREAM +reader (`NntpContinuousRxReader.ReadUnitAsync`) for an observational compare +only. + +### Forensic IHAVE reader methodology + +- Benchmark type: session/application (`IHaveArticleReader` + in-process Pipes). +- Not measured: TCP, TLS, DEFLATE, HistoryDB, Redis, the response writer, queue persist. +- Corpus: complete `.artifacts/Articles` (7,806,078,914 bytes). One pass per mode. +- Mode A (prebuffered): entire framed article is written and flushed before the reader starts. Pipe `pauseWriterThreshold` = 8 MiB. +- Mode B (streaming): a concurrent producer writes 4 / 16 / 64 / 256 KiB chunks. Pipe `pauseWriterThreshold` = `2 × chunk` so the producer cannot prebuffer the whole article. +- ReadAsync / AdvanceTo counts come from a bench-only `PipeReader` wrapper. Production hot-path instrumentation was not added. +- Safety: every article left `DATE\r\n` intact (0 leftover failures). + +### Forensic IHAVE reader corpus + +| Metric | Value | +|---|---:| +| articles | 10610 | +| total bytes | 7806078914 | +| average | 735728.5 | +| median / p50 | 740474 | +| p95 | 793113 | +| p99 | 1082806 | +| min | 647 | +| max | 2164763 | + +95.13% of articles are 500 KiB–1 MiB. There are no articles above 5 MiB. The +previous ~750 KiB estimate is close to this measured average (718.5 KiB) but +must not replace the measured number. + +Classification (production `ArticleTypeClassifier`; flags overlap): yEnc +10,574 (99.66%); MIME 33 (0.31%); BASE64 0; UUENCODE 0; binary 10,574; +text (`Default`/`None`) 3; multipart 2. No article has `Content-Length`. +10,574 articles have a `Bytes:` header; the production reader does not +consult it. 7,943 yEnc articles expose `size=`; the mean declared yEnc size +is 625,533,225 bytes (decoded/original size, not wire bytes). + +### Forensic IHAVE reader results + +Single Release run on the test host (`Windows 10.0.26200`, Intel Core +i9-12900KF) after the production raw-wire receive change. Complete corpus, +one iteration per row. Destuff/classification is not on this receive path. + +| Mode | Chunk | ReadAsync/art | AdvanceTo/art | bulk ops/art | bulk bytes/art | B/read | MB/s | art/s | +|---|---|---:|---:|---:|---:|---:|---:|---:| +| A prebuffered | (all first) | 1.00 | 1.00 | 1.00 | 735729 | 735732 | 2589.8 | 3691.1 | +| B streaming | 4 KiB | 90.90 | 90.90 | 1.00 | 735729 | 8093 | 1277.5 | 1820.7 | +| B streaming | 16 KiB | 22.93 | 22.93 | 1.00 | 735729 | 32080 | 1709.1 | 2435.8 | +| B streaming | 64 KiB | 6.10 | 6.10 | 1.00 | 735729 | 120649 | 2029.4 | 2892.3 | +| B streaming | 256 KiB | 1.99 | 1.99 | 1.00 | 735729 | 369345 | 2388.5 | 3404.2 | + +Leftover failures: 0. Prebuffered receive is 2.48× the previous destuff-on-ingress +result (1042.5 MB/s) on this same harness. + +Observational TAKETHIS STREAM (unmodified; framed wire copy, no destuff), same +corpus and arrival: + +| Mode | Chunk | ReadAsync/art | MB/s | art/s | +|---|---|---:|---:|---:| +| A prebuffered | (all first) | 1.00 | 2128.7 | 3033.8 | +| B streaming | 4 KiB | 90.87 | 1173.4 | 1672.4 | +| B streaming | 16 KiB | 22.93 | 1428.5 | 2035.9 | +| B streaming | 64 KiB | 6.08 | 1778.5 | 2534.7 | +| B streaming | 256 KiB | 1.99 | 1762.0 | 2511.2 | + +### Forensic IHAVE reader interpretation + +- Prebuffered IHAVE uses one `ReadAsync` because the whole article is already + in the Pipe when the reader starts. That is not evidence of streaming or + socket performance. +- Streaming IHAVE `ReadAsync` counts track the arrival quantum (about + `2 × chunk` bytes per read because `pauseWriterThreshold` is `2 × chunk`), + not a constant “one read per article”. +- Production IHAVE receive copies stuffed wire and stops at `CRLF . CRLF`. + Destuff and `Article` construction run in `IhaveArticleInterpreter` after + queue admission, not in this receive benchmark. +- The terminator remained authoritative; leftover `DATE` was never consumed. +- TAKETHIS STREAM used the same arrival model. On this after-run, prebuffered + IHAVE (2589.8 MB/s) exceeded observational STREAM (2128.7 MB/s). These are + Pipe figures, not TCP ingest rates. +- Do not treat these Pipe MB/s figures as Internet or TCP ingest rates. + +## BackFiller + +BackFiller article processing is an **in-process handler benchmark**. It calls +`ProviderArticleWorkHandler.HandleAsync` on articles already in memory. It does not open NNTP +sockets, RabbitMQ, or VATP storage. It is not a TCP ingest measurement and not a substitute for +BENCHIT, TAKETHIS, or IHAVE. + +The successive rows below use one harness, +`DestuffOptimizationMeasurementTests.MeasureDestuffOptimization_WhenEnabled`, Release, corpus +`.artifacts/Articles`, concurrency 1,000 × 100,000 operations after 1,000 warmup operations. +MiB/s counts framed wire bytes. The machine recorded in those audits has 24 logical processors. +The test runtime does not enable server GC. An earlier audit, +`.artifacts/backfiller-realworld-audit/REPORT.md`, measured 5,537 articles/s on +`BackFillerRealWorldAuditTests`. That is a different harness and is not a step in the progression +below. + +### Corpus + +Path: `.artifacts/Articles`. Distribution from +`.artifacts/backfiller-realworld-audit/REPORT.md`. The files were not modified for the later runs. +Every later harness run reported the same acceptance counts. + +| Metric | Value | +|---|---:| +| Files | 10,610 | +| Bytes | 7,806,078,914 (7.27 GiB) | +| Min | 647 | +| Average | 735,728 | +| Median | 740,474 | +| p90 | 741,075 | +| p95 | 793,113 | +| p99 | 1,082,806 | +| Max | 2,164,763 | + +Size buckets (count / bytes): 1–1 KiB 2 / 1,454; 1–4 KiB 25 / 55,930; 4–16 KiB 10 / 69,844; +16–64 KiB 3 / 128,600; 64–256 KiB 93 / 22,706,528; 256 KiB–1 MiB 10,282 / 7,542,286,318; +1–5 MiB 195 / 240,830,240; above 5 MiB 0. + +Full-corpus `ArticleRecordFactory.TryCreate` (Traverse): 10,596 accepted, 13 +`YEncDecodingFailed`, 1 `BodyLineTooLong`. 10,596 + 13 + 1 = 10,610. + +The audit describes the corpus as almost entirely ~740 KiB yEnc parts (`=ybegin`). The forensic +classification already recorded above is 10,574 yEnc of 10,610 (99.66%). That is the retained +real Usenet set used for this workload, not a synthetic mixed-content benchmark. The same audit +notes that it is a poor mix for text articles, tiny control messages, and articles near the 5 MiB +limit. + +The timed rows do not scan all 10,610 files on each operation. They recycle a 256-article accepted +sample, 184,739,144 bytes (mean 721,637; median 740,407). The corpus scan still classifies all +10,610 files once per harness run. + +### Optimization progression + +Each line is the 1,000 × 100,000 long run of the harness above. Comparability is the statement in +the corresponding audit report, not a renormalized series. + +| Step | Articles/s | Report | +|---|---:|---| +| Before destuff buffering | 3,264.4 | `destuff-opt-before.txt` | +| Destuff buffering | 4,707.7 | `destuff-opt-after.txt` | +| Article payload ownership | 4,973.0 | `canonical-ownership-opt.txt` | +| Uninitialized canonical allocation | 5,056.1 | `uninitialized-alloc-opt.txt` | +| Hardware yEnc CRC (mean of three quiet runs) | 6,414.7 | `hardware-crc-opt-REPORT.md` | + +Rounded, that progression is about 3,264 → 4,708 → 4,973 → 5,056 → 6,415 articles/s. The raw +one-decimal values above are the measurements. + +### Current steady state (hardware CRC) + +Baseline is the uninitialized-allocation long run: 5,056.1 articles/s. The hardware-CRC figure is +the mean of three quiet runs with no tracer attached: 6,460.3, 6,266.8, and 6,517.1 articles/s. +Mean 6,414.7 articles/s. That is +26.9% versus 5,056.1. The slowest run, 6,266.8 articles/s, is ++23.9% versus that baseline. + +| Metric | Uninitialized-allocation baseline | Hardware CRC mean | +|---|---:|---:| +| Articles/s | 5,056.1 | 6,414.7 | +| MiB/s | 3,479.6 | 4,414.7 | +| CPU cores busy | 22.59 | 20.63 | +| CPU time per article | 4.47 ms | 3.22 ms | +| Allocated bytes/article | 726,830 | 726,882 | +| Allocated bytes/s | 3.67 GB/s | 4.66 GB/s | +| p50 | 4.43 ms | 3.19 ms | +| p95 | 36.68 ms | 25.68 ms (runs 30.16, 24.17, 22.70) | +| p99 | 54.48 ms | 47.32 ms (runs 55.63, 44.31, 42.03) | +| Gen2 collections | 151 | 153 (runs 152, 156, 150) | +| Gen2/s | 7.63 | 9.79 | +| GC pause | 313.2 ms (15.83 ms/s) | 347.7 ms (22.30 ms/s) | + +Allocated bytes/article changed by +52 bytes (+0.007%). The allocation rate rose with throughput. +The gain is CPU time per article (4.47 ms → 3.22 ms), not a smaller article allocation. Gen2/s +rose because about the same 150 collections finished in less wall time. Collections per article +did not change. p50 fell on every run (3.17, 3.18, 3.21 versus 4.43). p95 and p99 improved on two +of the three runs. Run 1's p99 was 55.63 ms, next to the baseline 54.48 ms, so the tail change is +less stable than the throughput change. The harness does not record max latency. + +### CPU profile + +12-second `Microsoft-DotNETCore-SampleProfiler` trace of `testhost` during a hardware-CRC harness +run (`cpu-hardware-crc.nettrace`, 78,132 samples). Collection started after that process stayed at +or above 15 cores for 3 seconds. `dotnet-trace report topN` exclusive time for +`YEncArticleValidator.TryComputeDecodedCrc32AndLength` is 20.53% (inclusive 21.24%). The ownership +profile (`cpu-ownership-opt-REPORT.md`) recorded the same method at 32.84% exclusive. The +uninitialized-allocation step was not profiled. The stack walk of this trace has that method as +the deepest frame on 18.67% of samples. + +`System.IO.Hashing.Crc32.UpdateVectorized` is on the stacks: 501 samples (0.64%; 0.71% exclusive +in `topN`). That method is the library's PCLMUL folding routine. `Pclmulqdq.IsSupported` was true +on this runtime. `Pclmulqdq` / `CarrylessMultiply` is not a separate sampled frame; those calls are +inlined into `UpdateVectorized`. The native instruction bytes were not disassembled. + +The remaining time inside `TryComputeDecodedCrc32AndLength` is the yEnc decode loop. After that +method, the large costs in this trace are canonical `AllocateUninitializedArray` (15.2% deepest, +caller `Materialize`; 8.83% exclusive in `topN`) and destuff `Memmove` (12.1% of samples called +from `ReadArticlePayloadAsync`; `Memmove` overall 17.0% exclusive in `topN`). + +The traced process's own long row was 5,663 articles/s, CPU 18.18, 725,394 bytes/article, p50 +3.23 ms. The tracer was attached for most of that row. That row is not part of the 6,414.7 +articles/s mean. + +### Permanent yEnc CRC path + +`YEncArticleValidator` is the production trailer check. Each section uses one lazily created +thread-local `System.IO.Hashing.Crc32`: `Reset` once per section, `Append` of each 512-byte +decoded batch and the tail, then `GetCurrentHashAsUInt32`. `System.IO.Hashing.Crc32` selects +hardware or its own scalar fallback. There is no CRC32C, SSE4.2, or custom PCLMUL implementation. +`YEncCrc32` and `IeeeCrc32` remain the scalar reference and test oracle and are not called on this +path. + +`YEncHardwareCrcEquivalenceTests` compares the validator with `YEncCrc32.Compute` for batch +boundaries (including 0, 1, 15, 16, 511, 512, 513, and 4,096 bytes), ordinary and escaped bytes, +multipart `pcrc32`, a CRC mismatch, and a trailing escape. +`Yenc_validation_does_not_allocate_after_warmup` and +`Parser_yenc_validation_path_does_not_allocate_after_warmup` remain the zero-allocation-after-warmup +checks. The long-run +52 bytes/article is the full `HandleAsync` path, not a per-article `Crc32`. + +Verification of that permanent implementation (Release, no 1000 × 100,000 rerun): + +- yEnc CRC, equivalence, and allocation tests: 108 passed +- full Common: 651 passed +- BackFiller ownership / pipeline / article integration: 29 passed +- full BackFiller: 707 passed, 3 failed, 710 total +- `dotnet build VectorNNTP.NNTPD.sln -c Release`: 0 warnings, 0 errors +- `dotnet format VectorNNTP.NNTPD.sln --verify-no-changes`: exit 0 + +The three BackFiller failures are logging tests, not the CRC path. +`ProductionJson_DeclaresLoggingSettings_AndOmitsSerilogSection` expected `Information` and observed +`Debug`. `ConsoleSink_IsConfiguredAtDebug` failed `Assert.True`. +`Console_IsAbsentUnlessTheConsoleSwitchIsPresent` failed in that full run because the sink walk +saw a type whose name contains `Console`; the same test passed when run alone. The full BackFiller +suite was not green. + +### Interpretation + +On this harness, corpus, and host, hardware CRC raised steady-state throughput from 5,056.1 to +6,414.7 articles/s. Against the destuff-before long run, 6,414.7 / 3,264.4 = 1.965, approximately +twice that measured baseline. That is an in-process article-processing improvement. It is not a +universal BackFiller ceiling, not a product SLA, and not a server-only capacity. Network, RabbitMQ, +and storage were not on the path. + +### Reproduction + +Raw files are under `.artifacts/backfiller-realworld-audit/`. The harness writes +`destuff-opt-{phase}.txt` and will overwrite `destuff-opt-after.txt` unless that file is copied +aside first. Preserved baselines include `destuff-opt-before.txt`, `destuff-opt-after.txt`, +`canonical-ownership-opt.txt`, and `uninitialized-alloc-opt.txt`. Hardware-CRC rows are +`hardware-crc-opt-run1.txt`, `hardware-crc-opt-run2.txt`, and `hardware-crc-opt-run3.txt`. The +trace is `cpu-hardware-crc.nettrace`. + +```powershell +$env:BACKFILLER_DESTUFF_OPT = "after" +dotnet test tests\VectorNNTP.BackFiller.Tests\VectorNNTP.BackFiller.Tests.csproj -c Release ` + --filter FullyQualifiedName~DestuffOptimizationMeasurementTests.MeasureDestuffOptimization_WhenEnabled +``` + +## Workload comparison + +BENCHIT, TAKETHIS, CHECK, and IHAVE are different workloads. None replaces the others. + +| Aspect | BENCHIT | TAKETHIS | IHAVE command | +| --- | --- | --- | --- | +| What it measures | Production transport/TX path with a static multiline response | Real STREAM/TAKETHIS receive, framing, response, and ingestion processing, plus the relevant transport path | Real serialized IHAVE command: HistoryDB peek, 335, raw article receive, queue, 235 | +| Direction of bulk data | Server → client | Client → server | Client → server | +| Command | Internal unadvertised `BENCHIT` | RFC 4644 `MODE STREAM` + `TAKETHIS` | RFC 3977 `IHAVE` | +| Pipelining | Non-pipelined request/response | Bounded pipeline (256 / connection) | Not pipelined (one in flight / connection) | +| Article accounting | `768000`-byte response payload | `768054`-byte framed article | cycling `.artifacts/Articles` wire (mean 728,764 bytes in the prepared prefix) | +| Reported work unit | completed requests / s | articles sent / s | `235` accepted / s | +| Modes in this document | plain, DEFLATE, TLS, TLS+DEFLATE | plain TCP only | plain TCP only | +| HistoryDB | not used | not the admission path | production HistoryDB + Redis | +| Latency | p50 / p95 / p99 recorded | not recorded by this client | not recorded by this client | + +On this host, the existing BENCHIT plain 10-connection measurement is approximately 124.9 Gbit/s +logical. The TAKETHIS 10-connection measurement is approximately 26.9 Gbit/s logical. Those numbers +are not a ranking and not a statement that one path is “the” ceiling. They count different work. + +BackFiller articles/s is a fourth workload: in-process `HandleAsync` on the real corpus, with no +TCP session. Do not rank 6,414.7 BackFiller articles/s against BENCHIT req/s, TAKETHIS/s, or +IHAVE/s. Those NNTPD figures stay as published above. + +iperf3 remains a raw TCP reference (10 streams: 262 Gbit/s). It is not a TAKETHIS ceiling and not a +BENCHIT efficiency score. + +## Reproduction + +The server under test is the production `VectorNNTP.NNTPD` host bound to `198.18.0.66:1199` (and +`5633` for BENCHIT TLS). Start it separately, then run the client from the repository root. + +`--benchmark BENCHIT` is the explicit workload selector. Omitting `--benchmark` keeps the historical +BENCHIT default. + +```powershell +cd src\VectorNNTP.NNTPD +dotnet run -c Release --no-launch-profile +``` + +```powershell +# BENCHIT (default workload; --benchmark BENCHIT is optional) +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- ` + --benchmark BENCHIT ` + --host 198.18.0.66 --plain-port 1199 --tls-port 5633 ` + --server-pid --runs 2 +``` + +TAKETHIS CLI defaults differ from this document (warmup 0 s, one run, 30 s measure) unless the +flags below are supplied. Use these flags to reproduce the table. + +```powershell +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- ` + --benchmark TAKETHIS ` + --host 198.18.0.66 --port 1199 ` + --connections 1 --warmup-seconds 5 --measure-seconds 60 --runs 2 ` + --pipeline-depth 256 --server-pid + +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- ` + --benchmark TAKETHIS ` + --host 198.18.0.66 --port 1199 ` + --connections 10 --warmup-seconds 5 --measure-seconds 60 --runs 2 ` + --pipeline-depth 256 --server-pid + +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- ` + --benchmark TAKETHIS ` + --host 198.18.0.66 --port 1199 ` + --connections 50 --warmup-seconds 5 --measure-seconds 60 --runs 2 ` + --pipeline-depth 256 --server-pid +``` + +`--duration` is an alias for `--measure-seconds`. `--port` is an alias for `--plain-port`. + +CHECK does not use the TCP host/port flags. It runs in-process: + +```powershell +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- --benchmark CHECK +``` + +IHAVE CLI defaults differ from this document (warmup 0 s, one run, 30 s measure) unless the +flags below are supplied. Use these flags to reproduce the command-benchmark table. + +```powershell +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- ` + --benchmark IHAVE ` + --host 198.18.0.66 --port 1199 ` + --connections 1 --warmup-seconds 5 --measure-seconds 60 --runs 2 ` + --server-pid +``` + +`--benchmark IHAVE` is the real serialized command path. It is not the forensic Pipe-reader +microbenchmark. + +Diagnostic timing (does not replace the duration table): + +```powershell +dotnet run -c Release --project tools\VectorNNTP.NNTPD.Bench -- ` + --benchmark IHAVE --timing --samples 4000 ` + --host 198.18.0.66 --port 1199 ` + --connections 1 --warmup-seconds 5 --server-pid +``` + +## Limitations + +- Static in-memory article. +- Highly compressible synthetic content. +- No article database lookup. +- No overview database lookup. +- No disk/storage latency. +- No realistic article-size distribution. +- No realistic article entropy distribution. +- No realistic client think time. +- BENCHIT is not intended to represent production application throughput. +- TAKETHIS is not intended to represent a universal production ingest SLA. +- TAKETHIS uses a static in-memory article and a unique command Message-ID; it is not a realistic + article-size or entropy distribution. +- The measured TAKETHIS server does not write incoming article payloads to disk (the persist write + is inactive). Message-ID hashing and incoming-directory setup still run. +- TAKETHIS/sec is not interchangeable with BENCHIT req/s. +- Logical Gbit/s should not be interpreted as network wire throughput when compression is active. +- Results are specific to the test host, network path, runtime, OS, and benchmark implementation. +- Benchmark client and server share the same physical host, so isolated server-only capacity was not measured. +- CHECK is a session/application measure (`NntpSession` + Pipes + fake Redis). It is not TCP CHECK + throughput and is not comparable to BENCHIT req/s or TAKETHIS/sec. +- CHECK Redis latency is `Task.Delay` in a bench-only fake. On this Windows host the 1 / 2 / 5 ms + delayed rows completed in essentially the same elapsed time. +- CHECK allocation B/op is not reported. +- IHAVE command/sec is not interchangeable with BENCHIT req/s or TAKETHIS/sec. +- The IHAVE command benchmark cycles a prepared prefix of `.artifacts/Articles` (368 of + 10,610 files, 256 MiB budget) because holding the full 7.8 GiB corpus in the client is + not practical. Catalog order is preserved. Command Message-IDs are unique so HistoryDB + does not reject later iterations. +- The forensic IHAVE Pipe-reader MB/s figures are not TCP ingest rates and are not the + `--benchmark IHAVE` command result. +- BackFiller articles/s is not interchangeable with BENCHIT req/s, TAKETHIS/s, or IHAVE/s. + The BackFiller harness does not measure sockets, RabbitMQ, or storage, and it does not + establish a server-only capacity. + +## Post-migration shared transport note (2026-10-29) + +`VectorNNTPD` extracted the listening socket / duplex pipeline transport into +`VectorNNTPD.Common` (`SocketAcceptListener`, `NntpConnection`, `NntpConnectionStream`) and +migrated ReaderService, TransitService, StorageService, and BackFillerService onto that shared +implementation. Historical tables above remain the recorded measurements from +`VectorNNTP.NNTPD` before that extraction. + +### What remains comparable in principle + +The **BENCHIT** section is the closest historical transport-capacity baseline: real TCP to the +production NNTP host on this same machine class (Intel Core i9-12900KF, Windows 10.0.26200), +exercising the production listen → session → `PipeWriter` / send-pump path with a static +768000-byte multiline response. Peak plain measurement: approximately **124.9 Gbit/s logical** at +10 connections (60 s measure, 5 s warmup). + +### Why a quantitative before/after is not claimed here + +| Factor | Historical BENCHIT (`PERFORMANCE.md`) | Current loopback harness | +| --- | --- | --- | +| Implementation under test | Monolithic `VectorNNTP.NNTPD` transport (pre-Common extraction) | Shared `VectorNNTPD.Common` listen + `NntpConnection` + `NntpConnectionStream` echo | +| Workload | Non-pipelined `BENCHIT` server→client article response | Bidirectional loopback echo of a fixed buffer | +| Payload | 768000-byte framed article (~12×64 KiB flushes) | Default 65536-byte buffer | +| Concurrency / duration | 1 / 10 / 50 conn; 5 s warmup + 60 s measure | 8 clients; 2 s warmup + 5 s measure | +| Path depth | Full `NntpSession` + command dispatch | No NNTP session; stream adapter only | +| Client | `tools/VectorNNTP.NNTPD.Bench` (not present in this repository) | `.artifacts/shared-pipeline-transport-bench/` | +| Network | TCP to `198.18.0.66` (same-host client/server) | `IPAddress.Loopback` only | + +The authoritative BENCHIT client project is **not** checked into `VectorNNTPD`, so the historical +TCP suite cannot be re-executed from this tree without restoring that tool and a full Reader/Transit +host configuration. Comparing 124.9 Gbit/s BENCHIT logical throughput to the loopback echo +megabyte/s figures would mix incompatible work items. + +**Conclusion:** retain BENCHIT / TAKETHIS / IHAVE / CHECK / BackFiller tables as historical evidence +that the pre-migration transport stack was not fundamentally limiting on this host. Treat the +`.artifacts/shared-pipeline-transport-bench/` numbers as a **post-migration absolute baseline** for +the shared listen/pipeline/stream adapter only. Do not read them as a BENCHIT regression or +improvement score. + +### Current loopback baseline (post-migration, corrected accounting) + +Recorded 2026-10-29 on the same developer host class (Windows 10.0.26200, .NET 10.0.13, 24 logical +processors). Harness: `.artifacts/shared-pipeline-transport-bench/`. Command: + +```text +dotnet run --project .artifacts/shared-pipeline-transport-bench -c Release -p:Platform=x64 -- 65536 8 5 2 +``` + +Definitions: one **op** = one server echo round-trip; **payload** throughput counts the payload once +per op; **duplex** throughput counts read+write of that payload; counters reset after warmup; +metrics snapshotted before cancel/drain. + +| Metric | Value (latest re-run) | +| --- | ---: | +| ops | 354502 | +| elapsed (measure) | 5.002 s | +| ops/s | ~70870 | +| payload throughput | ~4429 MB/s | +| duplex throughput | ~8859 MB/s | +| alloc | ~1253 MB (~3708 B/op) | +| process CPU (measure window) | ~68.5 s sum-of-cores | + +Raw notes: `.artifacts/shared-pipeline-transport-bench/RESULTS.md`. + +## Future Performance Work + +Future benchmarks should eventually measure realistic workloads such as: + +- GROUP +- LIST +- LISTGROUP +- ARTICLE +- HEAD +- BODY +- STAT +- OVER +- HDR +- POST +- additional streaming/ingest variants beyond the TAKETHIS workload documented above +- article-store/cache behavior +- overview database behavior +- realistic article size and entropy distributions +- mixed concurrent client workloads + +No transport micro-optimizations are proposed at this stage. The current objective has been achieved: +establish that the socket/transport architecture is fundamentally sound. + +## Validation + +- Release build: passed +- `dotnet format --verify-no-changes`: passed +- BENCHIT tests: 4/4 passed +- Full test suite: 576/576 passed on rerun; the initial run had one transient failure in `Compress_Deflate_SessionRoundTrip_Repeated_AuthinfoRejected`, which passed on rerun +- `git diff --check`: clean +- Complete live BENCHIT benchmark suite completed +- Benchmark results retained at: `tools/VectorNNTP.NNTPD.Bench/results-full.txt` +- Complete live TAKETHIS benchmark (1 / 10 / 50 connections, 5 s warmup, 60 s measure, 2 runs) + completed on 2026-09-23 against production `VectorNNTP.NNTPD` on `198.18.0.66:1199` +- TAKETHIS harness exit code 0 on every cell; 100% `239`; zero `439` / protocol / connection errors +- TAKETHIS raw output retained at: `.artifacts/takethis-performance-md/takethis-results.txt` +- CHECK session/application benchmark (`--benchmark CHECK`, Release) completed on 2026-09-24; + every workload reported `ordered=True`; peak in-flight 16 on delayed Redis rows; local-hit + EXISTS 0; cooldown EXISTS 1 +- IHAVE real serialized command benchmark (`--benchmark IHAVE`, Release) completed on + 2026-09-24 against production `VectorNNTP.NNTPD` on `198.18.0.66:1199` with production + HistoryDB/Redis (`198.18.0.70:6379`); 1 connection; 5 s warmup; 60 s measure; 2 runs; + 100% `235`; zero `435` / `436` / `437` / protocol / connection errors; raw output at + `.artifacts/ihave-command-bench/results.txt` +- Forensic IHAVE Pipe-reader measure remains at `.artifacts/ihave-corpus-bench/REPORT.md` + and is not selected by `--benchmark IHAVE` +- BackFiller hardware-CRC permanence check (Release, no 1000 × 100,000 rerun): yEnc CRC / + equivalence / allocation tests 108 passed; full Common 651 passed; BackFiller article / + ownership / pipeline tests 29 passed; full BackFiller 707 passed and 3 failed (the two + previously observed logging-level tests, plus `Console_IsAbsentUnlessTheConsoleSwitchIsPresent`, + which passed when run alone). Solution build 0 warnings, 0 errors. Format verify exit 0. + Raw steady-state rows remain under `.artifacts/backfiller-realworld-audit/` diff --git a/README.md b/README.md index 66a9e11f..637ea278 100644 --- a/README.md +++ b/README.md @@ -22,15 +22,16 @@ Permanent docs live under [`docs/`](docs/). Start with [`docs/README.md`](docs/R | Document | Contents | |---|---| | [docs/architecture.md](docs/architecture.md) | BackFiller host lifecycle, Article Work, byte-oriented data plane | +| [docs/nntpd-reader-transit.md](docs/nntpd-reader-transit.md) | Reader/Transit roles, packaging, CAPABILITIES vs HELP, deployment | | [docs/configuration.md](docs/configuration.md) | BackFiller options, RabbitMQ, NntpDB, secrets (BackFiller-focused) | | [docs/storage.md](docs/storage.md) | StorageService architecture and configuration | | [docs/storage-nntp-feed.md](docs/storage-nntp-feed.md) | Storage NNTP feed (ARTICLE / TAKETHIS / QUIT) | | [docs/logging.md](docs/logging.md) | Serilog for BackFiller and StorageService | -| [docs/systemd.md](docs/systemd.md) | systemd notify/watchdog for both hosts | +| [docs/systemd.md](docs/systemd.md) | systemd notify/watchdog for BackFiller/Storage | | [docs/common/](docs/common/) | Shared library contracts (ArticleRecord, VATP, Common config) | | [docs/standards/](docs/standards/) | Local RFC and protocol standards library | -| [docs/schema/](docs/schema/) | Upstream Transit/PostFilter SQL references only — not applied by these hosts | -| [docs/reference/upstream-nntpd/](docs/reference/upstream-nntpd/) | Labeled upstream NNTPD notes (not VectorNNTPD behaviour) | +| [docs/schema/](docs/schema/) | Transit / PostFilter / account-art-type SQL (operator-applied; no migrator) | +| [docs/reference/upstream-nntpd/](docs/reference/upstream-nntpd/) | Historical monolith NNTPD notes | ## Build diff --git a/docs/README.md b/docs/README.md index 5ccfc6eb..44722671 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,22 +1,25 @@ # VectorNNTPD documentation -This directory is the permanent documentation for the **VectorNNTPD** solution currently in `VectorNNTPD.slnx`: BackFillerService, StorageService, and Common (plus their tests). +Permanent documentation for the **VectorNNTPD** solution in `VectorNNTPD.slnx`: -ReaderService and TransitService may appear on a development machine as untracked working-tree directories; they are **not** part of the solution on `master` and are not documented here as shipping hosts. +- `BackFillerService` +- `StorageService` +- `ReaderService` +- `TransitService` +- `Common` (+ corresponding test projects) ## Start here | Document | Audience | Contents | |---|---|---| | [../README.md](../README.md) | Operators / developers | Build, test, publish, Linux install overview | -| [architecture.md](architecture.md) | Developers | BackFiller host lifecycle, Article Work, NNTP client/listener roles, silent-consumer monitoring, byte-oriented data plane | -| [configuration.md](configuration.md) | Operators / developers | **BackFiller only** — options, JSON locations, RabbitMQ, NntpDB, secrets | -| [storage.md](storage.md) | Operators / developers | **StorageService** architecture, configuration, persistence, messaging, ops | -| [storage-nntp-feed.md](storage-nntp-feed.md) | Developers / integrators | Storage NNTP feed: ARTICLE / TAKETHIS / QUIT only | -| [logging.md](logging.md) | Operators / developers | Serilog-only logging for BackFiller and StorageService | -| [systemd.md](systemd.md) | Operators | systemd notify/watchdog install and operations for both hosts | - -Redis / PostFilter / Transit DDL under [schema/](schema/) are **not** Storage or BackFiller startup requirements. +| [nntpd-reader-transit.md](nntpd-reader-transit.md) | Operators / developers | Reader/Transit roles, packaging, CAPABILITIES vs HELP, deployment | +| [architecture.md](architecture.md) | Developers | BackFiller host lifecycle, Article Work, byte-oriented data plane | +| [configuration.md](configuration.md) | Operators / developers | **BackFiller** options, RabbitMQ, NntpDB, secrets | +| [storage.md](storage.md) | Operators / developers | **StorageService** architecture, configuration, persistence, messaging | +| [storage-nntp-feed.md](storage-nntp-feed.md) | Developers / integrators | Storage NNTP feed: ARTICLE / TAKETHIS / QUIT | +| [logging.md](logging.md) | Operators / developers | Serilog for BackFiller and StorageService (Reader/Transit use host Serilog sections) | +| [systemd.md](systemd.md) | Operators | systemd notify/watchdog (BackFiller/Storage; Reader/Transit units in deploy + nntpd-reader-transit) | ## Shared library @@ -27,36 +30,32 @@ Redis / PostFilter / Transit DDL under [schema/](schema/) are **not** Storage or | [common/configuration.md](common/configuration.md) | Shared configuration helpers | | [common/ArticleRecord.md](common/ArticleRecord.md) | Canonical article envelope | | [common/vatp.md](common/vatp.md) | Vector Article Transfer Protocol | -| [common/systemd-host-abstraction.md](common/systemd-host-abstraction.md) | **Proposal only** — comparing Storage vs BackFiller systemd stacks | +| [common/systemd-host-abstraction.md](common/systemd-host-abstraction.md) | **Proposal only** — Storage vs BackFiller systemd stacks | -## Standards and schema +## Common ownership vs Reader/Transit duplication -| Path | Contents | -|---|---| -| [standards/rfcs/](standards/rfcs/) | Local NNTP / Usenet RFC text library (authoritative for protocol work in this repo) | -| [standards/spamd/](standards/spamd/) | SPAMC/SPAMD protocol text — **upstream PostFilter interoperability**; not a VectorNNTPD host feature | -| [standards/haproxy/](standards/haproxy/) | PROXY protocol text — standards library only; not an implementation claim for BackFiller/Storage | -| [standards/pgpverify/](standards/pgpverify/) | INN pgpverify format notes — interoperability reference; BackFiller does not implement PGPVERIFY | -| [schema/](schema/) | **Upstream-only** Transit / PostFilter / `account_art_type` SQL — see [schema/README.md](schema/README.md) | +`VectorNNTPD.Common` holds genuinely shared infrastructure (ACME, Cloudflare, NntpDB sessions, article parse/`ArticleRecord`, RabbitMQ connection lifecycle, gzip logging sinks, cache-fleet wire contracts). It is **not** an NNTP server library. -## Upstream NNTPD reference (not hosts in this solution) +ReaderService and TransitService each embed a full NNTP server implementation (session, commands, listeners, PostFilter, Redis, ingestion, …). ~527 corresponding source files differ only by namespace (`VectorNNTPD.ReaderService` vs `VectorNNTPD.TransitService`); a small set differs by role (hosting registration, CAPABILITIES, MODE gating). That duplication is intentional so neither host depends on the other and Common does not become a relocated monolith. There is **no** `VectorNNTPD.NntpServer` project. -Material from `VectorNNTP.NNTPD` for engineers who need command or PostFilter context when -working beside an NNTPD deployment. Paths such as `src/VectorNNTP.NNTPD/...` refer to the -**upstream** repository. They are **not** an implementation guarantee for -`VectorNNTPD.BackFillerService` or `VectorNNTPD.StorageService`. +## Standards and schema -For what this solution actually speaks on the wire, use [architecture.md](architecture.md) -(BackFiller upstream client + cache listener) and [storage-nntp-feed.md](storage-nntp-feed.md) -(Storage ARTICLE / TAKETHIS / QUIT). Do not treat the upstream command checklist as Storage or -BackFiller CAPABILITIES. +| Path | Contents | +|---|---| +| [standards/rfcs/](standards/rfcs/) | Local NNTP / Usenet RFC text library | +| [standards/spamd/](standards/spamd/) | SPAMC/SPAMD protocol text (Reader PostFilter) | +| [standards/haproxy/](standards/haproxy/) | PROXY protocol reference | +| [standards/pgpverify/](standards/pgpverify/) | INN pgpverify format notes | +| [schema/](schema/) | NntpDB SQL for Transit / PostFilter / `account_art_type` — see [schema/README.md](schema/README.md) | -Upstream Redis and PostFilter configuration are **not** required to operate VectorNNTPD hosts. +## Historical / upstream NNTPD reference | Document | Topic | |---|---| -| [reference/upstream-nntpd/commands.md](reference/upstream-nntpd/commands.md) | Upstream NNTPD command inventory | -| [reference/upstream-nntpd/postfilter.md](reference/upstream-nntpd/postfilter.md) | Upstream PostFilter operator guide | +| [reference/upstream-nntpd/commands.md](reference/upstream-nntpd/commands.md) | Monolith NNTPD command inventory (provenance) | +| [reference/upstream-nntpd/postfilter.md](reference/upstream-nntpd/postfilter.md) | PostFilter operator guide (applies to Reader PostFilter behaviour) | + +For live VectorNNTPD wire behaviour prefer [nntpd-reader-transit.md](nntpd-reader-transit.md), [storage-nntp-feed.md](storage-nntp-feed.md), and [architecture.md](architecture.md). ## Agent and contribution docs (repository root) diff --git a/docs/common/README.md b/docs/common/README.md index 8dae0697..73b47336 100644 --- a/docs/common/README.md +++ b/docs/common/README.md @@ -2,7 +2,9 @@ Shared library imported from `VectorNNTPD.Common` into `src/VectorNNTPD.Common`. The assembly and root namespace remain `VectorNNTPD.Common`. Tests live in `tests/VectorNNTPD.Common.Tests` and reference this project. -The library owns reusable implementations. It does not own application topology, host startup order, or a second copy of BackFiller. +The library owns reusable implementations. It does not own application topology, host startup order, NNTP session/command servers, or a second copy of any host. + +ReaderService and TransitService consume Common for ACME/Cloudflare/NntpDB/articles/RabbitMQ connectivity/logging sinks, but keep duplicated NNTP server code inside each host project (see [../nntpd-reader-transit.md](../nntpd-reader-transit.md) and [../README.md](../README.md)). ## What this library contains @@ -10,6 +12,7 @@ The library owns reusable implementations. It does not own application topology, - Cloudflare DNS API client and A/AAAA reconciliation (`VectorNNTPD.Common.Cloudflare`) - Authoritative DNS queries for DNS-01: NS discovery and UDP/TCP TXT lookup (`VectorNNTPD.Common.Dns`) - Bind-address expansion and eligibility (`VectorNNTPD.Common.Networking`) +- High-performance listening socket and duplex pipeline transport (`VectorNNTPD.Common.Networking.Listeners` / `Transport`): `SocketAcceptListener`, `NntpConnection`, pipe options, outbound rate limiter, and `NntpConnectionStream` for stream-shaped session adapters. Reader, Transit, Storage, and BackFiller NNTP listeners consume this shared implementation. NNTP session/command servers stay in the host projects. - Options types those hosts adapt into (`VectorNNTPD.Common.Configuration`), including `ApplicationFqdn`, `ServerIdRules`, `VectorEnvironment`, `ApplicationLocalPath`, and `ApplicationJsonConfiguration`. Relative paths must stay inside the application base after `Path.GetFullPath` normalization. Rooted paths stay rooted. The check does not follow symlinks or junctions. StorageService keeps a separate `StorageRelativePath` helper for joining validated leaf segments under spool/sysfs bases; that is not a substitute for `ApplicationLocalPath`. See [configuration.md](configuration.md#application-local-and-storage-relative-paths). The JSON helper removes Generic Host `appsettings` sources and registers `{entry assembly}.json`. The RabbitMQ filename is an explicit argument. BackFiller passes `VectorNNTPD.RabbitMQ.json`. - NntpDB connection options and MySQL session (`VectorNNTPD.Common.NntpDb`) - Article content primitives: parser, `ArticleRecord`, `ArticleId`, classification, yEnc validation, and OverviewDB protobuf codec (`VectorNNTPD.Common.Articles`) diff --git a/docs/nntpd-reader-transit.md b/docs/nntpd-reader-transit.md index 3ae0f35d..e6148b88 100644 --- a/docs/nntpd-reader-transit.md +++ b/docs/nntpd-reader-transit.md @@ -6,13 +6,13 @@ This document describes the migrated NNTP server split in VectorNNTPD. It does n | Project | Role | |---------|------| -| `VectorNNTPD.Common` | Shared connectivity and domain primitives (ACME, Cloudflare, NntpDB, articles, VATP, RabbitMQ connection lifecycle). Not an NNTP server library. | +| `VectorNNTPD.Common` | Shared connectivity and domain primitives (ACME, Cloudflare, NntpDB, articles, VATP, RabbitMQ connection lifecycle) **and** the authoritative high-performance listen/accept + `System.IO.Pipelines` duplex transport (`SocketAcceptListener`, `NntpConnection`, related pipe/rate-limit helpers). Not an NNTP session/command server library. | | `VectorNNTPD.ReaderService` | Reader executable **and** Reader NNTP implementation (session/commands, listeners, auth, PostFilter, moderation, email, Redis session state, ingestion, RabbitMQ topology) | | `VectorNNTPD.TransitService` | Transit executable **and** Transit NNTP implementation (peer DNS/auth, CHECK/TAKETHIS/IHAVE, transit peer state, shared plumbing duplicated for independence) | | `tests/VectorNNTPD.ReaderService.Tests` | Reader protocol, hosting, PostFilter, session-state, and related tests | | `tests/VectorNNTPD.TransitService.Tests` | Transit protocol, peer, ingestion, and related tests | -There is **no** `VectorNNTPD.NntpServer` assembly. ReaderService → Common and TransitService → Common only. Shared *server* plumbing required by both hosts is **duplicated** into each executable project so neither depends on the other and Common does not become a relocated monolith. +There is **no** `VectorNNTPD.NntpServer` assembly. ReaderService → Common and TransitService → Common only. Shared *listen/pipeline transport* lives in Common. Shared *session/command* plumbing required by both hosts remains **duplicated** into each executable project so neither depends on the other and Common does not become a relocated session monolith. Configuration section name remains `Nntpd`. Primary JSON files are `{entry assembly}.json` plus shared `VectorNNTPD.RabbitMQ.json`, loaded from `AppContext.BaseDirectory` (the executable directory). @@ -38,7 +38,28 @@ Ingress destuffs transport wire once, then `ArticleRecordFactory` / `NntpArticle ## Schema DDL -Operator SQL for PostFilter and `nntpusers` account-art-type lives under `docs/schema/` (`postfilter.sql`, `nntpusers-account-art-type.sql`). Apply these on the site MySQL database before enabling features that depend on those tables/columns. Do not invent production DDL beyond the checked-in scripts. +Operator SQL for Transit catalogue, PostFilter, and `nntpusers` account-art-type lives under `docs/schema/` (`nntptransit.sql`, `postfilter.sql`, `nntpusers-account-art-type.sql`). Apply these on the site MySQL database before enabling features that depend on those tables/columns. There is no in-process migrator. See [schema/README.md](schema/README.md). Do not invent production DDL beyond the checked-in scripts. + +## Shared code vs Common + +Common owns ACME, Cloudflare, NntpDB connectivity, CanonicalV1 article materialization, RabbitMQ connection lifecycle, logging sinks, **and the shared listening-socket / pipeline transport** (`SocketAcceptListener`, `NntpConnection`, `NntpPipeOptions`, `ConnectionByteTransport`, outbound rate limiting used by the transport, `ConnectionClientIdentity` / `PrefixedStream` needed at connection start, and `NntpConnectionStream` for stream-shaped adapters). + +Reader and Transit each keep a full NNTP **session/command** tree under their own namespaces (protocol loops, CAPABILITIES, auth, PostFilter, etc.). Storage and BackFiller keep their feed/cache session protocols. Moving those trees into Common would make Common an NNTP server library and is intentionally avoided; creating a separate `NntpServer` assembly is also rejected. + +**Transport ownership:** there is one authoritative Pipeline duplex implementation in Common. Reader/Transit plain and TLS listeners, Storage’s NNTP TLS listener, and BackFiller’s cache NNTP TLS listener all execute that implementation for accept and connection I/O. TLS handshake for implicit-TLS listeners is performed by `NntpConnection.StartTlsAsync` (certificate context from Common’s TLS provider). STARTTLS / DEFLATE upgrades remain available on the shared connection for Reader/Transit session commands. Service-specific admission, PROXY preamble parsing (Reader/Transit), TCP buffer/keepalive policy (BackFiller), and I/O progress timeouts (Storage/BackFiller stream adapters) stay in the hosts. + +### Consolidation assessment (session trees retained; transport extracted) + +Evidence (branch `common-nntpd`): corresponding Reader/Transit `.cs` files still differ mostly by root namespace for the **session/server** tree; role-specific files remain (`Program.cs`, hosting registration, plain/TLS listener **services**, `NntpSession`, CAPABILITIES). The former duplicated `Networking/Transport` + `SocketAcceptListener` copies were extracted into Common and deleted from both hosts. + +| Option | Verdict | +|--------|---------| +| **A. Keep duplicated host session trees; share transport in Common** | **Current.** Role isolation and independent Native AOT packages preserved. Transport duplication removed. | +| B. Mass-move Session/PostFilter/Redis into Common | Rejected without an explicit product decision—pulls command graphs into Common with little gain for Storage/BackFiller. | +| C. Dedicated `NntpServer` assembly | Rejected (relocated monolith). | +| D. Linked/shared sources or `#if` | Rejected. | + +When fixing bugs in ns-only **session** files, patch both Reader and Transit trees. Transport bugs are fixed once in Common. ## Release package layout diff --git a/docs/schema/README.md b/docs/schema/README.md new file mode 100644 index 00000000..41c99f2b --- /dev/null +++ b/docs/schema/README.md @@ -0,0 +1,25 @@ +# NntpDB schema references + +SQL scripts for catalogues used by VectorNNTPD hosts. There is **no application migration +runner** that applies these files automatically. Operators apply them to the site MySQL +database when enabling the corresponding features. + +| File | Used by | Role | +|------|---------|------| +| [nntptransit.sql](nntptransit.sql) | **TransitService** (also present as duplicated plumbing in ReaderService sources) | Transit control-plane DDL (peer / receive / send / publication). Code SELECTs via `NntpTransitQueries`; it does not CREATE. | +| [postfilter.sql](postfilter.sql) | **ReaderService** PostFilter (duplicated types also exist under TransitService) | PostFilter policy DDL. Apply before enabling PostFilter. See [../nntpd-reader-transit.md](../nntpd-reader-transit.md) and [../reference/upstream-nntpd/postfilter.md](../reference/upstream-nntpd/postfilter.md). | +| [nntpusers-account-art-type.sql](nntpusers-account-art-type.sql) | **ReaderService** AUTHINFO ArtType capability | Additive `nntpusers.account_art_type`. Apply before deploying a Reader that SELECTs that column. | + +## Not provisioned by these scripts + +| Catalogue / store | Used by | How maintained | +|-------------------|---------|----------------| +| `nntpsharedconfig` (single row) | All hosts (column subsets differ) | External ops; no checked-in CREATE TABLE. | +| `nntpbackfilleraccounts` | BackFiller only | External ops; `MySqlProviderAccountSource` SELECTs. | +| SQLite `ArticleIndex` / `ArticleIndexMeta` | StorageService | Created in-process (`ArticleIndexDatabase`). See [../storage.md](../storage.md). | + +## Deployment notes + +- Do **not** treat these scripts as a BackFiller or StorageService install step. +- Transit and PostFilter DDL **are** required when operating TransitService / Reader PostFilter against a fresh NntpDB. +- Redis is required for Reader PostFilter quota and related Reader session features; it is **not** required for BackFiller or StorageService. diff --git a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerAcceptFailureObserver.cs b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerAcceptFailureObserver.cs new file mode 100644 index 00000000..64c10130 --- /dev/null +++ b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerAcceptFailureObserver.cs @@ -0,0 +1,31 @@ +using VectorNNTPD.Common.Networking.Listeners; + +namespace VectorNNTPD.BackFillerService.Listener +{ + /// + /// Maps shared accept-failure observations onto . + /// + internal sealed class CacheListenerAcceptFailureObserver : IListenAcceptFailureObserver + { + private readonly ILogger _logger; + + /// Initializes the observer with the cache listener logger. + internal CacheListenerAcceptFailureObserver(ILogger logger) + { + ArgumentNullException.ThrowIfNull(logger); + _logger = logger; + } + + /// + public void OnAcceptFailed(string socketError, int consecutiveFailures, int maxFailures) => + CacheListenerLogMessages.AcceptFailed(_logger, socketError, consecutiveFailures, maxFailures); + + /// + public void OnAcceptRecovered(int recoveredAfterFailures) => + CacheListenerLogMessages.AcceptRecovered(_logger, recoveredAfterFailures); + + /// + public void OnAcceptLoopStopping(int consecutiveFailures, string socketError) => + CacheListenerLogMessages.AcceptLoopStopping(_logger, consecutiveFailures, socketError); + } +} diff --git a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerEndpoints.cs b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerEndpoints.cs deleted file mode 100644 index 09867bde..00000000 --- a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerEndpoints.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Net; -using System.Net.Sockets; -using VectorNNTPD.Common.Core; -using VectorNNTPD.Common.Networking.Listeners; - -namespace VectorNNTPD.BackFillerService.Listener -{ - /// - /// Binds listen sockets from Common plans. - /// - /// - /// Each socket is a TCP stream socket. Nagle and buffer sizes are not set here: accepted sockets do not - /// inherit those options, and applies them to each accepted socket - /// before TLS. is assigned only for IPv6 sockets. The listen backlog passed to - /// is 512. Bind or listen failure disposes the socket and rethrows. - /// - internal static class CacheListenerEndpoints - { - /// Creates, binds, and listens on . - /// Address, port, and dual-mode flag from . - /// A listening socket. The caller owns it. - internal static Socket CreateBoundListenSocket(ListenBinding binding) - { - return CreateBoundListenSocket(binding.EndPoint, binding.DualMode); - } - - /// Creates, binds, and listens on . - /// Local TCP endpoint. IPv6 endpoints may also set dual mode. - /// - /// When and is IPv6, the socket accepts IPv4-mapped connections. - /// Ignored for IPv4. - /// - /// A listening socket. The caller owns it. - /// Thrown when is null. - /// Any exception from bind or listen disposes the new socket and is rethrown. - private static Socket CreateBoundListenSocket(IPEndPoint endpoint, bool dualMode = false) - { - ArgumentNullException.ThrowIfNull(endpoint); - using var ownership = new DisposableOwnership( - new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)); - var socket = ownership.Resource; - - if (endpoint.AddressFamily == AddressFamily.InterNetworkV6) - { - socket.DualMode = dualMode; - } - - socket.Bind(endpoint); - socket.Listen(512); - return ownership.Transfer(); - } - } -} diff --git a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs index c9745512..bfaba99f 100644 --- a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs +++ b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs @@ -12,9 +12,9 @@ using System.Net.Sockets; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.Common.Networking.Listeners; -using System.Net.Security; -using System.Security.Cryptography.X509Certificates; using VectorNNTPD.BackFillerService.Networking; +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.BackFillerService.Listener { @@ -26,10 +26,10 @@ namespace VectorNNTPD.BackFillerService.Listener /// /// Start requires and /// . Bind plans come from - /// . Each admitted socket gets one TLS handshake - /// (TLS 1.2 or 1.3, no client certificate, revocation checks off) and one - /// . The certificate lease is acquired per connection and disposed - /// with that connection. This service does not reload retired certificate generations. + /// . Each admitted socket is started through + /// (shared Common pipeline transport) and one + /// . The certificate lease is acquired by the shared + /// connection and disposed with that connection. This service does not reload retired certificate generations. /// The session does not own the socket or the certificate. /// /// @@ -71,13 +71,13 @@ internal sealed class CacheListenerService : IHostedService, IApplicationService private readonly IListenerDnsPublication? _publication; /// - /// Non-recursive lock for and . + /// Non-recursive lock for and . /// No caller enters it again on the same call stack. /// private readonly Lock _gate = new(); - /// Bound listen sockets. Mutated only while is held. - private readonly List _listenSockets = []; + /// Bound accept listeners. Mutated only while is held. + private readonly List _listeners = []; /// /// In-flight connection tasks. The value is unused. Entries are removed when the task completes. @@ -227,7 +227,8 @@ private static AcmeCertificateReadiness CreateReadyGate() /// /// /// treats a faulted or unexpectedly completed task as termination. - /// The task is the loop started by . + /// The task is over each + /// . /// public Task? Execution => _acceptTask; @@ -253,8 +254,8 @@ internal CacheListenerState State /// Gets bound listen endpoints (tests). /// - /// Copies for each socket currently in - /// while is held. Entries can be null after a socket is closed. + /// Copies for each listener currently in + /// while is held. /// internal IReadOnlyList LocalEndPoints { @@ -262,7 +263,7 @@ internal IReadOnlyList LocalEndPoints { lock (_gate) { - return [.. _listenSockets.Select(static socket => socket.LocalEndPoint)]; + return [.. _listeners.Select(static listener => (EndPoint?)listener.LocalEndPoint)]; } } } @@ -316,7 +317,7 @@ public async Task StartAsync(CancellationToken cancellationToken) CacheListenerLogMessages.Starting(_logger, _runtime.BindPortTls, bindings.Count); BindEndpoints(bindings); - if (_listenSockets.Count == 0) + if (_listeners.Count == 0) { throw new InvalidOperationException("Cache Listener failed to bind any endpoint."); } @@ -333,12 +334,13 @@ public async Task StartAsync(CancellationToken cancellationToken) CacheListenerLogMessages.Running( _logger, - _listenSockets.Count, + _listeners.Count, _runtime.BindPortTls, _runtime.Listener.SendBufferBytes, _runtime.Listener.ReceiveBufferBytes); _journal.Record(BackFillerStartupStages.ListenerStarted); - _acceptTask = AcceptAllAsync(_runCts.Token); + _acceptTask = Task.WhenAll( + _listeners.Select(static listener => listener.AcceptLoopTask ?? Task.CompletedTask)); } catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex)) { @@ -395,7 +397,7 @@ public async ValueTask DisposeAsync() CacheListenerLogMessages.Retiring(_logger); await _runCts.CancelAsync().ConfigureAwait(false); - CloseListenSockets(); + await CloseListenSocketsAsync().ConfigureAwait(false); Exception? unexpectedAcceptFault = null; var accept = _acceptTask; @@ -511,7 +513,7 @@ private void ObserveExpectedAcceptLoopFaultDuringDispose(Exception exception) => /// /// Reports whether every flattened inner exception is an expected accept-loop fault type. /// - /// Fault from via . + /// Fault from over accept loops. /// /// when every inner is , /// , , or @@ -552,28 +554,54 @@ private void BindEndpoints(IReadOnlyList bindings) { foreach (var binding in bindings) { + SocketAcceptListener? listener = null; try { - var socket = CacheListenerEndpoints.CreateBoundListenSocket(binding); + listener = new SocketAcceptListener( + binding, + OnAcceptedAsync, + _logger, + maxConsecutiveAcceptFailures: MaxConsecutiveAcceptFailures, + acceptFailureRetryDelay: AcceptFailureRetryDelay, + acceptFailureDelayAsync: AcceptFailureDelayAsync, + acceptFailureObserver: new CacheListenerAcceptFailureObserver(_logger), + acceptFailureLogInterval: AcceptFailureLogInterval, + reuseAddress: false) + { + AcceptAsyncOverride = AcceptAsyncOverride, + }; + listener.Start(); lock (_gate) { - _listenSockets.Add(socket); + _listeners.Add(listener); } - if (!_logger.IsEnabled(LogLevel.Information)) + listener = null; + if (_logger.IsEnabled(LogLevel.Information)) { - continue; + var endpoint = binding.EndPoint.ToString(); + var addressFamily = binding.Address.AddressFamily.ToString(); + CacheListenerLogMessages.EndpointBound(_logger, endpoint, addressFamily); } - - var endpoint = binding.EndPoint.ToString(); - var addressFamily = binding.Address.AddressFamily.ToString(); - CacheListenerLogMessages.EndpointBound(_logger, endpoint, addressFamily); } catch (SocketException ex) when ( IsImplicitWildcard(binding.EndPoint) && ex.SocketErrorCode is SocketError.AddressFamilyNotSupported or SocketError.ProtocolNotSupported or SocketError.AddressNotAvailable) { CacheListenerLogMessages.WildcardFamilySkipped(_logger, binding.Address.AddressFamily.ToString(), ex.SocketErrorCode.ToString()); + if (listener is not null) + { + _ = listener.DisposeAsync().AsTask(); + } + } + catch + { + if (listener is not null) + { + _ = listener.DisposeAsync().AsTask(); + } + + throw; } } } @@ -595,164 +623,40 @@ private bool IsImplicitWildcard(IPEndPoint endpoint) && (endpoint.Address.Equals(IPAddress.Any) || endpoint.Address.Equals(IPAddress.IPv6Any)); } - /// Runs one accept loop per socket that was bound when this method sampled the list. - /// Service run token. Cancelled by . - /// A task that completes when every sampled accept loop has exited. A fault in any loop faults the task. - private async Task AcceptAllAsync(CancellationToken cancellationToken) + /// + /// Admits one accepted socket, applies TCP options, and starts . + /// + private ValueTask OnAcceptedAsync(Socket accepted, CancellationToken cancellationToken) { - Socket[] sockets; - lock (_gate) + var remote = accepted.RemoteEndPoint?.ToString() ?? ""; + if (!TryAdmit()) { - sockets = [.. _listenSockets]; + CacheListenerLogMessages.CapacityRejected(_logger, remote, _runtime.Listener.MaxActiveConnections); + accepted.Dispose(); + return ValueTask.CompletedTask; } - await Task.WhenAll(sockets.Select(socket => AcceptLoopAsync(socket, cancellationToken))).ConfigureAwait(false); - } - - /// Accepts sockets until cancellation, disposal, or persistent accept failure. - /// Bound socket sampled by . - /// Service run token. - /// A task that completes when the loop breaks, or faults after consecutive accept failures. - /// - /// Cancellation, , and end the loop - /// when is cancelled. A while the token - /// is still active is logged (rate-limited), delayed by , and retried. - /// After consecutive failures the loop throws - /// so faults and the host health path observes it. - /// A successful accept resets the consecutive-failure count. Over-capacity sockets are disposed and not processed. - /// An admitted socket is configured with before . - /// Option failure disposes that socket, releases the admission slot, and continues the loop. - /// Admitted work is tracked in until the task completes. - /// - private async Task AcceptLoopAsync(Socket listenSocket, CancellationToken cancellationToken) - { - var consecutiveFailures = 0; - DateTimeOffset? lastFailureLogUtc = null; - - while (!cancellationToken.IsCancellationRequested) + if (!TryConfigureAcceptedSocket(accepted, remote)) { - Socket accepted; - try - { - accepted = AcceptAsyncOverride is not null - ? await AcceptAsyncOverride(listenSocket, cancellationToken).ConfigureAwait(false) - : await listenSocket.AcceptAsync(cancellationToken).ConfigureAwait(false); - - if (consecutiveFailures > 0) - { - CacheListenerLogMessages.AcceptRecovered(_logger, consecutiveFailures); - consecutiveFailures = 0; - lastFailureLogUtc = null; - } - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (SocketException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (SocketException ex) - { - if (cancellationToken.IsCancellationRequested) - { - break; - } - - consecutiveFailures++; - var socketError = ex.SocketErrorCode.ToString(); - var now = _time.GetUtcNow(); - if (ShouldLogAcceptFailure(consecutiveFailures, lastFailureLogUtc, now)) - { - CacheListenerLogMessages.AcceptFailed( - _logger, - socketError, - consecutiveFailures, - MaxConsecutiveAcceptFailures); - lastFailureLogUtc = now; - } - - if (consecutiveFailures >= MaxConsecutiveAcceptFailures) - { - CacheListenerLogMessages.AcceptLoopStopping(_logger, consecutiveFailures, socketError); - throw new InvalidOperationException( - $"Cache Listener accept failed {consecutiveFailures} consecutive times (last error: {socketError}).", - ex); - } - - try - { - if (AcceptFailureDelayAsync is not null) - { - await AcceptFailureDelayAsync(AcceptFailureRetryDelay, cancellationToken) - .ConfigureAwait(false); - } - else - { - await Task.Delay(AcceptFailureRetryDelay, _time, cancellationToken) - .ConfigureAwait(false); - } - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - break; - } - - continue; - } - - var remote = accepted.RemoteEndPoint?.ToString() ?? ""; - if (!TryAdmit()) - { - CacheListenerLogMessages.CapacityRejected(_logger, remote, _runtime.Listener.MaxActiveConnections); - accepted.Dispose(); - continue; - } - - if (!TryConfigureAcceptedSocket(accepted, remote)) - { - ReleaseAdmit(); - continue; - } - - CacheListenerLogMessages.Accepted(_logger, remote, ActiveConnections); - var connection = ProcessAsync(accepted, remote, cancellationToken); - _connections[connection] = 0; - _ = connection.ContinueWith( - static (task, state) => ((CacheListenerService)state!)._connections.TryRemove(task, out _), - this, - TaskScheduler.Default); - } - } - - /// - /// Reports whether an accept failure should emit a Warning log given consecutive count and prior log time. - /// - /// Failures since the last successful accept on this loop. - /// UTC of the last Warning for this loop, or null when none yet. - /// Current UTC from the service . - /// for the first failure, threshold crossing, or after . - private static bool ShouldLogAcceptFailure( - int consecutiveFailures, - DateTimeOffset? lastFailureLogUtc, - DateTimeOffset nowUtc) - { - if (consecutiveFailures == 1 || consecutiveFailures >= MaxConsecutiveAcceptFailures) - { - return true; + ReleaseAdmit(); + return ValueTask.CompletedTask; } - return lastFailureLogUtc is null - || nowUtc - lastFailureLogUtc.Value >= AcceptFailureLogInterval; + CacheListenerLogMessages.Accepted(_logger, remote, ActiveConnections); + var connection = ProcessAsync(accepted, remote, cancellationToken); + _connections[connection] = 0; + _ = connection.ContinueWith( + static (task, state) => ((CacheListenerService)state!)._connections.TryRemove(task, out _), + this, + TaskScheduler.Default); + return ValueTask.CompletedTask; } /// Handshakes one accepted socket and runs a single NNTP session. - /// Accepted socket. Owned by the created here. + /// + /// Accepted socket. Ownership transfers to on a successful TLS start; + /// otherwise this method disposes it. + /// /// Remote endpoint text captured before processing, used only in logs. /// Service run token passed to the handshake link and the NNTP session. /// A task that completes when the connection has been released from the admission count. @@ -760,33 +664,38 @@ private static bool ShouldLogAcceptFailure( /// The handshake uses a linked token cancelled after . /// That timeout becomes with message tls-handshake. After the handshake, /// applies - /// as one no-progress deadline per read or non-empty write, and leaves the open so the network stream still owns the socket. + /// as one no-progress deadline per read or non-empty write over the shared pipeline. /// from the service token is ignored. /// and are logged. - /// is not logged here. Other exceptions are logged as . + /// is logged as a closed connection. Other exceptions are logged as + /// . /// always runs. /// private async Task ProcessAsync(Socket accepted, string remote, CancellationToken cancellationToken) { var handshakeCompleted = false; + var socketOwnedByConnection = false; try { - await using var network = new NetworkStream(accepted, ownsSocket: true); - using var lease = _certificates.Acquire(); - await using var ssl = new SslStream(network, leaveInnerStreamOpen: true); - var options = new SslServerAuthenticationOptions + if (accepted.RemoteEndPoint is not IPEndPoint tcpPeer) { - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, - ClientCertificateRequired = false, - CertificateRevocationCheckMode = X509RevocationMode.NoCheck, - ServerCertificateContext = lease.Context, - }; + return; + } + var identity = ConnectionClientIdentity.Direct(tcpPeer); using var handshakeCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); handshakeCts.CancelAfter(_runtime.Listener.TlsHandshakeTimeout); + NntpConnection connection; try { - await ssl.AuthenticateAsServerAsync(options, handshakeCts.Token).ConfigureAwait(false); + connection = await NntpConnection.StartTlsAsync( + accepted, + _certificates, + identity, + _logger, + handshakeCts.Token) + .ConfigureAwait(false); + socketOwnedByConnection = true; } catch (OperationCanceledException) when (handshakeCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) { @@ -794,13 +703,17 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke } handshakeCompleted = true; - // The NNTP session starts only after TLS succeeds. It does not own the socket or the certificate lease. - await using var transport = new StreamCacheListenerTransport( - ssl, - _runtime.Listener.IoProgressTimeout, - leaveInnerStreamOpen: true); - var session = new NntpCacheListenerSession(transport, _retention, _time); - await session.RunAsync(cancellationToken).ConfigureAwait(false); + // The NNTP session starts only after TLS succeeds. Shared pipeline transport owns the socket and certificate lease. + await using (connection) + { + await using var stream = new NntpConnectionStream(connection, ownsConnection: false); + await using var transport = new StreamCacheListenerTransport( + stream, + _runtime.Listener.IoProgressTimeout, + leaveInnerStreamOpen: true); + var session = new NntpCacheListenerSession(transport, _retention, _time); + await session.RunAsync(cancellationToken).ConfigureAwait(false); + } } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { @@ -828,6 +741,17 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke } finally { + if (!socketOwnedByConnection) + { + try + { + accepted.Dispose(); + } + catch (ObjectDisposedException) + { + } + } + ReleaseAdmit(); } } @@ -900,28 +824,29 @@ private bool TryAdmit() /// private void ReleaseAdmit() => Interlocked.Decrement(ref _activeConnections); - /// Disposes every listen socket and clears the list. Dispose exceptions are ignored. - private void CloseListenSockets() + /// Disposes every accept listener and clears the list. Dispose exceptions are ignored. + private async Task CloseListenSocketsAsync() { + SocketAcceptListener[] listeners; lock (_gate) { - foreach (var socket in _listenSockets) + listeners = [.. _listeners]; + _listeners.Clear(); + } + + foreach (var listener in listeners) + { + try { - try - { - socket.Dispose(); - } - catch (ObjectDisposedException) - { - // Socket may already be closed by a concurrent listener shutdown path. - } - catch (SocketException ex) - { - CacheListenerLogMessages.ListenSocketDisposeFailed(_logger, ex.SocketErrorCode.ToString()); - } + await listener.DisposeAsync().ConfigureAwait(false); + } + catch (ObjectDisposedException) + { + } + catch (SocketException ex) + { + CacheListenerLogMessages.ListenSocketDisposeFailed(_logger, ex.SocketErrorCode.ToString()); } - - _listenSockets.Clear(); } } @@ -964,18 +889,12 @@ private async Task DrainConnectionsAsync() } } - /// Closes listen sockets. Certificate leases stay with the connections that acquired them. - /// A completed task after . + /// Closes accept listeners. Certificate leases stay with the connections that acquired them. + /// A task that completes after listeners are disposed. /// /// Certificate publication is owned by the Common TLS provider; outstanding leases /// keep retired generations alive until each connection completes. /// - private async Task DisposeBoundResourcesAsync() - { - CloseListenSockets(); - // Certificate publication is owned by the Common TLS provider; outstanding leases - // keep retired generations alive until each connection completes. - await Task.CompletedTask.ConfigureAwait(false); - } + private Task DisposeBoundResourcesAsync() => CloseListenSocketsAsync(); } } diff --git a/src/VectorNNTPD.Common/Networking/Diagnostics/ITcpByteDiagnostics.cs b/src/VectorNNTPD.Common/Networking/Diagnostics/ITcpByteDiagnostics.cs new file mode 100644 index 00000000..5877a575 --- /dev/null +++ b/src/VectorNNTPD.Common/Networking/Diagnostics/ITcpByteDiagnostics.cs @@ -0,0 +1,18 @@ +namespace VectorNNTPD.Common.Networking.Diagnostics +{ + /// + /// Optional TCP byte observability for duplex pipe connections. + /// + public interface ITcpByteDiagnostics + { + /// Gets whether probes should record bytes. + bool IsEnabled { get; } + + /// + /// Adds application-layer bytes read from an upstream connection (plain TCP payload, + /// or TLS-decrypted octets). No-op when disabled. + /// + /// Byte count to record. + void RecordTcpBytes(int bytes); + } +} diff --git a/src/VectorNNTPD.ReaderService/Networking/Listeners/ConnectionAcceptanceLogging.cs b/src/VectorNNTPD.Common/Networking/Listeners/ConnectionAcceptanceLogging.cs similarity index 96% rename from src/VectorNNTPD.ReaderService/Networking/Listeners/ConnectionAcceptanceLogging.cs rename to src/VectorNNTPD.Common/Networking/Listeners/ConnectionAcceptanceLogging.cs index 9a5c0a3b..aed0dcd2 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Listeners/ConnectionAcceptanceLogging.cs +++ b/src/VectorNNTPD.Common/Networking/Listeners/ConnectionAcceptanceLogging.cs @@ -1,8 +1,8 @@ using System.Net; using VectorNNTPD.Common.Networking; -using VectorNNTPD.ReaderService.Networking.Proxy; +using VectorNNTPD.Common.Networking.Proxy; -namespace VectorNNTPD.ReaderService.Networking.Listeners +namespace VectorNNTPD.Common.Networking.Listeners { /// Formats connection-acceptance Information logs (TCP peer vs PROXY effective client). internal static class ConnectionAcceptanceLogging diff --git a/src/VectorNNTPD.Common/Networking/Listeners/IListenAcceptFailureObserver.cs b/src/VectorNNTPD.Common/Networking/Listeners/IListenAcceptFailureObserver.cs new file mode 100644 index 00000000..cf118cfc --- /dev/null +++ b/src/VectorNNTPD.Common/Networking/Listeners/IListenAcceptFailureObserver.cs @@ -0,0 +1,23 @@ +namespace VectorNNTPD.Common.Networking.Listeners +{ + /// + /// Optional host telemetry for consecutive accept failures on . + /// + public interface IListenAcceptFailureObserver + { + /// Reports one accept . + /// Socket error code text. + /// Failures since the last successful accept. + /// Configured stop threshold. + void OnAcceptFailed(string socketError, int consecutiveFailures, int maxFailures); + + /// Reports that accepts resumed after one or more failures. + /// Consecutive failures cleared by the successful accept. + void OnAcceptRecovered(int recoveredAfterFailures); + + /// Reports that the accept loop is stopping after reaching the failure threshold. + /// Failure count that triggered the stop. + /// Last socket error code text. + void OnAcceptLoopStopping(int consecutiveFailures, string socketError); + } +} diff --git a/src/VectorNNTPD.ReaderService/Networking/Listeners/IListenSocketBinder.cs b/src/VectorNNTPD.Common/Networking/Listeners/IListenSocketBinder.cs similarity index 96% rename from src/VectorNNTPD.ReaderService/Networking/Listeners/IListenSocketBinder.cs rename to src/VectorNNTPD.Common/Networking/Listeners/IListenSocketBinder.cs index 0b7bd954..3df0d4cd 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Listeners/IListenSocketBinder.cs +++ b/src/VectorNNTPD.Common/Networking/Listeners/IListenSocketBinder.cs @@ -1,7 +1,7 @@ using System.Net; using System.Net.Sockets; -namespace VectorNNTPD.ReaderService.Networking.Listeners +namespace VectorNNTPD.Common.Networking.Listeners { /// /// Binds and listens on an already-created TCP socket. diff --git a/src/VectorNNTPD.TransitService/Networking/Listeners/SocketAcceptListener.cs b/src/VectorNNTPD.Common/Networking/Listeners/SocketAcceptListener.cs similarity index 51% rename from src/VectorNNTPD.TransitService/Networking/Listeners/SocketAcceptListener.cs rename to src/VectorNNTPD.Common/Networking/Listeners/SocketAcceptListener.cs index 1d233dc8..192ee791 100644 --- a/src/VectorNNTPD.TransitService/Networking/Listeners/SocketAcceptListener.cs +++ b/src/VectorNNTPD.Common/Networking/Listeners/SocketAcceptListener.cs @@ -3,7 +3,7 @@ using VectorNNTPD.Common.Networking; using VectorNNTPD.Common.Networking.Listeners; -namespace VectorNNTPD.TransitService.Networking.Listeners +namespace VectorNNTPD.Common.Networking.Listeners { using VectorNNTPD.Common.Core; /// @@ -21,23 +21,67 @@ public sealed class SocketAcceptListener : IAsyncDisposable private readonly IListenSocketBinder _binder; private readonly Socket _listenSocket; private readonly CancellationTokenSource _cts = new(); + private readonly int _maxConsecutiveAcceptFailures; + private readonly TimeSpan _acceptFailureRetryDelay; + private readonly TimeSpan _acceptFailureLogInterval; + private readonly Func? _acceptFailureDelayAsync; + private readonly IListenAcceptFailureObserver? _acceptFailureObserver; private Task? _acceptLoop; private int _stopped; private int _disposed; + /// + /// Optional accept replacement for tests. When null, production uses + /// . + /// + public Func>? AcceptAsyncOverride { get; set; } + /// Initializes a new instance of the class. + /// Planned local bind. + /// Per-connection handler. Failures are logged; the listen loop continues. + /// Listener logger. + /// Bind/listen ownership seam. Null uses the production binder. + /// + /// When greater than zero, the accept loop faults after this many consecutive + /// failures. Zero keeps the historical infinite-retry behaviour. + /// + /// Delay between consecutive accept failures when faulting is enabled. + /// Optional delay override for tests. + /// + /// Optional host-specific accept-failure telemetry (rate-limited warnings, recovery, stop). + /// When null, failures still emit shared events. + /// + /// + /// Minimum interval between observer failure logs for intermediate consecutive counts. + /// + /// + /// When (Reader/Transit default), sets SO_REUSEADDR. + /// When (Storage/BackFiller), keeps exclusive bind semantics. + /// public SocketAcceptListener( ListenBinding binding, Func onAccepted, ILogger logger, - IListenSocketBinder? listenBinder = null) + IListenSocketBinder? listenBinder = null, + int maxConsecutiveAcceptFailures = 0, + TimeSpan? acceptFailureRetryDelay = null, + Func? acceptFailureDelayAsync = null, + IListenAcceptFailureObserver? acceptFailureObserver = null, + TimeSpan? acceptFailureLogInterval = null, + bool reuseAddress = true) { ArgumentNullException.ThrowIfNull(onAccepted); ArgumentNullException.ThrowIfNull(logger); + ArgumentOutOfRangeException.ThrowIfNegative(maxConsecutiveAcceptFailures); _binding = binding; _onAccepted = onAccepted; _logger = logger; _binder = listenBinder ?? SocketListenBinder.Instance; + _maxConsecutiveAcceptFailures = maxConsecutiveAcceptFailures; + _acceptFailureRetryDelay = acceptFailureRetryDelay ?? TimeSpan.FromMilliseconds(250); + _acceptFailureDelayAsync = acceptFailureDelayAsync; + _acceptFailureObserver = acceptFailureObserver; + _acceptFailureLogInterval = acceptFailureLogInterval ?? TimeSpan.FromSeconds(5); var family = binding.Address.AddressFamily; _listenSocket = new Socket(family, SocketType.Stream, ProtocolType.Tcp); @@ -46,17 +90,21 @@ public SocketAcceptListener( _listenSocket.DualMode = binding.DualMode; } - // SO_REUSEADDR is retained: Bind success is ownership under these socket options. - // On Windows it can allow another process to bind the same endpoint; that is not a - // Bind failure and is not treated as exclusive port ownership. - try - { - _listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - } - catch (SocketException ex) + // SO_REUSEADDR is retained for Reader/Transit: Bind success is ownership under these + // socket options. On Windows it can allow another process to bind the same endpoint; + // that is not a Bind failure and is not treated as exclusive port ownership. + // Storage/BackFiller pass reuseAddress: false for exclusive bind semantics. + if (reuseAddress) { - _ = ex; // observed; intentionally ignored at this boundary - // Best-effort; some platforms differ. + try + { + _listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + } + catch (SocketException ex) + { + _ = ex; // observed; intentionally ignored at this boundary + // Best-effort; some platforms differ. + } } } @@ -70,6 +118,12 @@ public SocketAcceptListener( /// Gets whether the accept loop is still running (tests). internal bool AcceptLoopActive => _acceptLoop is { IsCompleted: false }; + /// + /// Gets the accept-loop task after , or before start. + /// Hosts may observe this as IApplicationService.Execution so accept-loop faults surface. + /// + public Task? AcceptLoopTask => _acceptLoop; + /// Binds, listens, and starts the accept loop. /// /// Bind/listen exceptions propagate to the caller and are startup failures. @@ -147,12 +201,23 @@ public async ValueTask DisposeAsync() private async Task AcceptLoopAsync(CancellationToken cancellationToken) { + var consecutiveFailures = 0; + DateTimeOffset? lastFailureLogUtc = null; while (!cancellationToken.IsCancellationRequested) { Socket accepted; try { - accepted = await _listenSocket.AcceptAsync(cancellationToken).ConfigureAwait(false); + accepted = AcceptAsyncOverride is not null + ? await AcceptAsyncOverride(_listenSocket, cancellationToken).ConfigureAwait(false) + : await _listenSocket.AcceptAsync(cancellationToken).ConfigureAwait(false); + + if (consecutiveFailures > 0) + { + _acceptFailureObserver?.OnAcceptRecovered(consecutiveFailures); + consecutiveFailures = 0; + lastFailureLogUtc = null; + } } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { @@ -167,17 +232,82 @@ private async Task AcceptLoopAsync(CancellationToken cancellationToken) NetworkingLogMessages.AcceptInterruptedDuringShutdown(_logger, ex); break; } + catch (SocketException ex) when (_maxConsecutiveAcceptFailures > 0) + { + consecutiveFailures++; + var socketError = ex.SocketErrorCode.ToString(); + var now = DateTimeOffset.UtcNow; + if (ShouldLogAcceptFailure(consecutiveFailures, lastFailureLogUtc, now)) + { + if (_acceptFailureObserver is not null) + { + _acceptFailureObserver.OnAcceptFailed( + socketError, + consecutiveFailures, + _maxConsecutiveAcceptFailures); + } + else + { + NetworkingLogMessages.AcceptFailed(_logger, ex, _binding.EndPoint); + } + + lastFailureLogUtc = now; + } + + if (consecutiveFailures >= _maxConsecutiveAcceptFailures) + { + _acceptFailureObserver?.OnAcceptLoopStopping(consecutiveFailures, socketError); + throw new InvalidOperationException( + $"Accept failed {consecutiveFailures} consecutive times (last error: {socketError}).", + ex); + } + + try + { + if (_acceptFailureDelayAsync is not null) + { + await _acceptFailureDelayAsync(_acceptFailureRetryDelay, cancellationToken) + .ConfigureAwait(false); + } + else + { + await Task.Delay(_acceptFailureRetryDelay, cancellationToken).ConfigureAwait(false); + } + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + break; + } + + continue; + } // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) + catch (Exception ex) when (_maxConsecutiveAcceptFailures == 0) { NetworkingLogMessages.AcceptFailed(_logger, ex, _binding.EndPoint); continue; } + // With a consecutive-failure policy, unexpected accept faults must surface on AcceptLoopTask. + _ = HandleAcceptedAsync(accepted, cancellationToken); } } + private bool ShouldLogAcceptFailure( + int consecutiveFailures, + DateTimeOffset? lastFailureLogUtc, + DateTimeOffset nowUtc) + { + if (consecutiveFailures == 1 || consecutiveFailures >= _maxConsecutiveAcceptFailures) + { + return true; + } + + return lastFailureLogUtc is null + || nowUtc - lastFailureLogUtc.Value >= _acceptFailureLogInterval; + } + private async Task HandleAcceptedAsync(Socket accepted, CancellationToken cancellationToken) { try diff --git a/src/VectorNNTPD.ReaderService/Networking/Proxy/ConnectionClientIdentity.cs b/src/VectorNNTPD.Common/Networking/Proxy/ConnectionClientIdentity.cs similarity index 98% rename from src/VectorNNTPD.ReaderService/Networking/Proxy/ConnectionClientIdentity.cs rename to src/VectorNNTPD.Common/Networking/Proxy/ConnectionClientIdentity.cs index ec264653..fb879643 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Proxy/ConnectionClientIdentity.cs +++ b/src/VectorNNTPD.Common/Networking/Proxy/ConnectionClientIdentity.cs @@ -1,6 +1,6 @@ using System.Net; -namespace VectorNNTPD.ReaderService.Networking.Proxy +namespace VectorNNTPD.Common.Networking.Proxy { /// /// Immutable connection-level client identity established once at accept time. diff --git a/src/VectorNNTPD.ReaderService/Networking/Proxy/PrefixedStream.cs b/src/VectorNNTPD.Common/Networking/Proxy/PrefixedStream.cs similarity index 98% rename from src/VectorNNTPD.ReaderService/Networking/Proxy/PrefixedStream.cs rename to src/VectorNNTPD.Common/Networking/Proxy/PrefixedStream.cs index 898bda82..f913ab9b 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Proxy/PrefixedStream.cs +++ b/src/VectorNNTPD.Common/Networking/Proxy/PrefixedStream.cs @@ -1,4 +1,4 @@ -namespace VectorNNTPD.ReaderService.Networking.Proxy +namespace VectorNNTPD.Common.Networking.Proxy { /// /// Stream that yields a fixed prefix before reading from an inner stream (TLS leftover after PROXY). diff --git a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IOutboundRateCap.cs b/src/VectorNNTPD.Common/Networking/RateLimiting/IOutboundRateCap.cs similarity index 89% rename from src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IOutboundRateCap.cs rename to src/VectorNNTPD.Common/Networking/RateLimiting/IOutboundRateCap.cs index 71b9efff..e9ae1a68 100644 --- a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IOutboundRateCap.cs +++ b/src/VectorNNTPD.Common/Networking/RateLimiting/IOutboundRateCap.cs @@ -1,4 +1,4 @@ -namespace VectorNNTPD.ReaderService.SessionState.RateLimiting +namespace VectorNNTPD.Common.Networking.RateLimiting { /// Dynamically adjustable outbound bytes-per-second cap. public interface IOutboundRateCap diff --git a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/OutboundRateLimiter.cs b/src/VectorNNTPD.Common/Networking/RateLimiting/OutboundRateLimiter.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/SessionState/RateLimiting/OutboundRateLimiter.cs rename to src/VectorNNTPD.Common/Networking/RateLimiting/OutboundRateLimiter.cs index d979ef8d..07f6816c 100644 --- a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/OutboundRateLimiter.cs +++ b/src/VectorNNTPD.Common/Networking/RateLimiting/OutboundRateLimiter.cs @@ -1,4 +1,4 @@ -namespace VectorNNTPD.ReaderService.SessionState.RateLimiting +namespace VectorNNTPD.Common.Networking.RateLimiting { /// /// Stream wrapper that throttles writes to a dynamically adjustable bytes/sec cap. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/ConnectionByteTransport.cs b/src/VectorNNTPD.Common/Networking/Transport/ConnectionByteTransport.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/ConnectionByteTransport.cs rename to src/VectorNNTPD.Common/Networking/Transport/ConnectionByteTransport.cs index f0538eaf..5aa05e11 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/ConnectionByteTransport.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/ConnectionByteTransport.cs @@ -1,8 +1,8 @@ using System.Diagnostics; using System.Net.Security; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// Single owner of byte-stream I/O under pumps. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/INntpConnection.cs b/src/VectorNNTPD.Common/Networking/Transport/INntpConnection.cs similarity index 98% rename from src/VectorNNTPD.ReaderService/Networking/Transport/INntpConnection.cs rename to src/VectorNNTPD.Common/Networking/Transport/INntpConnection.cs index ba2918ad..9fd34a4f 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/INntpConnection.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/INntpConnection.cs @@ -1,10 +1,10 @@ using System.IO.Pipelines; using System.Net; using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.RateLimiting; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// Byte-oriented NNTP connection transport exposed to a future session/protocol layer. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/NntpConnection.cs b/src/VectorNNTPD.Common/Networking/Transport/NntpConnection.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/NntpConnection.cs rename to src/VectorNNTPD.Common/Networking/Transport/NntpConnection.cs index 2e0308d3..3fa27b9a 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/NntpConnection.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/NntpConnection.cs @@ -6,13 +6,13 @@ using System.Net.Sockets; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; -using VectorNNTPD.ReaderService.Diagnostics; +using VectorNNTPD.Common.Networking.Diagnostics; using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.ReaderService.Networking.Listeners; -using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Listeners; +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.RateLimiting; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { using VectorNNTPD.Common.Core; /// @@ -51,7 +51,7 @@ public sealed class NntpConnection : INntpConnection private readonly CancellationTokenSource _connectionCts = new(); private readonly object _completeGate = new(); private readonly ConnectionClientIdentity _clientIdentity; - private readonly IFeedDiagnostics? _feedDiagnostics; + private readonly ITcpByteDiagnostics? _feedDiagnostics; private Socket? _socket; private ConnectionByteTransport? _transport; private TlsCertificateLease? _certificateLease; @@ -84,7 +84,7 @@ private NntpConnection( bool isTls, ConnectionClientIdentity clientIdentity, ILogger logger, - IFeedDiagnostics? feedDiagnostics = null) + ITcpByteDiagnostics? feedDiagnostics = null) { _socket = socket; _transport = transport; @@ -208,7 +208,7 @@ public static NntpConnection StartPlain( ConnectionClientIdentity clientIdentity, ILogger logger, ReadOnlyMemory receivePrefix = default, - IFeedDiagnostics? feedDiagnostics = null) + ITcpByteDiagnostics? feedDiagnostics = null) { ArgumentNullException.ThrowIfNull(socket); ArgumentNullException.ThrowIfNull(clientIdentity); @@ -248,7 +248,7 @@ public static async Task StartTlsAsync( ILogger logger, CancellationToken cancellationToken, ReadOnlyMemory tlsPrefix = default, - IFeedDiagnostics? feedDiagnostics = null) + ITcpByteDiagnostics? feedDiagnostics = null) { ArgumentNullException.ThrowIfNull(socket); ArgumentNullException.ThrowIfNull(certificateProvider); diff --git a/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs b/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs new file mode 100644 index 00000000..978b0d34 --- /dev/null +++ b/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs @@ -0,0 +1,240 @@ +using System.Buffers; +using System.IO.Pipelines; +using System.Net.Sockets; + +namespace VectorNNTPD.Common.Networking.Transport +{ + /// + /// adapter over an duplex pipe pair. + /// + /// + /// + /// Reads consume ; writes produce to + /// and flush. Disposal completes and disposes the + /// underlying connection when is true. + /// Used by Storage and BackFiller session layers that speak stream APIs over the shared + /// pipeline transport. + /// + /// + /// Write cancellation contract: each chunk is copied into the + /// and Advanced before FlushAsync. The pipe API cannot roll back advanced bytes. + /// If cancellation is observed during or after a flush, those committed bytes may still be + /// delivered to the peer (or remain readable from the outbound pipe). A caller that retries the + /// same buffer after can therefore duplicate data. + /// does + /// not report how many bytes were committed before cancellation. + /// + /// + public sealed class NntpConnectionStream : Stream + { + private const int WriteChunkBytes = 64 * 1024; + + private readonly INntpConnection _connection; + private readonly bool _ownsConnection; + private int _disposed; + + /// Initializes a stream over . + /// Live duplex pipe connection with pumps running. + /// + /// When , disposing this stream completes and disposes + /// . + /// + public NntpConnectionStream(INntpConnection connection, bool ownsConnection = true) + { + ArgumentNullException.ThrowIfNull(connection); + _connection = connection; + _ownsConnection = ownsConnection; + } + + /// + public override bool CanRead => true; + + /// + public override bool CanSeek => false; + + /// + public override bool CanWrite => true; + + /// + public override long Length => throw new NotSupportedException(); + + /// + public override long Position + { + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); + } + + /// + public override void Flush() + { + } + + /// + public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask; + + /// + public override int Read(byte[] buffer, int offset, int count) => + throw new NotSupportedException("Synchronous reads are not supported on NntpConnectionStream."); + + /// + public override void Write(byte[] buffer, int offset, int count) => + throw new NotSupportedException("Synchronous writes are not supported on NntpConnectionStream."); + + /// + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); + + /// + public override void SetLength(long value) => throw new NotSupportedException(); + + /// + public override async ValueTask ReadAsync( + Memory buffer, + CancellationToken cancellationToken = default) + { + ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); + if (buffer.IsEmpty) + { + return 0; + } + + while (true) + { + var result = await _connection.Input.ReadAsync(cancellationToken).ConfigureAwait(false); + var sequence = result.Buffer; + // Exactly one AdvanceTo per ReadAsync. Default keeps unread bytes and examines nothing + // so a failure before copy progress re-presents the same buffer. + var consumed = sequence.Start; + var examined = sequence.Start; + try + { + if (result.IsCanceled) + { + cancellationToken.ThrowIfCancellationRequested(); + throw new OperationCanceledException(cancellationToken); + } + + if (sequence.IsEmpty) + { + examined = sequence.End; + if (result.IsCompleted) + { + return 0; + } + + continue; + } + + var copied = CopyToBuffer(sequence, buffer.Span); + consumed = sequence.GetPosition(copied); + // Examine only through consumed. Advancing examined to sequence.End while + // leaving unread bytes would make the next ReadAsync wait for more writes + // instead of returning the remainder. + examined = consumed; + return copied; + } + finally + { + _connection.Input.AdvanceTo(consumed, examined); + } + } + } + + /// + public override async ValueTask WriteAsync( + ReadOnlyMemory buffer, + CancellationToken cancellationToken = default) + { + ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); + cancellationToken.ThrowIfCancellationRequested(); + if (buffer.IsEmpty) + { + return; + } + + // Every GetMemory/GetSpan is paired with Advance for that slice. Large payloads are + // chunked and flushed per chunk so pause-writer backpressure can apply. + var remaining = buffer; + FlushResult flush; + do + { + var memory = _connection.Output.GetMemory(Math.Min(remaining.Length, WriteChunkBytes)); + var toCopy = Math.Min(memory.Length, remaining.Length); + remaining.Span[..toCopy].CopyTo(memory.Span); + _connection.Output.Advance(toCopy); + remaining = remaining[toCopy..]; + + flush = await _connection.Output.FlushAsync(cancellationToken).ConfigureAwait(false); + if (flush.IsCanceled) + { + cancellationToken.ThrowIfCancellationRequested(); + throw new OperationCanceledException(cancellationToken); + } + + if (flush.IsCompleted) + { + throw new IOException("NNTP connection output completed while writing."); + } + } + while (!remaining.IsEmpty); + } + + /// + public override async ValueTask DisposeAsync() + { + if (Interlocked.Exchange(ref _disposed, 1) == 1) + { + return; + } + + if (_ownsConnection) + { + try + { + await _connection.CompleteAsync().ConfigureAwait(false); + } + catch (ObjectDisposedException) + { + } + catch (IOException) + { + } + catch (SocketException) + { + } + + await _connection.DisposeAsync().ConfigureAwait(false); + } + + await base.DisposeAsync().ConfigureAwait(false); + } + + /// + protected override void Dispose(bool disposing) + { + if (disposing && Interlocked.Exchange(ref _disposed, 1) == 0 && _ownsConnection) + { + _connection.DisposeAsync().AsTask().GetAwaiter().GetResult(); + } + + base.Dispose(disposing); + } + + private static int CopyToBuffer(ReadOnlySequence sequence, Span destination) + { + var copied = 0; + var position = sequence.Start; + while (copied < destination.Length && sequence.TryGet(ref position, out var memory, advance: true)) + { + var available = destination.Length - copied; + var slice = memory.Length <= available + ? memory.Span + : memory.Span[..available]; + slice.CopyTo(destination[copied..]); + copied += slice.Length; + } + + return copied; + } + } +} diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/NntpDeflateStream.cs b/src/VectorNNTPD.Common/Networking/Transport/NntpDeflateStream.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/NntpDeflateStream.cs rename to src/VectorNNTPD.Common/Networking/Transport/NntpDeflateStream.cs index 50c24304..73276994 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/NntpDeflateStream.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/NntpDeflateStream.cs @@ -1,6 +1,6 @@ using System.IO.Compression; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// Bidirectional raw DEFLATE transform for an NNTP connection byte stream (RFC 8054 / RFC 1951). diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/NntpPipeOptions.cs b/src/VectorNNTPD.Common/Networking/Transport/NntpPipeOptions.cs similarity index 97% rename from src/VectorNNTPD.ReaderService/Networking/Transport/NntpPipeOptions.cs rename to src/VectorNNTPD.Common/Networking/Transport/NntpPipeOptions.cs index 392fd101..72080534 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/NntpPipeOptions.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/NntpPipeOptions.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// Centralized defaults for NNTP connection transports. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/SocketPayloadSender.cs b/src/VectorNNTPD.Common/Networking/Transport/SocketPayloadSender.cs similarity index 97% rename from src/VectorNNTPD.ReaderService/Networking/Transport/SocketPayloadSender.cs rename to src/VectorNNTPD.Common/Networking/Transport/SocketPayloadSender.cs index 4394d201..40cc222a 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/SocketPayloadSender.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/SocketPayloadSender.cs @@ -1,6 +1,6 @@ using System.Net.Sockets; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// Completes a TCP send of an entire buffer, looping on partial sends. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TcpDisconnectReason.cs b/src/VectorNNTPD.Common/Networking/Transport/TcpDisconnectReason.cs similarity index 96% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TcpDisconnectReason.cs rename to src/VectorNNTPD.Common/Networking/Transport/TcpDisconnectReason.cs index 84d47d41..8257800c 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TcpDisconnectReason.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TcpDisconnectReason.cs @@ -1,4 +1,4 @@ -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// Why an established TCP connection completed. Values are only those the existing diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TlsNegotiationLogging.cs b/src/VectorNNTPD.Common/Networking/Transport/TlsNegotiationLogging.cs similarity index 96% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TlsNegotiationLogging.cs rename to src/VectorNNTPD.Common/Networking/Transport/TlsNegotiationLogging.cs index 077a8d79..007609ae 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TlsNegotiationLogging.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TlsNegotiationLogging.cs @@ -1,7 +1,7 @@ using System.Net.Security; using System.Security.Authentication; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// Formats negotiated parameters for operational logs. internal static class TlsNegotiationLogging diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoProbe.cs b/src/VectorNNTPD.Common/Networking/Transport/TransportIoProbe.cs similarity index 98% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoProbe.cs rename to src/VectorNNTPD.Common/Networking/Transport/TransportIoProbe.cs index e87e37a8..a31eca45 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoProbe.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TransportIoProbe.cs @@ -1,4 +1,4 @@ -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// DIAGNOSTIC-ONLY. Off-by-default ConnectionByteTransport socket/Pipe cadence sampler. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoReport.cs b/src/VectorNNTPD.Common/Networking/Transport/TransportIoReport.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoReport.cs rename to src/VectorNNTPD.Common/Networking/Transport/TransportIoReport.cs index 7456e665..c4e87402 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoReport.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TransportIoReport.cs @@ -1,7 +1,7 @@ using System.Globalization; using System.Text; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// DIAGNOSTIC-ONLY formatter for . internal static class TransportIoReport diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoSession.cs b/src/VectorNNTPD.Common/Networking/Transport/TransportIoSession.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoSession.cs rename to src/VectorNNTPD.Common/Networking/Transport/TransportIoSession.cs index 6332fa02..503addc4 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportIoSession.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TransportIoSession.cs @@ -1,7 +1,7 @@ using System.Buffers; using System.Diagnostics; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// DIAGNOSTIC-ONLY per-connection aggregate of stream Read/Write/Flush and pipe wait times. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportLogMessages.cs b/src/VectorNNTPD.Common/Networking/Transport/TransportLogMessages.cs similarity index 95% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TransportLogMessages.cs rename to src/VectorNNTPD.Common/Networking/Transport/TransportLogMessages.cs index 4b350ba1..bcb0f6cd 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TransportLogMessages.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TransportLogMessages.cs @@ -1,6 +1,6 @@ using System.Net; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// Source-generated NNTP connection transport log messages. internal static partial class TransportLogMessages diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TxPipePauseExperiment.cs b/src/VectorNNTPD.Common/Networking/Transport/TxPipePauseExperiment.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TxPipePauseExperiment.cs rename to src/VectorNNTPD.Common/Networking/Transport/TxPipePauseExperiment.cs index 39b545f6..4599201b 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TxPipePauseExperiment.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TxPipePauseExperiment.cs @@ -1,4 +1,4 @@ -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// DIAGNOSTIC-ONLY. Off-by-default TX output-pipe pause-threshold override. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TxWriteCoalescer.cs b/src/VectorNNTPD.Common/Networking/Transport/TxWriteCoalescer.cs similarity index 97% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TxWriteCoalescer.cs rename to src/VectorNNTPD.Common/Networking/Transport/TxWriteCoalescer.cs index a143f75d..fc645c5d 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TxWriteCoalescer.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TxWriteCoalescer.cs @@ -1,6 +1,6 @@ using System.Diagnostics; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// DIAGNOSTIC-ONLY reusable contiguous buffer for experimental TX write aggregation. diff --git a/src/VectorNNTPD.ReaderService/Networking/Transport/TxWriteGranularityExperiment.cs b/src/VectorNNTPD.Common/Networking/Transport/TxWriteGranularityExperiment.cs similarity index 99% rename from src/VectorNNTPD.ReaderService/Networking/Transport/TxWriteGranularityExperiment.cs rename to src/VectorNNTPD.Common/Networking/Transport/TxWriteGranularityExperiment.cs index 030b7f7b..30c241ce 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Transport/TxWriteGranularityExperiment.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/TxWriteGranularityExperiment.cs @@ -1,6 +1,6 @@ using System.Runtime.CompilerServices; -namespace VectorNNTPD.ReaderService.Networking.Transport +namespace VectorNNTPD.Common.Networking.Transport { /// /// DIAGNOSTIC-ONLY. Off-by-default TX write-granularity override. diff --git a/src/VectorNNTPD.Common/VectorNNTPD.Common.csproj b/src/VectorNNTPD.Common/VectorNNTPD.Common.csproj index e5317bb7..4aaee0a0 100644 --- a/src/VectorNNTPD.Common/VectorNNTPD.Common.csproj +++ b/src/VectorNNTPD.Common/VectorNNTPD.Common.csproj @@ -5,7 +5,7 @@ x64 VectorNNTPD.Common VectorNNTPD.Common - Shared ACME, Cloudflare DNS, bind-address, NntpDB, article-content primitives, Diablo article classification, ArticleRecord, ArticleId, OverviewDB protobuf wire contract, StorageServer cache-fleet advertisement JSON contract and topology name constants, byte-oriented article parsing, yEnc validation, canonical article materialization, Vector Article Transfer Protocol (VATP) framing, and RabbitMQ connectivity (connection lifecycle, channels) for VectorNNTP applications. RabbitMQ topology declaration, ArticleWork, and Management HTTP remain application-owned. + Shared ACME, Cloudflare DNS, bind-address, high-performance listen/accept and System.IO.Pipelines duplex transport (SocketAcceptListener, NntpConnection), NntpDB, article-content primitives, Diablo article classification, ArticleRecord, ArticleId, OverviewDB protobuf wire contract, StorageServer cache-fleet advertisement JSON contract and topology name constants, byte-oriented article parsing, yEnc validation, canonical article materialization, Vector Article Transfer Protocol (VATP) framing, and RabbitMQ connectivity (connection lifecycle, channels) for VectorNNTP applications. RabbitMQ topology declaration, ArticleWork, NNTP session/command servers, and Management HTTP remain application-owned. false Library diff --git a/src/VectorNNTPD.ReaderService/ArticleIngestion/ArticleRecordIngress.cs b/src/VectorNNTPD.ReaderService/ArticleIngestion/ArticleRecordIngress.cs index ebd98e80..7a9fc5a5 100644 --- a/src/VectorNNTPD.ReaderService/ArticleIngestion/ArticleRecordIngress.cs +++ b/src/VectorNNTPD.ReaderService/ArticleIngestion/ArticleRecordIngress.cs @@ -2,7 +2,7 @@ using VectorNNTPD.Common.Articles.Parsing; using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.Session.Framing; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.ArticleIngestion { /// diff --git a/src/VectorNNTPD.ReaderService/ArticleIngestion/InboundArticle.cs b/src/VectorNNTPD.ReaderService/ArticleIngestion/InboundArticle.cs index de3c7559..d5b8f796 100644 --- a/src/VectorNNTPD.ReaderService/ArticleIngestion/InboundArticle.cs +++ b/src/VectorNNTPD.ReaderService/ArticleIngestion/InboundArticle.cs @@ -1,6 +1,6 @@ using VectorNNTPD.Common.Articles; using VectorNNTPD.ReaderService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.ArticleIngestion { /// diff --git a/src/VectorNNTPD.ReaderService/Authentication/NntpAccountPolicy.cs b/src/VectorNNTPD.ReaderService/Authentication/NntpAccountPolicy.cs index aed878b8..63746619 100644 --- a/src/VectorNNTPD.ReaderService/Authentication/NntpAccountPolicy.cs +++ b/src/VectorNNTPD.ReaderService/Authentication/NntpAccountPolicy.cs @@ -1,5 +1,5 @@ using VectorNNTPD.Common.Articles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Authentication { /// diff --git a/src/VectorNNTPD.ReaderService/Diagnostics/FeedDiagnosticsService.cs b/src/VectorNNTPD.ReaderService/Diagnostics/FeedDiagnosticsService.cs index ec5a5968..68a84b5c 100644 --- a/src/VectorNNTPD.ReaderService/Diagnostics/FeedDiagnosticsService.cs +++ b/src/VectorNNTPD.ReaderService/Diagnostics/FeedDiagnosticsService.cs @@ -7,7 +7,7 @@ namespace VectorNNTPD.ReaderService.Diagnostics { /// - /// Periodic feed-diagnostics reporter. Does not run when is false. + /// Periodic feed-diagnostics reporter. Does not run when is false. /// public sealed class FeedDiagnosticsService : IApplicationService, IAsyncDisposable { diff --git a/src/VectorNNTPD.ReaderService/Diagnostics/IFeedDiagnostics.cs b/src/VectorNNTPD.ReaderService/Diagnostics/IFeedDiagnostics.cs index 37436822..44160da5 100644 --- a/src/VectorNNTPD.ReaderService/Diagnostics/IFeedDiagnostics.cs +++ b/src/VectorNNTPD.ReaderService/Diagnostics/IFeedDiagnostics.cs @@ -1,17 +1,15 @@ using VectorNNTPD.ReaderService.ArticleIngestion; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Transit; +using VectorNNTPD.Common.Networking.Diagnostics; namespace VectorNNTPD.ReaderService.Diagnostics { /// /// Opt-in real-feed observability. Disabled implementations are no-ops. /// - public interface IFeedDiagnostics + public interface IFeedDiagnostics : ITcpByteDiagnostics { - /// Gets whether probes should be attached and snapshots emitted. - bool IsEnabled { get; } - /// Records a Transit admission rejection (no session runs). void OnRejected(string peerName, string remote); @@ -21,12 +19,6 @@ public interface IFeedDiagnostics /// Unregisters a probe when the session ends. void OnReleased(FeedSessionProbe? probe); - /// - /// Adds application-layer bytes read from an upstream connection (plain TCP payload, - /// or TLS-decrypted octets). No-op when disabled. - /// - void RecordTcpBytes(int bytes); - /// Marks the single spool drain loop as busy around one persist. void BeginSpoolWork(); diff --git a/src/VectorNNTPD.ReaderService/Hosting/NntpdServiceCollectionExtensions.cs b/src/VectorNNTPD.ReaderService/Hosting/NntpdServiceCollectionExtensions.cs index 35e71adb..91e99314 100644 --- a/src/VectorNNTPD.ReaderService/Hosting/NntpdServiceCollectionExtensions.cs +++ b/src/VectorNNTPD.ReaderService/Hosting/NntpdServiceCollectionExtensions.cs @@ -20,7 +20,7 @@ using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.SessionState; using VectorNNTPD.ReaderService.Storage; @@ -41,7 +41,8 @@ using VectorNNTPD.ReaderService.Transit; using VectorNNTPD.ReaderService.Transport.Vatp; using VectorNNTPD.Common.Configuration; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.ReaderService.Hosting { /// diff --git a/src/VectorNNTPD.ReaderService/Moderation/ModerationSubmission.cs b/src/VectorNNTPD.ReaderService/Moderation/ModerationSubmission.cs index 71e311d7..e4df366f 100644 --- a/src/VectorNNTPD.ReaderService/Moderation/ModerationSubmission.cs +++ b/src/VectorNNTPD.ReaderService/Moderation/ModerationSubmission.cs @@ -1,5 +1,5 @@ using VectorNNTPD.ReaderService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Moderation { /// diff --git a/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpPlainListenerService.cs b/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpPlainListenerService.cs index 62e8201c..9f9631f4 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpPlainListenerService.cs +++ b/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpPlainListenerService.cs @@ -11,9 +11,9 @@ using VectorNNTPD.Common.Networking; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.SessionState; using VectorNNTPD.ReaderService.Session; @@ -30,7 +30,7 @@ using VectorNNTPD.ReaderService.Transport.Vatp; using VectorNNTPD.Common.Networking.Listeners; using VectorNNTPD.Common.Cloudflare; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Networking.Listeners { /// diff --git a/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpTlsListenerService.cs b/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpTlsListenerService.cs index cfa7e2f1..a5ea213e 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpTlsListenerService.cs +++ b/src/VectorNNTPD.ReaderService/Networking/Listeners/NntpTlsListenerService.cs @@ -11,9 +11,9 @@ using VectorNNTPD.Common.Networking; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.SessionState; using VectorNNTPD.ReaderService.Session; @@ -31,7 +31,7 @@ using VectorNNTPD.Common.Configuration; using VectorNNTPD.Common.Networking.Listeners; using VectorNNTPD.Common.Cloudflare; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Networking.Listeners { /// diff --git a/src/VectorNNTPD.ReaderService/Networking/Listeners/SocketAcceptListener.cs b/src/VectorNNTPD.ReaderService/Networking/Listeners/SocketAcceptListener.cs deleted file mode 100644 index dcba5b03..00000000 --- a/src/VectorNNTPD.ReaderService/Networking/Listeners/SocketAcceptListener.cs +++ /dev/null @@ -1,217 +0,0 @@ -using System.Net; -using System.Net.Sockets; -using VectorNNTPD.Common.Networking; -using VectorNNTPD.Common.Networking.Listeners; - -namespace VectorNNTPD.ReaderService.Networking.Listeners -{ - using VectorNNTPD.Common.Core; - /// - /// Owns one listening and runs an allocation-efficient asynchronous accept loop. - /// - /// - /// A single failed accepted connection must not stop this listener. The listen socket remains open - /// until / stop. - /// - public sealed class SocketAcceptListener : IAsyncDisposable - { - private readonly ListenBinding _binding; - private readonly Func _onAccepted; - private readonly ILogger _logger; - private readonly IListenSocketBinder _binder; - private readonly Socket _listenSocket; - private readonly CancellationTokenSource _cts = new(); - private Task? _acceptLoop; - private int _stopped; - private int _disposed; - - /// Initializes a new instance of the class. - public SocketAcceptListener( - ListenBinding binding, - Func onAccepted, - ILogger logger, - IListenSocketBinder? listenBinder = null) - { - ArgumentNullException.ThrowIfNull(onAccepted); - ArgumentNullException.ThrowIfNull(logger); - _binding = binding; - _onAccepted = onAccepted; - _logger = logger; - _binder = listenBinder ?? SocketListenBinder.Instance; - - var family = binding.Address.AddressFamily; - _listenSocket = new Socket(family, SocketType.Stream, ProtocolType.Tcp); - if (family == AddressFamily.InterNetworkV6) - { - _listenSocket.DualMode = binding.DualMode; - } - - // SO_REUSEADDR is retained: Bind success is ownership under these socket options. - // On Windows it can allow another process to bind the same endpoint; that is not a - // Bind failure and is not treated as exclusive port ownership. - try - { - _listenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - } - catch (SocketException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - // Best-effort; some platforms differ. - } - } - - /// Gets the local endpoint after (port may be ephemeral in tests). - public IPEndPoint LocalEndPoint => - _listenSocket.LocalEndPoint as IPEndPoint ?? _binding.EndPoint; - - /// Gets the planned binding. - public ListenBinding Binding => _binding; - - /// Gets whether the accept loop is still running (tests). - internal bool AcceptLoopActive => _acceptLoop is { IsCompleted: false }; - - /// Binds, listens, and starts the accept loop. - /// - /// Bind/listen exceptions propagate to the caller and are startup failures. - /// The accept loop is started only after bind/listen succeed; later accept - /// failures are logged and the loop continues. - /// - public void Start(int backlog = 512) - { - _binder.BindAndListen(_listenSocket, _binding.EndPoint, backlog); - _acceptLoop = AcceptLoopAsync(_cts.Token); - NetworkingLogMessages.ListenerStarted(_logger, LocalEndPoint, _binding.DualMode); - } - - /// Stops accepting and closes the listen socket. - public async Task StopAsync() - { - if (Interlocked.Exchange(ref _stopped, 1) == 1) - { - return; - } - - try - { - await _cts.CancelAsync().ConfigureAwait(false); - } - catch (ObjectDisposedException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - // Already disposed. - } - - try - { - _listenSocket.Dispose(); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - - if (_acceptLoop is not null) - { - try - { - await _acceptLoop.ConfigureAwait(false); - } - catch (OperationCanceledException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - // Expected. - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) - { - NetworkingLogMessages.AcceptLoopStopError(_logger, ex); - } - } - - NetworkingLogMessages.ListenerStopped(_logger, _binding.Address, _binding.Port); - } - - /// - public async ValueTask DisposeAsync() - { - if (Interlocked.Exchange(ref _disposed, 1) == 1) - { - return; - } - - await StopAsync().ConfigureAwait(false); - _cts.Dispose(); - } - - private async Task AcceptLoopAsync(CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - Socket accepted; - try - { - accepted = await _listenSocket.AcceptAsync(cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested || _stopped != 0) - { - break; - } - catch (SocketException ex) when (cancellationToken.IsCancellationRequested || _stopped != 0) - { - NetworkingLogMessages.AcceptInterruptedDuringShutdown(_logger, ex); - break; - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) - { - NetworkingLogMessages.AcceptFailed(_logger, ex, _binding.EndPoint); - continue; - } - - _ = HandleAcceptedAsync(accepted, cancellationToken); - } - } - - private async Task HandleAcceptedAsync(Socket accepted, CancellationToken cancellationToken) - { - try - { - await _onAccepted(accepted, cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - try - { - accepted.Dispose(); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) - { - NetworkingLogMessages.AcceptedHandlerFailed(_logger, ex); - try - { - accepted.Dispose(); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - } - } - } -} diff --git a/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyPreambleResolver.cs b/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyPreambleResolver.cs index d06e3f06..70b62804 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyPreambleResolver.cs +++ b/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyPreambleResolver.cs @@ -1,6 +1,6 @@ using System.Net; using System.Net.Sockets; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Networking.Proxy { /// Result of resolving connection identity and optional PROXY leftover octets. diff --git a/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyProtocolParser.cs b/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyProtocolParser.cs index 63202e1c..41cb1c96 100644 --- a/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyProtocolParser.cs +++ b/src/VectorNNTPD.ReaderService/Networking/Proxy/ProxyProtocolParser.cs @@ -2,7 +2,7 @@ using System.Net; using System.Net.Sockets; using System.Text; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Networking.Proxy { /// Incremental parse status for a PROXY v1/v2 preamble buffer. diff --git a/src/VectorNNTPD.ReaderService/PostFilter/IPostFilter.cs b/src/VectorNNTPD.ReaderService/PostFilter/IPostFilter.cs index 3fc6b4ae..0e7859fe 100644 --- a/src/VectorNNTPD.ReaderService/PostFilter/IPostFilter.cs +++ b/src/VectorNNTPD.ReaderService/PostFilter/IPostFilter.cs @@ -1,7 +1,7 @@ using VectorNNTPD.Common.Articles; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.PostFilter { /// NNTPD POST-only accept filter. Does not own CreateQueued / TryAdmit / History. diff --git a/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandContext.cs b/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandContext.cs index 10d8f073..38538edd 100644 --- a/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandContext.cs +++ b/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandContext.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session.Framing; namespace VectorNNTPD.ReaderService.Session.CommandProcessor @@ -62,6 +62,13 @@ public NntpCommandContext( /// public string? CompletionDetail { get; set; } + /// + /// When , the handler expects the connection to become completed + /// as a normal outcome (for example QUIT). must not + /// treat that completion as [failed]. Thrown exceptions still log as failed. + /// + public bool ExpectConnectionComplete { get; set; } + /// Gets the underlying transport connection. public INntpConnection Connection => Session.Connection; diff --git a/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandExecution.cs b/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandExecution.cs index 54e2ac0f..f61b85fe 100644 --- a/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandExecution.cs +++ b/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpCommandExecution.cs @@ -41,8 +41,13 @@ public static async ValueTask RunAsync( // Handler-owned detail (e.g. QUIT peer disconnect, STARTTLS cipher, COMPRESS). detail = context.CompletionDetail; } - else if (!completedBefore && context.Connection.IsCompleted) + else if (!completedBefore + && context.Connection.IsCompleted + && !context.ExpectConnectionComplete) { + // Connection completed during the handler without a handler-owned detail and + // without an explicit expectation (QUIT sets ExpectConnectionComplete). + // STARTTLS handshake abort after 382 still surfaces as failed. detail = "failed"; } } diff --git a/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpPeerDisconnect.cs b/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpPeerDisconnect.cs index 8d9cd11f..fed4d792 100644 --- a/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpPeerDisconnect.cs +++ b/src/VectorNNTPD.ReaderService/Session/CommandProcessor/NntpPeerDisconnect.cs @@ -1,5 +1,5 @@ using System.Net.Sockets; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Session.CommandProcessor { diff --git a/src/VectorNNTPD.ReaderService/Session/Commands/Posting/PostHeaderNormalizer.cs b/src/VectorNNTPD.ReaderService/Session/Commands/Posting/PostHeaderNormalizer.cs index 0e2e0a9d..e3b32d7a 100644 --- a/src/VectorNNTPD.ReaderService/Session/Commands/Posting/PostHeaderNormalizer.cs +++ b/src/VectorNNTPD.ReaderService/Session/Commands/Posting/PostHeaderNormalizer.cs @@ -2,7 +2,7 @@ using System.Net; using System.Text; using VectorNNTPD.ReaderService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Session.Commands.Posting { /// Replaces server-owned POST headers and emits one destuffed article buffer. diff --git a/src/VectorNNTPD.ReaderService/Session/Commands/Posting/StreamingPostReadResult.cs b/src/VectorNNTPD.ReaderService/Session/Commands/Posting/StreamingPostReadResult.cs index c80610eb..e9912d9b 100644 --- a/src/VectorNNTPD.ReaderService/Session/Commands/Posting/StreamingPostReadResult.cs +++ b/src/VectorNNTPD.ReaderService/Session/Commands/Posting/StreamingPostReadResult.cs @@ -1,6 +1,6 @@ using VectorNNTPD.ReaderService.Moderation; using VectorNNTPD.ReaderService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Session.Commands.Posting { /// What the POST command should do with a completed receive. diff --git a/src/VectorNNTPD.ReaderService/Session/Commands/Quit.cs b/src/VectorNNTPD.ReaderService/Session/Commands/Quit.cs index c1b75cf6..3b7a2e07 100644 --- a/src/VectorNNTPD.ReaderService/Session/Commands/Quit.cs +++ b/src/VectorNNTPD.ReaderService/Session/Commands/Quit.cs @@ -30,6 +30,7 @@ private static async ValueTask ExecuteAsync(NntpCommandContext context, Cancella { // Valid QUIT accepted: after this point the session must become terminal and no further // NNTP commands/responses should be processed (aside from finishing this 205 write). + context.ExpectConnectionComplete = true; var responseWritten = false; var idleVersionBefore205 = context.Connection.OutboundIdleVersion; try @@ -88,6 +89,13 @@ await context.Connection { context.CompletionDetail = "peer disconnected"; } + + // StatusLine is normally noted only when Debug command logging is enabled; ensure TX + // completion can still show 205 after a successful write. + if (responseWritten) + { + context.StatusLine ??= NntpResponseStatus.ConnectionClosing; + } } } } diff --git a/src/VectorNNTPD.ReaderService/Session/Commands/StartTls.cs b/src/VectorNNTPD.ReaderService/Session/Commands/StartTls.cs index e150b01b..1a9a9a41 100644 --- a/src/VectorNNTPD.ReaderService/Session/Commands/StartTls.cs +++ b/src/VectorNNTPD.ReaderService/Session/Commands/StartTls.cs @@ -1,8 +1,8 @@ using System.IO.Pipelines; using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Session.Commands { /// diff --git a/src/VectorNNTPD.ReaderService/Session/NntpSession.cs b/src/VectorNNTPD.ReaderService/Session/NntpSession.cs index 00b4efac..e6436bcf 100644 --- a/src/VectorNNTPD.ReaderService/Session/NntpSession.cs +++ b/src/VectorNNTPD.ReaderService/Session/NntpSession.cs @@ -8,9 +8,9 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.SessionState; using VectorNNTPD.ReaderService.Session.Authentication; @@ -25,7 +25,8 @@ using VectorNNTPD.ReaderService.RabbitMq.ArticleWork; using VectorNNTPD.ReaderService.Storage; using VectorNNTPD.ReaderService.Transport.Vatp; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Session { using VectorNNTPD.Common.Core; diff --git a/src/VectorNNTPD.ReaderService/SessionState/DistributedSessionStateTracker.cs b/src/VectorNNTPD.ReaderService/SessionState/DistributedSessionStateTracker.cs index 732d5dca..be52242b 100644 --- a/src/VectorNNTPD.ReaderService/SessionState/DistributedSessionStateTracker.cs +++ b/src/VectorNNTPD.ReaderService/SessionState/DistributedSessionStateTracker.cs @@ -1,8 +1,8 @@ using System.Collections.Concurrent; using System.Net; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.SessionState.RateLimiting; - namespace VectorNNTPD.ReaderService.SessionState { /// diff --git a/src/VectorNNTPD.ReaderService/SessionState/InMemorySessionStateTracker.cs b/src/VectorNNTPD.ReaderService/SessionState/InMemorySessionStateTracker.cs index 31034792..5f56e3f3 100644 --- a/src/VectorNNTPD.ReaderService/SessionState/InMemorySessionStateTracker.cs +++ b/src/VectorNNTPD.ReaderService/SessionState/InMemorySessionStateTracker.cs @@ -1,6 +1,6 @@ using System.Net; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.SessionState.RateLimiting; - namespace VectorNNTPD.ReaderService.SessionState { /// diff --git a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/AccountRateAllocator.cs b/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/AccountRateAllocator.cs index a244b7f3..aeb65943 100644 --- a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/AccountRateAllocator.cs +++ b/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/AccountRateAllocator.cs @@ -1,5 +1,7 @@ using System.Collections.Concurrent; +using VectorNNTPD.Common.Networking.RateLimiting; + namespace VectorNNTPD.ReaderService.SessionState.RateLimiting { /// diff --git a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IAccountRateAllocator.cs b/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IAccountRateAllocator.cs index 7db3715e..eed8ff59 100644 --- a/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IAccountRateAllocator.cs +++ b/src/VectorNNTPD.ReaderService/SessionState/RateLimiting/IAccountRateAllocator.cs @@ -1,3 +1,5 @@ +using VectorNNTPD.Common.Networking.RateLimiting; + namespace VectorNNTPD.ReaderService.SessionState.RateLimiting { /// diff --git a/src/VectorNNTPD.ReaderService/Transit/TransitConnectionAdmission.cs b/src/VectorNNTPD.ReaderService/Transit/TransitConnectionAdmission.cs index 4cb6cb00..c1f15f0a 100644 --- a/src/VectorNNTPD.ReaderService/Transit/TransitConnectionAdmission.cs +++ b/src/VectorNNTPD.ReaderService/Transit/TransitConnectionAdmission.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs index 112533bc..37e148a9 100644 --- a/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs @@ -1,12 +1,12 @@ using System.Collections.Concurrent; using System.Net; -using System.Net.Security; using System.Net.Sockets; using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; using VectorNNTPD.Common.Acme; using VectorNNTPD.Common.Core; using VectorNNTPD.Common.Networking.Certificates; +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.StorageService.Configuration; using VectorNNTPD.Common.Articles; using VectorNNTPD.StorageService.Ingress; @@ -39,7 +39,7 @@ public sealed class StorageNntpListenerService : IApplicationService, IAsyncDisp /// private readonly Func? _publishAfterBind; private readonly object _gate = new(); - private readonly List _listenSockets = []; + private readonly List _listeners = []; private readonly ConcurrentDictionary _connections = new(); private readonly CancellationTokenSource _runCts = new(); @@ -189,7 +189,7 @@ internal IReadOnlyList LocalEndPoints { lock (_gate) { - return [.. _listenSockets.Select(static socket => socket.LocalEndPoint)]; + return [.. _listeners.Select(static listener => (EndPoint?)listener.LocalEndPoint)]; } } } @@ -232,7 +232,7 @@ public async Task StartAsync(CancellationToken cancellationToken) StorageNntpListenerLogMessages.Starting(_logger, Runtime.BindPortTls, bindings.Count); BindEndpoints(bindings); - if (_listenSockets.Count == 0) + if (_listeners.Count == 0) { throw new InvalidOperationException("Storage NNTP Listener failed to bind any endpoint."); } @@ -247,8 +247,9 @@ public async Task StartAsync(CancellationToken cancellationToken) _state = StorageNntpListenerState.Running; } - StorageNntpListenerLogMessages.Running(_logger, _listenSockets.Count, Runtime.BindPortTls); - _acceptTask = AcceptAllAsync(_runCts.Token); + StorageNntpListenerLogMessages.Running(_logger, _listeners.Count, Runtime.BindPortTls); + _acceptTask = Task.WhenAll( + _listeners.Select(static listener => listener.AcceptLoopTask ?? Task.CompletedTask)); } catch (Exception ex) { @@ -285,7 +286,7 @@ public async ValueTask DisposeAsync() StorageNntpListenerLogMessages.Retiring(_logger); await _runCts.CancelAsync().ConfigureAwait(false); - CloseListenSockets(); + await CloseListenSocketsAsync().ConfigureAwait(false); var accept = _acceptTask; if (accept is not null) @@ -319,14 +320,21 @@ private void BindEndpoints(IReadOnlyList bindings) { foreach (var binding in bindings) { + SocketAcceptListener? listener = null; try { - var socket = CreateBoundListenSocket(binding); + listener = new SocketAcceptListener( + binding, + OnAcceptedAsync, + _logger, + reuseAddress: false); + listener.Start(); lock (_gate) { - _listenSockets.Add(socket); + _listeners.Add(listener); } + listener = null; StorageNntpListenerLogMessages.EndpointBound( _logger, binding.EndPoint.ToString() ?? string.Empty, @@ -340,28 +348,21 @@ private void BindEndpoints(IReadOnlyList bindings) _logger, binding.Address.AddressFamily.ToString(), ex.SocketErrorCode.ToString()); + if (listener is not null) + { + _ = listener.DisposeAsync().AsTask(); + } } - } - } - - private static Socket CreateBoundListenSocket(ListenBinding binding) - { - var endpoint = binding.EndPoint; - using var ownership = new DisposableOwnership( - new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) + catch { - NoDelay = true, - }); - var socket = ownership.Resource; + if (listener is not null) + { + _ = listener.DisposeAsync().AsTask(); + } - if (endpoint.AddressFamily == AddressFamily.InterNetworkV6) - { - socket.DualMode = binding.DualMode; + throw; + } } - - socket.Bind(endpoint); - socket.Listen(512); - return ownership.Transfer(); } private bool IsImplicitWildcard(IPEndPoint endpoint) @@ -375,89 +376,53 @@ private bool IsImplicitWildcard(IPEndPoint endpoint) && (endpoint.Address.Equals(IPAddress.Any) || endpoint.Address.Equals(IPAddress.IPv6Any)); } - private async Task AcceptAllAsync(CancellationToken cancellationToken) + private ValueTask OnAcceptedAsync(Socket accepted, CancellationToken cancellationToken) { - Socket[] sockets; - lock (_gate) + var remote = accepted.RemoteEndPoint?.ToString() ?? ""; + if (!TryAdmit()) { - sockets = [.. _listenSockets]; + StorageNntpListenerLogMessages.CapacityRejected(_logger, remote, Runtime.Listener.MaxActiveConnections); + accepted.Dispose(); + return ValueTask.CompletedTask; } - await Task.WhenAll(sockets.Select(socket => AcceptLoopAsync(socket, cancellationToken))).ConfigureAwait(false); - } - - private async Task AcceptLoopAsync(Socket listenSocket, CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - Socket accepted; - try - { - accepted = await listenSocket.AcceptAsync(cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (ObjectDisposedException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (SocketException) when (cancellationToken.IsCancellationRequested) - { - break; - } - catch (SocketException) - { - if (cancellationToken.IsCancellationRequested) - { - break; - } - - continue; - } - - var remote = accepted.RemoteEndPoint?.ToString() ?? ""; - if (!TryAdmit()) - { - StorageNntpListenerLogMessages.CapacityRejected(_logger, remote, Runtime.Listener.MaxActiveConnections); - accepted.Dispose(); - continue; - } - - StorageNntpListenerLogMessages.Accepted(_logger, remote, ActiveConnections); - var connection = ProcessAsync(accepted, remote, cancellationToken); - _connections[connection] = 0; - _ = connection.ContinueWith( - static (task, state) => ((StorageNntpListenerService)state!)._connections.TryRemove(task, out _), - this, - TaskScheduler.Default); - } + StorageNntpListenerLogMessages.Accepted(_logger, remote, ActiveConnections); + var connection = ProcessAsync(accepted, remote, cancellationToken); + _connections[connection] = 0; + _ = connection.ContinueWith( + static (task, state) => ((StorageNntpListenerService)state!)._connections.TryRemove(task, out _), + this, + TaskScheduler.Default); + return ValueTask.CompletedTask; } private async Task ProcessAsync(Socket accepted, string remote, CancellationToken cancellationToken) { var handshakeCompleted = false; + var socketOwnedByConnection = false; try { - await using var network = new NetworkStream(accepted, ownsSocket: true); LastAcceptedSendBuffer = accepted.SendBufferSize; LastAcceptedReceiveBuffer = accepted.ReceiveBufferSize; - using var lease = _certificates.Acquire(); - await using var ssl = new SslStream(network, leaveInnerStreamOpen: true); - var options = new SslServerAuthenticationOptions + if (accepted.RemoteEndPoint is not IPEndPoint tcpPeer) { - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, - ClientCertificateRequired = false, - CertificateRevocationCheckMode = X509RevocationMode.NoCheck, - ServerCertificateContext = lease.Context, - }; + return; + } + var identity = ConnectionClientIdentity.Direct(tcpPeer); using var handshakeCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); handshakeCts.CancelAfter(Runtime.Listener.TlsHandshakeTimeout); + NntpConnection connection; try { - await ssl.AuthenticateAsServerAsync(options, handshakeCts.Token).ConfigureAwait(false); + connection = await NntpConnection.StartTlsAsync( + accepted, + _certificates, + identity, + _logger, + handshakeCts.Token) + .ConfigureAwait(false); + socketOwnedByConnection = true; } catch (OperationCanceledException) when (handshakeCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) { @@ -465,23 +430,27 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke } handshakeCompleted = true; - await using var transport = new StreamStorageListenerTransport( - ssl, - Runtime.Listener.IoProgressTimeout, - leaveInnerStreamOpen: true); - await using var session = new StorageNntpFeedSession( - transport, - Runtime.Listener, - _logger, - Runtime.Fqdn, - acceptedArticles: _acceptedArticles, - assemblyAdmission: _assemblyAdmission, - allowsArticleAdmission: _allowsArticleAdmission, - articlePresent: _articlePresent, - resolveRetrievableArticle: _resolveRetrievableArticle, - afterArticleDelivered: _afterArticleDelivered, - acquireArticleReadLease: _acquireArticleReadLease); - await session.RunAsync(cancellationToken).ConfigureAwait(false); + await using (connection) + { + await using var stream = new NntpConnectionStream(connection, ownsConnection: false); + await using var transport = new StreamStorageListenerTransport( + stream, + Runtime.Listener.IoProgressTimeout, + leaveInnerStreamOpen: true); + await using var session = new StorageNntpFeedSession( + transport, + Runtime.Listener, + _logger, + Runtime.Fqdn, + acceptedArticles: _acceptedArticles, + assemblyAdmission: _assemblyAdmission, + allowsArticleAdmission: _allowsArticleAdmission, + articlePresent: _articlePresent, + resolveRetrievableArticle: _resolveRetrievableArticle, + afterArticleDelivered: _afterArticleDelivered, + acquireArticleReadLease: _acquireArticleReadLease); + await session.RunAsync(cancellationToken).ConfigureAwait(false); + } } catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) { @@ -509,6 +478,17 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke } finally { + if (!socketOwnedByConnection) + { + try + { + accepted.Dispose(); + } + catch (ObjectDisposedException) + { + } + } + ReleaseAdmit(); } } @@ -532,24 +512,25 @@ private bool TryAdmit() private void ReleaseAdmit() => Interlocked.Decrement(ref _activeConnections); - private void CloseListenSockets() + private async Task CloseListenSocketsAsync() { + SocketAcceptListener[] listeners; lock (_gate) { - foreach (var socket in _listenSockets) + listeners = [.. _listeners]; + _listeners.Clear(); + } + + foreach (var listener in listeners) + { + try { - try - { - socket.Dispose(); - } - catch (Exception ex) when (ex is ObjectDisposedException or System.Net.Sockets.SocketException) - { - // Concurrent retire may dispose the same listen socket twice. - ExpectedException.Ignore(ex); - } + await listener.DisposeAsync().ConfigureAwait(false); + } + catch (Exception ex) when (ex is ObjectDisposedException or SocketException) + { + ExpectedException.Ignore(ex); } - - _listenSockets.Clear(); } } @@ -607,11 +588,7 @@ private void LogUnexpectedDrainFailures(Task[] tasks, Exception awaitException) } } - private Task DisposeBoundResourcesAsync() - { - CloseListenSockets(); - return Task.CompletedTask; - } + private Task DisposeBoundResourcesAsync() => CloseListenSocketsAsync(); } /// Local lifecycle states for the StorageServer NNTP listener. diff --git a/src/VectorNNTPD.TransitService/ArticleIngestion/ArticleRecordIngress.cs b/src/VectorNNTPD.TransitService/ArticleIngestion/ArticleRecordIngress.cs index 87c5c442..176972db 100644 --- a/src/VectorNNTPD.TransitService/ArticleIngestion/ArticleRecordIngress.cs +++ b/src/VectorNNTPD.TransitService/ArticleIngestion/ArticleRecordIngress.cs @@ -2,7 +2,7 @@ using VectorNNTPD.Common.Articles.Parsing; using VectorNNTPD.TransitService.Networking.Proxy; using VectorNNTPD.TransitService.Session.Framing; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.ArticleIngestion { /// diff --git a/src/VectorNNTPD.TransitService/ArticleIngestion/InboundArticle.cs b/src/VectorNNTPD.TransitService/ArticleIngestion/InboundArticle.cs index a556e733..5c945b09 100644 --- a/src/VectorNNTPD.TransitService/ArticleIngestion/InboundArticle.cs +++ b/src/VectorNNTPD.TransitService/ArticleIngestion/InboundArticle.cs @@ -1,6 +1,6 @@ using VectorNNTPD.Common.Articles; using VectorNNTPD.TransitService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.ArticleIngestion { /// diff --git a/src/VectorNNTPD.TransitService/Authentication/NntpAccountPolicy.cs b/src/VectorNNTPD.TransitService/Authentication/NntpAccountPolicy.cs index 7fc1e985..8e512a98 100644 --- a/src/VectorNNTPD.TransitService/Authentication/NntpAccountPolicy.cs +++ b/src/VectorNNTPD.TransitService/Authentication/NntpAccountPolicy.cs @@ -1,5 +1,5 @@ using VectorNNTPD.Common.Articles; - +using VectorNNTPD.TransitService.SessionState.RateLimiting; namespace VectorNNTPD.TransitService.Authentication { /// diff --git a/src/VectorNNTPD.TransitService/Diagnostics/FeedDiagnosticsService.cs b/src/VectorNNTPD.TransitService/Diagnostics/FeedDiagnosticsService.cs index 34f11e5b..535efa34 100644 --- a/src/VectorNNTPD.TransitService/Diagnostics/FeedDiagnosticsService.cs +++ b/src/VectorNNTPD.TransitService/Diagnostics/FeedDiagnosticsService.cs @@ -7,7 +7,7 @@ namespace VectorNNTPD.TransitService.Diagnostics { /// - /// Periodic feed-diagnostics reporter. Does not run when is false. + /// Periodic feed-diagnostics reporter. Does not run when is false. /// public sealed class FeedDiagnosticsService : IApplicationService, IAsyncDisposable { diff --git a/src/VectorNNTPD.TransitService/Diagnostics/IFeedDiagnostics.cs b/src/VectorNNTPD.TransitService/Diagnostics/IFeedDiagnostics.cs index b374f16a..54584fdd 100644 --- a/src/VectorNNTPD.TransitService/Diagnostics/IFeedDiagnostics.cs +++ b/src/VectorNNTPD.TransitService/Diagnostics/IFeedDiagnostics.cs @@ -1,17 +1,15 @@ using VectorNNTPD.TransitService.ArticleIngestion; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Transit; +using VectorNNTPD.Common.Networking.Diagnostics; namespace VectorNNTPD.TransitService.Diagnostics { /// /// Opt-in real-feed observability. Disabled implementations are no-ops. /// - public interface IFeedDiagnostics + public interface IFeedDiagnostics : ITcpByteDiagnostics { - /// Gets whether probes should be attached and snapshots emitted. - bool IsEnabled { get; } - /// Records a Transit admission rejection (no session runs). void OnRejected(string peerName, string remote); @@ -21,12 +19,6 @@ public interface IFeedDiagnostics /// Unregisters a probe when the session ends. void OnReleased(FeedSessionProbe? probe); - /// - /// Adds application-layer bytes read from an upstream connection (plain TCP payload, - /// or TLS-decrypted octets). No-op when disabled. - /// - void RecordTcpBytes(int bytes); - /// Marks the single spool drain loop as busy around one persist. void BeginSpoolWork(); diff --git a/src/VectorNNTPD.TransitService/Hosting/NntpdServiceCollectionExtensions.cs b/src/VectorNNTPD.TransitService/Hosting/NntpdServiceCollectionExtensions.cs index 8e569ecb..dc1c43ae 100644 --- a/src/VectorNNTPD.TransitService/Hosting/NntpdServiceCollectionExtensions.cs +++ b/src/VectorNNTPD.TransitService/Hosting/NntpdServiceCollectionExtensions.cs @@ -20,7 +20,7 @@ using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.SessionState.BytesAccounting; -using VectorNNTPD.TransitService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.TransitService.Authentication; using VectorNNTPD.TransitService.SessionState; using VectorNNTPD.TransitService.Storage; @@ -41,7 +41,8 @@ using VectorNNTPD.TransitService.Transit; using VectorNNTPD.TransitService.Transport.Vatp; using VectorNNTPD.Common.Configuration; - +using VectorNNTPD.TransitService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.TransitService.Hosting { /// diff --git a/src/VectorNNTPD.TransitService/Moderation/ModerationSubmission.cs b/src/VectorNNTPD.TransitService/Moderation/ModerationSubmission.cs index dd20f98b..f8933bf6 100644 --- a/src/VectorNNTPD.TransitService/Moderation/ModerationSubmission.cs +++ b/src/VectorNNTPD.TransitService/Moderation/ModerationSubmission.cs @@ -1,5 +1,5 @@ using VectorNNTPD.TransitService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Moderation { /// diff --git a/src/VectorNNTPD.TransitService/Networking/Listeners/ConnectionAcceptanceLogging.cs b/src/VectorNNTPD.TransitService/Networking/Listeners/ConnectionAcceptanceLogging.cs deleted file mode 100644 index 3cae0664..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Listeners/ConnectionAcceptanceLogging.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Net; -using VectorNNTPD.Common.Networking; -using VectorNNTPD.TransitService.Networking.Proxy; - -namespace VectorNNTPD.TransitService.Networking.Listeners -{ - /// Formats connection-acceptance Information logs (TCP peer vs PROXY effective client). - internal static class ConnectionAcceptanceLogging - { - /// Logs a successful plain (or plain+PROXY) connection acceptance. - public static void LogPlainAccepted(ILogger logger, ConnectionClientIdentity identity) - { - ArgumentNullException.ThrowIfNull(logger); - ArgumentNullException.ThrowIfNull(identity); - - if (identity.UsedProxyHeader) - { - NetworkingLogMessages.ProxyConnectionAccepted( - logger, - FormatEndpoint(identity.TcpPeer), - FormatEndpoint(identity.Client)); - } - else - { - NetworkingLogMessages.PlainConnectionAccepted( - logger, - FormatEndpoint(identity.TcpPeer)); - } - } - - /// Logs a successful TLS (or TLS+PROXY) connection acceptance after handshake. - public static void LogTlsAccepted( - ILogger logger, - ConnectionClientIdentity identity, - string tlsVersion, - string cipher) - { - ArgumentNullException.ThrowIfNull(logger); - ArgumentNullException.ThrowIfNull(identity); - ArgumentException.ThrowIfNullOrWhiteSpace(tlsVersion); - ArgumentException.ThrowIfNullOrWhiteSpace(cipher); - - if (identity.UsedProxyHeader) - { - NetworkingLogMessages.TlsProxyConnectionAccepted( - logger, - FormatEndpoint(identity.TcpPeer), - FormatEndpoint(identity.Client), - tlsVersion, - cipher); - } - else - { - NetworkingLogMessages.TlsConnectionAccepted( - logger, - FormatEndpoint(identity.TcpPeer), - tlsVersion, - cipher); - } - } - - /// Formats an endpoint as ip:port for connection acceptance logs. - public static string FormatEndpoint(IPEndPoint endpoint) - { - ArgumentNullException.ThrowIfNull(endpoint); - return $"{endpoint.Address}:{endpoint.Port}"; - } - - /// Formats a TCP endpoint for accept/disconnect logs, or unknown. - public static string FormatEndpoint(EndPoint? endpoint) => - endpoint switch - { - IPEndPoint ip => FormatEndpoint(ip), - not null => endpoint.ToString() ?? "unknown", - _ => "unknown", - }; - - /// Logs the single Information disconnect summary for an established TCP connection. - public static void LogDisconnected( - ILogger logger, - EndPoint? remote, - EndPoint? local, - string reason) - { - ArgumentNullException.ThrowIfNull(logger); - ArgumentException.ThrowIfNullOrWhiteSpace(reason); - NetworkingLogMessages.TcpConnectionDisconnected( - logger, - FormatEndpoint(remote), - FormatEndpoint(local), - reason); - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Listeners/IListenSocketBinder.cs b/src/VectorNNTPD.TransitService/Networking/Listeners/IListenSocketBinder.cs deleted file mode 100644 index db4b9b92..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Listeners/IListenSocketBinder.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Net; -using System.Net.Sockets; - -namespace VectorNNTPD.TransitService.Networking.Listeners -{ - /// - /// Binds and listens on an already-created TCP socket. - /// - /// - /// This is the startup ownership seam for a listening endpoint. A successful - /// means the socket accepted bind/listen under the - /// application's socket options. Accept-loop failures after that point are not - /// startup failures. - /// - public interface IListenSocketBinder - { - /// - /// Binds to and starts listening. - /// - /// The listen socket created for . - /// The local endpoint that must be owned after this call. - /// The listen backlog. - void BindAndListen(Socket socket, IPEndPoint endpoint, int backlog); - } - - /// Production / implementation. - internal sealed class SocketListenBinder : IListenSocketBinder - { - /// Gets the shared production binder. - public static SocketListenBinder Instance { get; } = new(); - - /// - public void BindAndListen(Socket socket, IPEndPoint endpoint, int backlog) - { - ArgumentNullException.ThrowIfNull(socket); - ArgumentNullException.ThrowIfNull(endpoint); - socket.Bind(endpoint); - socket.Listen(backlog); - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Listeners/NntpPlainListenerService.cs b/src/VectorNNTPD.TransitService/Networking/Listeners/NntpPlainListenerService.cs index fb87b712..d6647b4a 100644 --- a/src/VectorNNTPD.TransitService/Networking/Listeners/NntpPlainListenerService.cs +++ b/src/VectorNNTPD.TransitService/Networking/Listeners/NntpPlainListenerService.cs @@ -11,9 +11,9 @@ using VectorNNTPD.Common.Networking; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.SessionState.BytesAccounting; -using VectorNNTPD.TransitService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.TransitService.Authentication; using VectorNNTPD.TransitService.SessionState; using VectorNNTPD.TransitService.Session; @@ -30,7 +30,7 @@ using VectorNNTPD.TransitService.Transport.Vatp; using VectorNNTPD.Common.Networking.Listeners; using VectorNNTPD.Common.Cloudflare; - +using VectorNNTPD.TransitService.SessionState.RateLimiting; namespace VectorNNTPD.TransitService.Networking.Listeners { /// diff --git a/src/VectorNNTPD.TransitService/Networking/Listeners/NntpTlsListenerService.cs b/src/VectorNNTPD.TransitService/Networking/Listeners/NntpTlsListenerService.cs index 4727bac3..601050da 100644 --- a/src/VectorNNTPD.TransitService/Networking/Listeners/NntpTlsListenerService.cs +++ b/src/VectorNNTPD.TransitService/Networking/Listeners/NntpTlsListenerService.cs @@ -11,9 +11,9 @@ using VectorNNTPD.Common.Networking; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.SessionState.BytesAccounting; -using VectorNNTPD.TransitService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.TransitService.Authentication; using VectorNNTPD.TransitService.SessionState; using VectorNNTPD.TransitService.Session; @@ -31,7 +31,7 @@ using VectorNNTPD.Common.Configuration; using VectorNNTPD.Common.Networking.Listeners; using VectorNNTPD.Common.Cloudflare; - +using VectorNNTPD.TransitService.SessionState.RateLimiting; namespace VectorNNTPD.TransitService.Networking.Listeners { /// diff --git a/src/VectorNNTPD.TransitService/Networking/Proxy/ConnectionClientIdentity.cs b/src/VectorNNTPD.TransitService/Networking/Proxy/ConnectionClientIdentity.cs deleted file mode 100644 index d165a0c9..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Proxy/ConnectionClientIdentity.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Net; - -namespace VectorNNTPD.TransitService.Networking.Proxy -{ - /// - /// Immutable connection-level client identity established once at accept time. - /// - /// - /// - /// is always the accepted socket's remote endpoint. - /// is the effective client endpoint exposed to the session layer - /// (equal to for direct and untrusted peers; the PROXY-reported - /// source for trusted HAProxy peers that supplied a proxied address). - /// - /// - /// Trust and PROXY parsing are completed before this value is published; the session layer - /// must not reparse PROXY headers or inspect the socket to obtain the client endpoint. - /// - /// - public sealed class ConnectionClientIdentity - { - private ConnectionClientIdentity( - IPEndPoint tcpPeer, - IPEndPoint client, - bool isTrustedProxy, - bool usedProxyHeader, - int? proxyProtocolVersion) - { - TcpPeer = tcpPeer; - Client = client; - IsTrustedProxy = isTrustedProxy; - UsedProxyHeader = usedProxyHeader; - ProxyProtocolVersion = proxyProtocolVersion; - } - - /// Gets the actual TCP peer endpoint of the accepted socket. - public IPEndPoint TcpPeer { get; } - - /// Gets the effective client endpoint for the session layer. - public IPEndPoint Client { get; } - - /// Gets the effective client IP address. - public IPAddress ClientAddress => Client.Address; - - /// Gets the effective client TCP source port. - public int ClientPort => Client.Port; - - /// Gets whether the TCP peer matched a configured trusted PROXY host. - public bool IsTrustedProxy { get; } - - /// Gets whether a PROXY protocol header was consumed for this connection. - public bool UsedProxyHeader { get; } - - /// Gets the PROXY protocol version when a header was consumed; otherwise . - public int? ProxyProtocolVersion { get; } - - /// Creates a direct (non-proxy) identity where client equals TCP peer. - /// - /// Publishes independent instances so later mutation of the - /// caller's endpoint (or of one published property) cannot alias into the other. - /// - public static ConnectionClientIdentity Direct(IPEndPoint tcpPeer) - { - ArgumentNullException.ThrowIfNull(tcpPeer); - var peer = CloneEndpoint(tcpPeer); - var client = CloneEndpoint(tcpPeer); - return new ConnectionClientIdentity( - peer, - client, - isTrustedProxy: false, - usedProxyHeader: false, - proxyProtocolVersion: null); - } - - /// Creates an identity for a trusted proxy peer with an optional PROXY-reported client. - public static ConnectionClientIdentity FromTrustedProxy( - IPEndPoint tcpPeer, - IPEndPoint client, - int proxyProtocolVersion) - { - ArgumentNullException.ThrowIfNull(tcpPeer); - ArgumentNullException.ThrowIfNull(client); - return new ConnectionClientIdentity( - CloneEndpoint(tcpPeer), - CloneEndpoint(client), - isTrustedProxy: true, - usedProxyHeader: true, - proxyProtocolVersion); - } - - private static IPEndPoint CloneEndpoint(IPEndPoint endpoint) => - new(endpoint.Address, endpoint.Port); - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Proxy/PrefixedStream.cs b/src/VectorNNTPD.TransitService/Networking/Proxy/PrefixedStream.cs deleted file mode 100644 index b4f3a40e..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Proxy/PrefixedStream.cs +++ /dev/null @@ -1,137 +0,0 @@ -namespace VectorNNTPD.TransitService.Networking.Proxy -{ - /// - /// Stream that yields a fixed prefix before reading from an inner stream (TLS leftover after PROXY). - /// - internal sealed class PrefixedStream : Stream - { - private readonly Stream _inner; - private readonly bool _leaveInnerOpen; - private byte[]? _prefix; - private int _prefixOffset; - - public PrefixedStream(Stream inner, ReadOnlyMemory prefix, bool leaveInnerOpen = false) - { - ArgumentNullException.ThrowIfNull(inner); - _inner = inner; - _leaveInnerOpen = leaveInnerOpen; - if (!prefix.IsEmpty) - { - _prefix = prefix.ToArray(); - } - } - - /// - public override bool CanRead => true; - - /// - public override bool CanSeek => false; - - /// - public override bool CanWrite => _inner.CanWrite; - - /// - public override long Length => throw new NotSupportedException(); - - /// - public override long Position - { - get => throw new NotSupportedException(); - set => throw new NotSupportedException(); - } - - /// - public override void Flush() => _inner.Flush(); - - /// - public override Task FlushAsync(CancellationToken cancellationToken) => - _inner.FlushAsync(cancellationToken); - - /// - public override int Read(byte[] buffer, int offset, int count) - { - ArgumentNullException.ThrowIfNull(buffer); - return Read(buffer.AsSpan(offset, count)); - } - - /// - public override int Read(Span buffer) - { - if (_prefix is { } prefix && _prefixOffset < prefix.Length) - { - var available = prefix.Length - _prefixOffset; - var take = Math.Min(available, buffer.Length); - prefix.AsSpan(_prefixOffset, take).CopyTo(buffer); - _prefixOffset += take; - if (_prefixOffset >= prefix.Length) - { - _prefix = null; - } - - return take; - } - - return _inner.Read(buffer); - } - - /// - public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) - { - if (_prefix is { } prefix && _prefixOffset < prefix.Length) - { - var available = prefix.Length - _prefixOffset; - var take = Math.Min(available, buffer.Length); - prefix.AsSpan(_prefixOffset, take).CopyTo(buffer.Span); - _prefixOffset += take; - if (_prefixOffset >= prefix.Length) - { - _prefix = null; - } - - return ValueTask.FromResult(take); - } - - return _inner.ReadAsync(buffer, cancellationToken); - } - - /// - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => - ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); - - /// - public override void Write(byte[] buffer, int offset, int count) => - _inner.Write(buffer, offset, count); - - /// - public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) => - _inner.WriteAsync(buffer, cancellationToken); - - /// - public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); - - /// - public override void SetLength(long value) => throw new NotSupportedException(); - - /// - protected override void Dispose(bool disposing) - { - if (disposing && !_leaveInnerOpen) - { - _inner.Dispose(); - } - - base.Dispose(disposing); - } - - /// - public override async ValueTask DisposeAsync() - { - if (!_leaveInnerOpen) - { - await _inner.DisposeAsync().ConfigureAwait(false); - } - - await base.DisposeAsync().ConfigureAwait(false); - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyPreambleResolver.cs b/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyPreambleResolver.cs index b4633814..3bb14063 100644 --- a/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyPreambleResolver.cs +++ b/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyPreambleResolver.cs @@ -1,6 +1,6 @@ using System.Net; using System.Net.Sockets; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Networking.Proxy { /// Result of resolving connection identity and optional PROXY leftover octets. diff --git a/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyProtocolParser.cs b/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyProtocolParser.cs index 4f38b23d..4f2c1b85 100644 --- a/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyProtocolParser.cs +++ b/src/VectorNNTPD.TransitService/Networking/Proxy/ProxyProtocolParser.cs @@ -2,7 +2,7 @@ using System.Net; using System.Net.Sockets; using System.Text; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Networking.Proxy { /// Incremental parse status for a PROXY v1/v2 preamble buffer. diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/ConnectionByteTransport.cs b/src/VectorNNTPD.TransitService/Networking/Transport/ConnectionByteTransport.cs deleted file mode 100644 index 3197f4c7..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/ConnectionByteTransport.cs +++ /dev/null @@ -1,748 +0,0 @@ -using System.Diagnostics; -using System.Net.Security; -using VectorNNTPD.TransitService.SessionState.RateLimiting; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// Single owner of byte-stream I/O under pumps. - /// - /// - /// - /// Pumps call / only. They never touch the socket - /// or directly. The active stream may be replaced during an in-place TLS - /// upgrade after guarantees no outstanding stream I/O. - /// - /// - /// Quiescence blocks new application reads/writes at a gate, cancels in-flight reads - /// (freeing the socket for TLS handshake or DEFLATE wrap), and waits for in-flight writes - /// to finish under the caller token. Admission of an I/O operation and registration of that - /// operation as outstanding are atomic with respect to the quiescence state transition under - /// _gate. - /// - /// - internal sealed class ConnectionByteTransport : IAsyncDisposable - { - private const int StateActive = 0; - private const int StateReadsPaused = 1; - private const int StateQuiesced = 2; - private const int StateAbandoned = 3; - private const int StateDisposed = 4; - - private readonly object _gate = new(); - private Stream _stream; - private CancellationTokenSource _readCts = new(); - private TaskCompletionSource _resumeTcs = - new(TaskCreationOptions.RunContinuationsAsynchronously); - private TaskCompletionSource? _idleTcs; - private TaskCompletionSource? _readIdleTcs; - private int _activeReads; - private int _activeWrites; - private int _state; - private bool _isTls; - private bool _isCompressed; - private byte[]? _pendingUpgradePrefix; - private readonly OutboundRateLimiter _rateLimiter; - - public ConnectionByteTransport(Stream stream, bool isTls, OutboundRateLimiter? rateLimiter = null) - { - ArgumentNullException.ThrowIfNull(stream); - if (rateLimiter is not null) - { - _rateLimiter = rateLimiter; - _stream = stream; - } - else if (stream is OutboundRateLimiter existing) - { - _rateLimiter = existing; - _stream = existing; - } - else - { - _rateLimiter = new OutboundRateLimiter(stream, initialMaxSendBytesPerSecond: 0, leaveInnerOpen: false); - _stream = _rateLimiter; - } - - _isTls = isTls; - _resumeTcs.SetResult(); - } - - /// - /// Wire-level outbound cap. Sits under TLS/DEFLATE so throttled bytes are - /// the octets written toward the socket. - /// - internal OutboundRateLimiter RateLimiter => _rateLimiter; - - public bool IsTls - { - get - { - lock (_gate) - { - return _isTls; - } - } - } - - public bool IsCompressed - { - get - { - lock (_gate) - { - return _isCompressed; - } - } - } - - /// - /// Test-only: awaited after / writable gate returns and before admission under - /// _gate. Used to reproduce the historical Active-observed / not-yet-counted window. - /// - internal Func? AfterActiveBeforeAdmitProbe { get; set; } - - /// - /// Test-only: awaited inside after write admission and before the stream write. - /// Used to hold SendAsync between ReadAsync and AdvanceTo. - /// - internal Func? BeforeStreamWriteProbe { get; set; } - - /// - /// Test-only: awaited inside after read admission and before the stream read. - /// - internal Func? BeforeStreamReadProbe { get; set; } - - /// - /// Test-only: awaited after a successful stream read returns and before the count is - /// handed back to ReceiveAsync for Advance. - /// - internal Func? AfterStreamReadProbe { get; set; } - - /// DIAGNOSTIC-ONLY session attached when is enabled. - internal TransportIoSession? Io { get; set; } - - public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) - { - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - await WaitUntilReadableAsync(cancellationToken).ConfigureAwait(false); - - if (AfterActiveBeforeAdmitProbe is { } probe) - { - await probe().ConfigureAwait(false); - } - - Stream stream; - CancellationToken readToken; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state != StateActive) - { - continue; - } - - _activeReads++; - stream = _stream; - readToken = _readCts.Token; - } - - using var linked = CancellationTokenSource.CreateLinkedTokenSource(readToken, cancellationToken); - try - { - if (BeforeStreamReadProbe is { } readProbe) - { - await readProbe().ConfigureAwait(false); - } - - var received = await ReadStreamAsync(stream, buffer, linked.Token).ConfigureAwait(false); - if (AfterStreamReadProbe is { } afterRead) - { - await afterRead().ConfigureAwait(false); - } - - return received; - } - catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) - { - continue; - } - finally - { - CompleteOperation(isRead: true); - } - } - } - - public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) - { - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - await WaitUntilWritableAsync(cancellationToken).ConfigureAwait(false); - - if (AfterActiveBeforeAdmitProbe is { } probe) - { - await probe().ConfigureAwait(false); - } - - Stream stream; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state is not (StateActive or StateReadsPaused)) - { - continue; - } - - _activeWrites++; - stream = _stream; - } - - try - { - if (BeforeStreamWriteProbe is { } writeProbe) - { - await writeProbe().ConfigureAwait(false); - } - - await WriteStreamAsync(stream, buffer, cancellationToken).ConfigureAwait(false); - return; - } - finally - { - CompleteOperation(isRead: false); - } - } - } - - /// - /// Flushes the active stream (required for DEFLATE sync-flush so compressed bytes reach the peer). - /// - public async ValueTask FlushAsync(CancellationToken cancellationToken) - { - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - await WaitUntilWritableAsync(cancellationToken).ConfigureAwait(false); - - if (AfterActiveBeforeAdmitProbe is { } probe) - { - await probe().ConfigureAwait(false); - } - - Stream stream; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state is not (StateActive or StateReadsPaused)) - { - continue; - } - - _activeWrites++; - stream = _stream; - } - - try - { - await FlushStreamAsync(stream, cancellationToken).ConfigureAwait(false); - return; - } - finally - { - CompleteOperation(isRead: false); - } - } - } - - /// - /// Gets a value indicating whether the transport is in the reads-paused upgrade drain state. - /// - public bool IsReadsPaused - { - get - { - lock (_gate) - { - return _state is StateReadsPaused or StateQuiesced; - } - } - } - - /// - /// After a successful , decides whether bytes may enter the application input pipe. - /// - /// - /// When quiesced for TLS/DEFLATE upgrade, socket octets already read must not be injected into - /// — they are retained as a prefix for the upgraded stream - /// (ClientHello race after a STARTTLS 382 response). - /// - /// - /// if the caller should write to the application pipe; - /// if the bytes were claimed for the pending upgrade prefix. - /// - public bool TryCommitReadToApplication(ReadOnlySpan data) - { - lock (_gate) - { - if (_state is not (StateReadsPaused or StateQuiesced or StateAbandoned)) - { - return true; - } - - if (data.IsEmpty) - { - return false; - } - - if (_pendingUpgradePrefix is null) - { - _pendingUpgradePrefix = data.ToArray(); - } - else - { - var combined = new byte[_pendingUpgradePrefix.Length + data.Length]; - _pendingUpgradePrefix.CopyTo(combined, 0); - data.CopyTo(combined.AsSpan(_pendingUpgradePrefix.Length)); - _pendingUpgradePrefix = combined; - } - - return false; - } - } - - /// Takes any socket octets retained during quiescence for the upgraded stream wrap. - public ReadOnlyMemory TakePendingUpgradePrefix() - { - lock (_gate) - { - var prefix = _pendingUpgradePrefix; - _pendingUpgradePrefix = null; - return prefix ?? ReadOnlyMemory.Empty; - } - } - - /// - /// Restores Active I/O after a post-quiescence precondition failure (no stream replacement). - /// - public void ResumeFromQuiesceWithoutUpgrade() - { - TaskCompletionSource resume; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state is not (StateQuiesced or StateReadsPaused)) - { - throw new InvalidOperationException("Transport is not quiesced."); - } - - _state = StateActive; - _idleTcs = null; - resume = _resumeTcs; - _pendingUpgradePrefix = null; - } - - resume.TrySetResult(); - } - - /// - /// Cancels in-flight reads and blocks new reads while still allowing writes (outbound drain). - /// - public async Task PauseReadsAsync(CancellationToken cancellationToken) - { - CancellationTokenSource oldReadCts; - TaskCompletionSource? readIdle; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state is StateReadsPaused or StateQuiesced) - { - throw new InvalidOperationException("Transport reads are already paused."); - } - - _state = StateReadsPaused; - _resumeTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - oldReadCts = _readCts; - _readCts = new CancellationTokenSource(); - if (_activeReads == 0) - { - readIdle = null; - } - else - { - readIdle = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - _readIdleTcs = readIdle; - } - } - - await oldReadCts.CancelAsync().ConfigureAwait(false); - oldReadCts.Dispose(); - - if (readIdle is not null) - { - await readIdle.Task.WaitAsync(cancellationToken).ConfigureAwait(false); - } - } - - /// - /// After , blocks writes and waits until no stream I/O is outstanding. - /// - public async Task QuiesceWritesAsync(CancellationToken cancellationToken) - { - TaskCompletionSource idle; - bool alreadyIdle; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state == StateQuiesced) - { - throw new InvalidOperationException("Transport is already quiesced."); - } - - if (_state != StateReadsPaused) - { - throw new InvalidOperationException("Pause reads before quiescing writes."); - } - - _state = StateQuiesced; - idle = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - _idleTcs = idle; - alreadyIdle = _activeReads == 0 && _activeWrites == 0; - } - - if (alreadyIdle) - { - idle.TrySetResult(); - } - - await idle.Task.WaitAsync(cancellationToken).ConfigureAwait(false); - } - - /// - /// Stops application stream I/O and waits until the underlying stream has no outstanding operations. - /// - public async Task QuiesceAsync(CancellationToken cancellationToken) - { - await PauseReadsAsync(cancellationToken).ConfigureAwait(false); - await QuiesceWritesAsync(cancellationToken).ConfigureAwait(false); - } - - /// - /// Returns the quiesced plain stream so the caller can wrap it in . - /// - public Stream TakeQuiescedStreamForTlsWrap() - { - lock (_gate) - { - ThrowIfUnavailable(); - if (_state != StateQuiesced) - { - throw new InvalidOperationException("Transport must be quiesced before TLS wrap."); - } - - if (_isTls) - { - throw new InvalidOperationException("Transport is already TLS-protected."); - } - - if (_isCompressed) - { - throw new InvalidOperationException("Cannot negotiate TLS after DEFLATE is active."); - } - - return _stream; - } - } - - /// - /// Returns the quiesced stream (plain or TLS) so the caller can wrap it in . - /// - public Stream TakeQuiescedStreamForDeflateWrap() - { - lock (_gate) - { - ThrowIfUnavailable(); - if (_state != StateQuiesced) - { - throw new InvalidOperationException("Transport must be quiesced before DEFLATE wrap."); - } - - if (_isCompressed) - { - throw new InvalidOperationException("Transport is already DEFLATE-compressed."); - } - - return _stream; - } - } - - /// - /// Publishes an authenticated and resumes pump I/O. - /// - public void PublishTlsAndResume(SslStream sslStream) - { - ArgumentNullException.ThrowIfNull(sslStream); - TaskCompletionSource resume; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state != StateQuiesced) - { - throw new InvalidOperationException("Transport must be quiesced before publishing TLS."); - } - - if (_isCompressed) - { - throw new InvalidOperationException("Cannot publish TLS after DEFLATE is active."); - } - - _stream = sslStream; - _isTls = true; - _state = StateActive; - _idleTcs = null; - resume = _resumeTcs; - } - - resume.TrySetResult(); - } - - /// - /// Publishes a bidirectional raw-DEFLATE stream and resumes pump I/O. - /// - public void PublishDeflateAndResume(NntpDeflateStream deflateStream) - { - ArgumentNullException.ThrowIfNull(deflateStream); - TaskCompletionSource resume; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state != StateQuiesced) - { - throw new InvalidOperationException("Transport must be quiesced before publishing DEFLATE."); - } - - if (_isCompressed) - { - throw new InvalidOperationException("Transport is already DEFLATE-compressed."); - } - - _stream = deflateStream; - _isCompressed = true; - _state = StateActive; - _idleTcs = null; - resume = _resumeTcs; - } - - resume.TrySetResult(); - } - - /// - /// Marks the transport abandoned after a failed upgrade and wakes waiters for connection teardown. - /// Does not restore plaintext I/O. - /// - public void AbortQuiesceForConnectionTeardown() - { - TaskCompletionSource? resume; - TaskCompletionSource? idle; - lock (_gate) - { - if (_state != StateQuiesced) - { - return; - } - - _state = StateAbandoned; - resume = _resumeTcs; - idle = _idleTcs; - _idleTcs = null; - } - - idle?.TrySetResult(); - resume.TrySetResult(); - } - - public async ValueTask DisposeAsync() - { - Stream stream; - CancellationTokenSource readCts; - TaskCompletionSource resume; - TaskCompletionSource? idle; - lock (_gate) - { - if (_state == StateDisposed) - { - return; - } - - _state = StateDisposed; - stream = _stream; - readCts = _readCts; - resume = _resumeTcs; - idle = _idleTcs; - _idleTcs = null; - } - - idle?.TrySetResult(); - resume.TrySetResult(); - await readCts.CancelAsync().ConfigureAwait(false); - readCts.Dispose(); - await stream.DisposeAsync().ConfigureAwait(false); - } - - private void ThrowIfUnavailable() - { - if (_state == StateAbandoned) - { - throw new ObjectDisposedException(nameof(ConnectionByteTransport), "Transport abandoned after failed TLS upgrade."); - } - - ObjectDisposedException.ThrowIf(_state == StateDisposed, this); - } - - private void CompleteOperation(bool isRead) - { - TaskCompletionSource? idle = null; - TaskCompletionSource? readIdle = null; - lock (_gate) - { - if (isRead) - { - _activeReads--; - } - else - { - _activeWrites--; - } - - if (isRead - && _state == StateReadsPaused - && _readIdleTcs is not null - && _activeReads == 0) - { - readIdle = _readIdleTcs; - _readIdleTcs = null; - } - - if (_state == StateQuiesced - && _idleTcs is not null - && _activeReads == 0 - && _activeWrites == 0) - { - idle = _idleTcs; - } - } - - readIdle?.TrySetResult(); - idle?.TrySetResult(); - } - - private async ValueTask ReadStreamAsync(Stream stream, Memory buffer, CancellationToken cancellationToken) - { - var io = Io; - if (io is null) - { - return await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); - } - - var started = Stopwatch.GetTimestamp(); - var vt = stream.ReadAsync(buffer, cancellationToken); - if (vt.IsCompletedSuccessfully) - { - var n = vt.Result; - io.RecordReceive(buffer.Length, n, started, started, sync: true); - return n; - } - - var awaitStart = Stopwatch.GetTimestamp(); - var received = await vt.ConfigureAwait(false); - io.RecordReceive(buffer.Length, received, started, awaitStart, sync: false); - return received; - } - - private async ValueTask WriteStreamAsync(Stream stream, ReadOnlyMemory buffer, CancellationToken cancellationToken) - { - var io = Io; - if (io is null) - { - await stream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); - return; - } - - var started = Stopwatch.GetTimestamp(); - var vt = stream.WriteAsync(buffer, cancellationToken); - if (vt.IsCompletedSuccessfully) - { - io.RecordSend(buffer.Length, buffer.Length, started, started, sync: true); - return; - } - - var awaitStart = Stopwatch.GetTimestamp(); - await vt.ConfigureAwait(false); - io.RecordSend(buffer.Length, buffer.Length, started, awaitStart, sync: false); - } - - private async ValueTask FlushStreamAsync(Stream stream, CancellationToken cancellationToken) - { - var io = Io; - if (io is null) - { - await stream.FlushAsync(cancellationToken).ConfigureAwait(false); - return; - } - - var started = Stopwatch.GetTimestamp(); - var vt = stream.FlushAsync(cancellationToken); - if (vt.IsCompletedSuccessfully) - { - io.RecordFlush(started, started, sync: true); - return; - } - - var awaitStart = Stopwatch.GetTimestamp(); - await vt.ConfigureAwait(false); - io.RecordFlush(started, awaitStart, sync: false); - } - - private async Task WaitUntilReadableAsync(CancellationToken cancellationToken) - { - while (true) - { - Task wait; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state == StateActive) - { - return; - } - - wait = _resumeTcs.Task; - } - - await wait.WaitAsync(cancellationToken).ConfigureAwait(false); - } - } - - private async Task WaitUntilWritableAsync(CancellationToken cancellationToken) - { - while (true) - { - Task wait; - lock (_gate) - { - ThrowIfUnavailable(); - if (_state is StateActive or StateReadsPaused) - { - return; - } - - wait = _resumeTcs.Task; - } - - await wait.WaitAsync(cancellationToken).ConfigureAwait(false); - } - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/INntpConnection.cs b/src/VectorNNTPD.TransitService/Networking/Transport/INntpConnection.cs deleted file mode 100644 index b0cfc048..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/INntpConnection.cs +++ /dev/null @@ -1,187 +0,0 @@ -using System.IO.Pipelines; -using System.Net; -using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.SessionState.RateLimiting; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// Byte-oriented NNTP connection transport exposed to a future session/protocol layer. - /// - /// - /// - /// carries octets received from the peer (after TLS decryption when applicable). - /// carries octets to send to the peer (before TLS encryption when applicable). - /// - /// - /// No NNTP greeting or command semantics are implemented. The transport does not keep an unused - /// connection alive beyond normal TCP/TLS lifetime and pipeline backpressure. - /// - /// - /// upgrades an established plaintext connection to TLS on the same - /// TCP socket. activates bidirectional raw DEFLATE (RFC 8054) - /// above the current byte stream (plain or TLS). The NNTP STARTTLS and COMPRESS commands live in the - /// session layer and invoke these transport upgrades when appropriate. - /// - /// - public interface INntpConnection : IAsyncDisposable - { - /// - /// Wire-level outbound rate cap, or when the connection - /// does not expose a limiter (test doubles). - /// - IOutboundRateCap? OutboundRate => null; - - /// Gets octets received from the peer (after TLS decryption / DEFLATE inflate when applicable). - PipeReader Input { get; } - - /// Gets the writer used by the application to send octets to the network. - PipeWriter Output { get; } - - /// Gets the remote TCP peer endpoint when known. - EndPoint? RemoteEndPoint { get; } - - /// Gets the local endpoint when known. - EndPoint? LocalEndPoint { get; } - - /// - /// Gets the immutable effective client identity established at connection initialization. - /// - ConnectionClientIdentity ClientIdentity { get; } - - /// Gets a value indicating whether the connection is TLS-protected. - bool IsTls { get; } - - /// - /// When TLS is active, returns the negotiated protocol and cipher suite labels for logging. - /// - /// Operational TLS version label (for example TLSv1.3). - /// Negotiated cipher suite name from . - /// when TLS parameters are available. - bool TryGetNegotiatedTlsParameters(out string tlsVersion, out string cipher); - - /// Gets a value indicating whether bidirectional raw DEFLATE is active. - bool IsCompressed { get; } - - /// Gets a token that is cancelled when the connection transport is shutting down. - CancellationToken ConnectionClosed { get; } - - /// - /// Gets a value indicating whether has been entered (transport is terminal). - /// - bool IsCompleted { get; } - - /// - /// Completes the transport (stops pumps, closes the socket). Safe to call multiple times. - /// - /// Optional exception indicating abortive completion. - Task CompleteAsync(Exception? exception = null); - - /// - /// Gets a monotonic generation bumped each time the send pump becomes idle (awaiting more Output). - /// - /// - /// For STARTTLS: first, capture this version, write 382, then - /// until the generation advances. - /// - long OutboundIdleVersion { get; } - - /// - /// Pauses application socket reads while still allowing writes (STARTTLS handoff ownership). - /// - /// - /// After this returns, successful receive completions are retained for the TLS upgrade prefix - /// instead of being committed to . Writes remain admitted so a 382 - /// response can still reach the peer. Must be called before writing 382. - /// - /// Cancellation token. - Task PauseReadsAsync(CancellationToken cancellationToken = default); - - /// - /// Waits until the send pump has become idle after . - /// - /// - /// captured before flushing pre-upgrade plaintext to . - /// - /// Cancellation token. - Task WaitForOutboundDeliveryAsync( - long outboundIdleVersionBeforeFlush, - CancellationToken cancellationToken = default); - - /// - /// Pauses application reads, then waits until the send pump has become idle after - /// . - /// - /// - /// Prefer before writing 382, then - /// . This combined helper is for callers that already - /// wrote the pre-upgrade response under an existing pause, or that pause immediately after write - /// when no peer ClientHello can race (test helpers). STARTTLS must pause before 382. - /// - Task WaitForOutboundDeliveryAndPauseReadsAsync( - long outboundIdleVersionBeforeFlush, - CancellationToken cancellationToken = default); - - /// - /// Performs an in-place server TLS handshake on the existing TCP socket. - /// - /// Provider used to acquire a connection-lifetime certificate lease. - /// Token that cancels the handshake (and aborts the connection on failure). - /// - /// - /// Preconditions: the connection must be plaintext and not DEFLATE-compressed; the caller must not - /// hold outstanding reads or writes; any plaintext that must - /// be visible to the peer before TLS (for example a STARTTLS 382 response) must already be - /// flushed to (STARTTLS pauses reads before writing 382). - /// The upgrade quiesces writes before inspecting ; any application plaintext - /// that still remains in is a precondition failure (connection stays plaintext). - /// Pipelined NNTP after STARTTLS must be discarded by the session (RFC 8143) before calling this - /// method. Reads may already be paused by . - /// - /// - /// On success, subsequent / traffic is TLS application data - /// on the same socket. is unchanged. On failure, the connection is - /// completed and does not fall back to plaintext. - /// - /// - /// - /// Already TLS, already compressed, upgrade in progress, or unconsumed plaintext remains in - /// (precondition failure does not complete the connection). - /// - /// The connection is closed or disposed. - Task UpgradeToTlsAsync( - ITlsCertificateContextProvider certificateProvider, - CancellationToken cancellationToken = default); - - /// - /// Activates bidirectional raw DEFLATE on the existing connection byte stream. - /// - /// Token that cancels activation (and aborts the connection on failure). - /// - /// - /// Layering (RFC 8054): DEFLATE sits above TLS when both are active — - /// NNTP → DEFLATE → TLS → TCP. TLS must be negotiated first when both are used; this method - /// rejects activation when DEFLATE is already active, and rejects - /// TLS after DEFLATE. - /// - /// - /// Preconditions: DEFLATE not already active; no outstanding reads / - /// writes; application data that belongs before compression must already be - /// consumed from ; any bytes that must reach the peer uncompressed (for example - /// a future 206 COMPRESS response) must already be flushed to . - /// - /// - /// On success, subsequent pipe traffic is compressed in both directions on the same socket and - /// pipes. is unchanged. Post-quiescence failure completes the - /// connection with no uncompressed fallback. - /// - /// - /// - /// Already compressed, upgrade in progress, or unconsumed application data remains in - /// (precondition failure does not complete the connection). - /// - /// The connection is closed or disposed. - Task UpgradeToDeflateAsync(CancellationToken cancellationToken = default); - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/NntpConnection.cs b/src/VectorNNTPD.TransitService/Networking/Transport/NntpConnection.cs deleted file mode 100644 index ab23911a..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/NntpConnection.cs +++ /dev/null @@ -1,1522 +0,0 @@ -using System.Buffers; -using System.Diagnostics; -using System.IO.Pipelines; -using System.Net; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using VectorNNTPD.TransitService.Diagnostics; -using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.TransitService.Networking.Listeners; -using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.SessionState.RateLimiting; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - using VectorNNTPD.Common.Core; - /// - /// Duplex pipe transport over a replaceable byte-stream I/O owner (). - /// - /// - /// - /// Ownership: this type owns the accepted socket and the (plain - /// or authenticated ). The application owns - /// consumption of / production to . - /// - /// - /// Plain connections may call to authenticate TLS on the same socket. - /// Plain or TLS connections may call to activate bidirectional raw - /// DEFLATE above the current byte stream (RFC 8054 layering: NNTP → DEFLATE → TLS → TCP). - /// Pumps keep running across upgrades; exclusive stream ownership is enforced by transport quiescence. - /// The NNTP STARTTLS and COMPRESS commands live in the session layer and invoke these upgrades. - /// - /// - /// NNTPD performs server-side TLS authentication only. Client certificates are never requested or validated. - /// - /// - public sealed class NntpConnection : INntpConnection - { - private const int ModePlain = 0; - private const int ModeUpgrading = 1; - private const int ModeTls = 2; - - private const int CompressionOff = 0; - private const int CompressionUpgrading = 1; - private const int CompressionOn = 2; - - private readonly ILogger _logger; - private readonly Pipe _inputPipe; - private readonly Pipe _outputPipe; - private readonly CancellationTokenSource _connectionCts = new(); - private readonly object _completeGate = new(); - private readonly ConnectionClientIdentity _clientIdentity; - private readonly IFeedDiagnostics? _feedDiagnostics; - private Socket? _socket; - private ConnectionByteTransport? _transport; - private TlsCertificateLease? _certificateLease; - private byte[]? _receivePrefix; - private Task? _receiveTask; - private Task? _sendTask; - private int _mode; - private int _compression; - private int _inplaceUpgradeBusy; - private int _completed; - private int _outputWriterCompleted; - private int _outputReaderCompleted; - private int _inputReaderCompleted; - private int _inputWriterCompleted; - private int _inputReaderConsumer; - private int _disconnectReason; - private int _disconnectedLogged; - private int _disposed; - private int _sendPumpAwaitingOutput; - private long _outboundIdleVersion; - private TaskCompletionSource? _outboundIdleWaiter; - private string? _negotiatedTlsVersion; - private string? _negotiatedCipher; - - private NntpConnection( - Socket socket, - ConnectionByteTransport transport, - EndPoint? remoteEndPoint, - EndPoint? localEndPoint, - bool isTls, - ConnectionClientIdentity clientIdentity, - ILogger logger, - IFeedDiagnostics? feedDiagnostics = null) - { - _socket = socket; - _transport = transport; - RemoteEndPoint = remoteEndPoint; - LocalEndPoint = localEndPoint; - _mode = isTls ? ModeTls : ModePlain; - _clientIdentity = clientIdentity; - _logger = logger; - _feedDiagnostics = feedDiagnostics; - var inputOptions = NntpPipeOptions.Create(); - var outputOptions = NntpPipeOptions.CreateOutput(); - _inputPipe = new Pipe(inputOptions); - _outputPipe = new Pipe(outputOptions); - if (transport.Io is { } io) - { - io.TxPipePauseBytes = outputOptions.PauseWriterThreshold; - io.TxPipeResumeBytes = outputOptions.ResumeWriterThreshold; - } - } - - /// - public PipeReader Input => _inputPipe.Reader; - - /// - public PipeWriter Output => _outputPipe.Writer; - - /// - public IOutboundRateCap? OutboundRate => _transport?.RateLimiter; - - /// - public EndPoint? RemoteEndPoint { get; } - - /// - public EndPoint? LocalEndPoint { get; } - - /// - public ConnectionClientIdentity ClientIdentity => _clientIdentity; - - /// - public bool IsTls => Volatile.Read(ref _mode) == ModeTls; - - /// - public bool TryGetNegotiatedTlsParameters(out string tlsVersion, out string cipher) - { - var version = Volatile.Read(ref _negotiatedTlsVersion); - var suite = Volatile.Read(ref _negotiatedCipher); - if (version is null || suite is null) - { - tlsVersion = string.Empty; - cipher = string.Empty; - return false; - } - - tlsVersion = version; - cipher = suite; - return true; - } - - /// - public bool IsCompressed => Volatile.Read(ref _compression) == CompressionOn; - - /// - public long OutboundIdleVersion => Volatile.Read(ref _outboundIdleVersion); - - /// - public bool IsCompleted => Volatile.Read(ref _completed) == 1; - - /// Test-only: current byte transport (null after teardown closes it). - internal ConnectionByteTransport? ByteTransportForTests => _transport; - - /// Test-only: send pump task started by . - internal Task? SendPumpTaskForTests => _sendTask; - - /// Test-only: whether Output.Writer has been completed. - internal bool OutputWriterCompletedForTests => Volatile.Read(ref _outputWriterCompleted) == 1; - - /// Test-only: whether Output.Reader has been completed exactly once. - internal bool OutputReaderCompletedForTests => Volatile.Read(ref _outputReaderCompleted) == 1; - - /// Test-only: whether Input.Reader has been completed exactly once. - internal bool InputReaderCompletedForTests => Volatile.Read(ref _inputReaderCompleted) == 1; - - /// Test-only: whether Input.Writer has been completed exactly once. - internal bool InputWriterCompletedForTests => Volatile.Read(ref _inputWriterCompleted) == 1; - - /// Test-only: receive pump task started by . - internal Task? ReceivePumpTaskForTests => _receiveTask; - - /// Test-only: 1 when the send pump is awaiting more . - internal int SendPumpAwaitingOutputForTests => Volatile.Read(ref _sendPumpAwaitingOutput); - - /// - public Task PauseReadsAsync(CancellationToken cancellationToken = default) - { - var transport = _transport ?? throw new ObjectDisposedException(nameof(NntpConnection)); - return transport.PauseReadsAsync(cancellationToken); - } - - /// - public Task WaitForOutboundDeliveryAsync( - long outboundIdleVersionBeforeFlush, - CancellationToken cancellationToken = default) => - WaitForOutboundIdleAfterAsync(outboundIdleVersionBeforeFlush, cancellationToken); - - /// - public async Task WaitForOutboundDeliveryAndPauseReadsAsync( - long outboundIdleVersionBeforeFlush, - CancellationToken cancellationToken = default) - { - await PauseReadsAsync(cancellationToken).ConfigureAwait(false); - await WaitForOutboundIdleAfterAsync(outboundIdleVersionBeforeFlush, cancellationToken) - .ConfigureAwait(false); - } - - /// - public CancellationToken ConnectionClosed => _connectionCts.Token; - - /// Creates and starts a plain TCP connection transport. - public static NntpConnection StartPlain( - Socket socket, - ConnectionClientIdentity clientIdentity, - ILogger logger, - ReadOnlyMemory receivePrefix = default, - IFeedDiagnostics? feedDiagnostics = null) - { - ArgumentNullException.ThrowIfNull(socket); - ArgumentNullException.ThrowIfNull(clientIdentity); - ArgumentNullException.ThrowIfNull(logger); - - ConfigureAcceptedSocket(socket); - var network = new NetworkStream(socket, ownsSocket: false); - var transport = new ConnectionByteTransport(network, isTls: false) - { - Io = TransportIoProbe.CreateSession(), - }; - var connection = new NntpConnection( - socket, - transport, - TryGetRemote(socket), - TryGetLocal(socket), - isTls: false, - clientIdentity, - logger, - feedDiagnostics); - if (!receivePrefix.IsEmpty) - { - connection._receivePrefix = receivePrefix.ToArray(); - } - - connection.StartPumps(); - return connection; - } - - /// - /// Performs a server TLS handshake then starts the duplex pipe pumps over the encrypted stream. - /// - public static async Task StartTlsAsync( - Socket socket, - ITlsCertificateContextProvider certificateProvider, - ConnectionClientIdentity clientIdentity, - ILogger logger, - CancellationToken cancellationToken, - ReadOnlyMemory tlsPrefix = default, - IFeedDiagnostics? feedDiagnostics = null) - { - ArgumentNullException.ThrowIfNull(socket); - ArgumentNullException.ThrowIfNull(certificateProvider); - ArgumentNullException.ThrowIfNull(clientIdentity); - ArgumentNullException.ThrowIfNull(logger); - - ConfigureAcceptedSocket(socket); - var remote = TryGetRemote(socket); - var local = TryGetLocal(socket); - var lease = certificateProvider.Acquire(); - - var networkStream = new NetworkStream(socket, ownsSocket: false); - Stream sslInner = tlsPrefix.IsEmpty - ? networkStream - : new PrefixedStream(networkStream, tlsPrefix, leaveInnerOpen: false); - var limiter = new OutboundRateLimiter(sslInner, initialMaxSendBytesPerSecond: 0, leaveInnerOpen: false); - var sslStream = new SslStream(limiter, leaveInnerStreamOpen: false); - - try - { - await sslStream - .AuthenticateAsServerAsync(CreateServerAuthenticationOptions(lease.Context), cancellationToken) - .ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch - { - await sslStream.DisposeAsync().ConfigureAwait(false); - lease.Dispose(); - socket.Dispose(); - throw; - } - - TlsNegotiationLogging.Capture(sslStream, out var tlsVersion, out var cipher); - var transport = new ConnectionByteTransport(sslStream, isTls: true, limiter) - { - Io = TransportIoProbe.CreateSession(), - }; - var connection = new NntpConnection(socket, transport, remote, local, isTls: true, clientIdentity, logger, feedDiagnostics) - { - _certificateLease = lease, - _negotiatedTlsVersion = tlsVersion, - _negotiatedCipher = cipher, - }; - - connection.StartPumps(); - TransportLogMessages.TlsHandshakeCompleted(logger, remote); - return connection; - } - - /// - public async Task UpgradeToTlsAsync( - ITlsCertificateContextProvider certificateProvider, - CancellationToken cancellationToken = default) - { - ArgumentNullException.ThrowIfNull(certificateProvider); - ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); - - if (Volatile.Read(ref _completed) == 1) - { - throw new ObjectDisposedException(nameof(NntpConnection), "Connection is closed."); - } - - if (Volatile.Read(ref _mode) == ModeTls) - { - throw new InvalidOperationException("Connection is already TLS-protected."); - } - - if (Volatile.Read(ref _compression) != CompressionOff) - { - throw new InvalidOperationException("Cannot negotiate TLS after DEFLATE is active."); - } - - if (Interlocked.CompareExchange(ref _inplaceUpgradeBusy, 1, 0) != 0) - { - throw new InvalidOperationException("A transport layer upgrade is already in progress."); - } - - var prior = Interlocked.CompareExchange(ref _mode, ModeUpgrading, ModePlain); - if (prior != ModePlain) - { - Volatile.Write(ref _inplaceUpgradeBusy, 0); - if (prior == ModeUpgrading) - { - throw new InvalidOperationException("A TLS upgrade is already in progress."); - } - - if (prior == ModeTls) - { - throw new InvalidOperationException("Connection is already TLS-protected."); - } - - throw new InvalidOperationException("Connection cannot be upgraded in its current state."); - } - - if (Volatile.Read(ref _compression) != CompressionOff) - { - Volatile.Write(ref _mode, ModePlain); - Volatile.Write(ref _inplaceUpgradeBusy, 0); - throw new InvalidOperationException("Cannot negotiate TLS after DEFLATE is active."); - } - - var transport = _transport ?? throw new ObjectDisposedException(nameof(NntpConnection)); - Exception? failure = null; - var quiesced = false; - var preconditionFailed = false; - - try - { - using var linked = CancellationTokenSource.CreateLinkedTokenSource( - cancellationToken, - _connectionCts.Token); - - // STARTTLS pauses reads before writing 382; other callers may still need a pause here. - if (!transport.IsReadsPaused) - { - await transport.PauseReadsAsync(linked.Token).ConfigureAwait(false); - } - - await WaitForOutboundIdleAsync(linked.Token).ConfigureAwait(false); - await transport.QuiesceWritesAsync(linked.Token).ConfigureAwait(false); - quiesced = true; - - try - { - EnsureApplicationInputDrainedForUpgrade(); - } - catch (InvalidOperationException) - { - preconditionFailed = true; - transport.ResumeFromQuiesceWithoutUpgrade(); - quiesced = false; - Volatile.Write(ref _mode, ModePlain); - throw; - } - - var lease = certificateProvider.Acquire(); - var plainStream = transport.TakeQuiescedStreamForTlsWrap(); - var upgradePrefix = transport.TakePendingUpgradePrefix(); - Stream sslInner = upgradePrefix.IsEmpty - ? plainStream - : new PrefixedStream(plainStream, upgradePrefix, leaveInnerOpen: false); - var sslStream = new SslStream(sslInner, leaveInnerStreamOpen: false); - - try - { - await sslStream - .AuthenticateAsServerAsync(CreateServerAuthenticationOptions(lease.Context), linked.Token) - .ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) - { - try - { - await sslStream.DisposeAsync().ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort; disposes plain NetworkStream as well. - } - - lease.Dispose(); - failure = ex; - throw; - } - - _certificateLease = lease; - TlsNegotiationLogging.Capture(sslStream, out var tlsVersion, out var cipher); - Volatile.Write(ref _negotiatedTlsVersion, tlsVersion); - Volatile.Write(ref _negotiatedCipher, cipher); - transport.PublishTlsAndResume(sslStream); - Volatile.Write(ref _mode, ModeTls); - TransportLogMessages.InPlaceTlsUpgradeCompleted(_logger, RemoteEndPoint); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) when (failure is null && !preconditionFailed) - { - failure = ex; - throw; - } - finally - { - if (failure is not null) - { - // Mark terminal and cancel ConnectionClosed before waking pumps so the session - // dispatcher cannot write a secondary NNTP status onto a dying transport. - if (Volatile.Read(ref _completed) == 0) - { - await CompleteAsync(failure).ConfigureAwait(false); - } - - if (quiesced) - { - transport.AbortQuiesceForConnectionTeardown(); - } - else if (Volatile.Read(ref _mode) == ModeUpgrading) - { - Volatile.Write(ref _mode, ModePlain); - } - - Volatile.Write(ref _inplaceUpgradeBusy, 0); - } - else - { - Volatile.Write(ref _inplaceUpgradeBusy, 0); - } - } - } - - /// - public async Task UpgradeToDeflateAsync(CancellationToken cancellationToken = default) - { - ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); - - if (Volatile.Read(ref _completed) == 1) - { - throw new ObjectDisposedException(nameof(NntpConnection), "Connection is closed."); - } - - if (Volatile.Read(ref _compression) == CompressionOn) - { - throw new InvalidOperationException("Connection is already DEFLATE-compressed."); - } - - if (Interlocked.CompareExchange(ref _inplaceUpgradeBusy, 1, 0) != 0) - { - throw new InvalidOperationException("A transport layer upgrade is already in progress."); - } - - var prior = Interlocked.CompareExchange(ref _compression, CompressionUpgrading, CompressionOff); - if (prior != CompressionOff) - { - Volatile.Write(ref _inplaceUpgradeBusy, 0); - if (prior == CompressionUpgrading) - { - throw new InvalidOperationException("A DEFLATE upgrade is already in progress."); - } - - throw new InvalidOperationException("Connection is already DEFLATE-compressed."); - } - - if (Volatile.Read(ref _mode) == ModeUpgrading) - { - Volatile.Write(ref _compression, CompressionOff); - Volatile.Write(ref _inplaceUpgradeBusy, 0); - throw new InvalidOperationException("A TLS upgrade is already in progress."); - } - - var transport = _transport ?? throw new ObjectDisposedException(nameof(NntpConnection)); - Exception? failure = null; - var quiesced = false; - var preconditionFailed = false; - - try - { - using var linked = CancellationTokenSource.CreateLinkedTokenSource( - cancellationToken, - _connectionCts.Token); - - if (!transport.IsReadsPaused) - { - await transport.PauseReadsAsync(linked.Token).ConfigureAwait(false); - } - - await WaitForOutboundIdleAsync(linked.Token).ConfigureAwait(false); - await transport.QuiesceWritesAsync(linked.Token).ConfigureAwait(false); - quiesced = true; - - try - { - EnsureApplicationInputDrainedForUpgrade(); - } - catch (InvalidOperationException) - { - preconditionFailed = true; - transport.ResumeFromQuiesceWithoutUpgrade(); - quiesced = false; - Volatile.Write(ref _compression, CompressionOff); - throw; - } - - var inner = transport.TakeQuiescedStreamForDeflateWrap(); - var upgradePrefix = transport.TakePendingUpgradePrefix(); - Stream deflateInner = upgradePrefix.IsEmpty - ? inner - : new PrefixedStream(inner, upgradePrefix, leaveInnerOpen: false); - var deflate = new NntpDeflateStream(deflateInner); - transport.PublishDeflateAndResume(deflate); - Volatile.Write(ref _compression, CompressionOn); - TransportLogMessages.InPlaceDeflateActivated(_logger, RemoteEndPoint); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) when (!preconditionFailed) - { - failure = ex; - throw; - } - finally - { - if (failure is not null) - { - if (Volatile.Read(ref _completed) == 0) - { - await CompleteAsync(failure).ConfigureAwait(false); - } - - if (quiesced) - { - transport.AbortQuiesceForConnectionTeardown(); - } - else if (Volatile.Read(ref _compression) == CompressionUpgrading) - { - Volatile.Write(ref _compression, CompressionOff); - } - } - - Volatile.Write(ref _inplaceUpgradeBusy, 0); - } - } - - /// - /// - /// - /// writer completion and connection cancellation may unblock - /// SendAsync. Output.Reader is owned exclusively by the send pump: this - /// method must not complete that reader while SendAsync can still - /// AdvanceTo an examined buffer. After the send pump returns, a best-effort - /// reader complete runs only when the pump did not complete the reader itself. - /// - /// - /// Connection cancellation unblocks the receive pump. Input.Writer is owned - /// exclusively by ReceiveAsync: this method must not complete the writer while - /// GetMemory / Advance can still run. The receive pump completes the writer - /// in finally after it exits. Completing the writer here while the session has - /// already completed the reader would CompletePipe and invalidate the writing head. - /// - /// - /// Input.Reader is owned exclusively by the session RX consumer - /// (NntpSession / NntpContinuousRxReader): this method must not complete - /// the reader while ReadAsync / AdvanceTo can still run. The session - /// completes the reader after the RX loop has stopped. - /// - /// - public async Task CompleteAsync(Exception? exception = null) - { - if (Interlocked.Exchange(ref _completed, 1) == 1) - { - return; - } - - NoteDisconnectReason(ClassifyCompleteReason(exception)); - LogTcpDisconnectedOnce(); - - try - { - await _connectionCts.CancelAsync().ConfigureAwait(false); - } - catch (ObjectDisposedException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - // Already disposed. - } - - try - { - await _outputPipe.Writer.CompleteAsync(exception).ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - finally - { - Interlocked.Exchange(ref _outputWriterCompleted, 1); - } - - var receive = _receiveTask; - var send = _sendTask; - if (receive is not null) - { - try - { - await receive.ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Observed via pump logging. - } - } - else - { - await CompleteInputWriterAsync(exception).ConfigureAwait(false); - } - - if (send is not null) - { - try - { - await send.ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Observed via pump logging. - } - - if (!send.IsCompletedSuccessfully) - { - await CompleteOutputReaderBestEffortAsync(exception).ConfigureAwait(false); - } - } - else - { - await CompleteOutputReaderBestEffortAsync(exception).ConfigureAwait(false); - } - - WriteTransportIoDump(); - await CloseTransportSocketAsync().ConfigureAwait(false); - } - - private void WriteTransportIoDump() - { - var io = _transport?.Io; - if (io is null || !TransportIoProbe.IsEnabled || TransportIoProbe.OutputDirectory is not { } dir) - { - return; - } - - try - { - var label = RemoteEndPoint?.ToString() ?? "connection"; - io.Write(dir, label); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Diagnostic dump must not affect teardown. - } - } - - /// - /// Completes Output.Reader only when the send pump did not already do so. - /// - private async Task CompleteOutputReaderBestEffortAsync(Exception? exception) - { - if (Interlocked.Exchange(ref _outputReaderCompleted, 1) == 1) - { - return; - } - - try - { - await _outputPipe.Reader.CompleteAsync(exception).ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort after a faulted send pump. - } - } - - /// - public async ValueTask DisposeAsync() - { - if (Interlocked.Exchange(ref _disposed, 1) == 1) - { - return; - } - - await CompleteAsync().ConfigureAwait(false); - if (Volatile.Read(ref _inputReaderConsumer) == 0) - { - await CompleteInputReaderAsync().ConfigureAwait(false); - } - - _connectionCts.Dispose(); - _certificateLease?.Dispose(); - _certificateLease = null; - } - - /// - /// Records the first known disconnect cause. Later notes do not overwrite. - /// - internal void NoteDisconnectReason(TcpDisconnectReason reason) - { - if (reason == TcpDisconnectReason.Unspecified) - { - return; - } - - Interlocked.CompareExchange(ref _disconnectReason, (int)reason, (int)TcpDisconnectReason.Unspecified); - } - - /// Test-only: first-wins disconnect reason recorded for this connection. - internal TcpDisconnectReason DisconnectReasonForTests => - (TcpDisconnectReason)Volatile.Read(ref _disconnectReason); - - private static TcpDisconnectReason ClassifyCompleteReason(Exception? exception) => - exception switch - { - null => TcpDisconnectReason.LocalClose, - OperationCanceledException => TcpDisconnectReason.Cancellation, - _ => TcpDisconnectReason.ConnectionClosed, - }; - - private void LogTcpDisconnectedOnce() - { - if (Interlocked.Exchange(ref _disconnectedLogged, 1) == 1) - { - return; - } - - var reason = (TcpDisconnectReason)Volatile.Read(ref _disconnectReason); - if (reason == TcpDisconnectReason.Unspecified) - { - reason = TcpDisconnectReason.ConnectionClosed; - } - - ConnectionAcceptanceLogging.LogDisconnected( - _logger, - RemoteEndPoint, - LocalEndPoint, - reason.ToString()); - } - - /// - /// Marks the session RX loop as the exclusive reader owner. - /// - internal void AttachInputReaderConsumer() => Interlocked.Exchange(ref _inputReaderConsumer, 1); - - /// - /// Completes after the application consumer has stopped reading. - /// Safe to call multiple times. - /// - internal async Task CompleteInputReaderAsync() - { - Interlocked.Exchange(ref _inputReaderConsumer, 0); - if (Interlocked.Exchange(ref _inputReaderCompleted, 1) == 1) - { - return; - } - - try - { - await _inputPipe.Reader.CompleteAsync().ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort; reader may already be completed by a test host. - } - } - - /// - /// Completes Input.Writer exactly once. Owned by ReceiveAsync; - /// calls this only when no receive pump was started. - /// - private async Task CompleteInputWriterAsync(Exception? exception) - { - if (Interlocked.Exchange(ref _inputWriterCompleted, 1) == 1) - { - return; - } - - try - { - await _inputPipe.Writer.CompleteAsync(exception).ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - } - - /// Shared server TLS options (server auth only; no client certificates). - internal static SslServerAuthenticationOptions CreateServerAuthenticationOptions( - SslStreamCertificateContext certificateContext) => - new() - { - ServerCertificateContext = certificateContext, - ClientCertificateRequired = false, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, - CertificateRevocationCheckMode = X509RevocationMode.NoCheck, - }; - - private void StartPumps() - { - _receiveTask = ReceiveAsync(); - _sendTask = SendAsync(); - _ = ObservePumpAsync(_receiveTask, "receive"); - _ = ObservePumpAsync(_sendTask, "send"); - } - - private void EnsureApplicationInputDrainedForUpgrade() - { - if (!_inputPipe.Reader.TryRead(out var result)) - { - return; - } - - try - { - if (!result.Buffer.IsEmpty) - { - throw new InvalidOperationException( - "Cannot upgrade to TLS while unconsumed plaintext remains in Input. " + - "Drain application data before calling UpgradeToTlsAsync."); - } - } - finally - { - // Leave unconsumed bytes in place for the caller (do not examine past start on failure). - _inputPipe.Reader.AdvanceTo(result.Buffer.Start, result.Buffer.Start); - } - } - - private async Task ObservePumpAsync(Task pump, string name) - { - Exception? error = null; - try - { - await pump.ConfigureAwait(false); - } - catch (OperationCanceledException ex) when (_connectionCts.IsCancellationRequested) - { - _ = ex; // observed; intentionally ignored at this boundary - // Expected on shutdown. - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) - { - error = ex; - TransportLogMessages.PumpEndedWithError(_logger, ex, name); - NoteDisconnectReason( - string.Equals(name, "send", StringComparison.Ordinal) - ? TcpDisconnectReason.SendError - : TcpDisconnectReason.ReceiveError); - } - finally - { - if (Volatile.Read(ref _completed) == 0) - { - _ = CompleteAsync(error); - } - } - } - - private async Task WaitForOutboundIdleAsync(CancellationToken cancellationToken) => - await WaitForOutboundIdleAfterAsync(Volatile.Read(ref _outboundIdleVersion) - 1, cancellationToken) - .ConfigureAwait(false); - - private async Task WaitForOutboundIdleAfterAsync( - long outboundIdleVersionBeforeFlush, - CancellationToken cancellationToken) - { - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - - if (Volatile.Read(ref _sendPumpAwaitingOutput) == 1 - && Volatile.Read(ref _outboundIdleVersion) > outboundIdleVersionBeforeFlush) - { - return; - } - - var waiter = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - Volatile.Write(ref _outboundIdleWaiter, waiter); - - if (Volatile.Read(ref _sendPumpAwaitingOutput) == 1 - && Volatile.Read(ref _outboundIdleVersion) > outboundIdleVersionBeforeFlush) - { - Volatile.Write(ref _outboundIdleWaiter, null); - waiter.TrySetResult(); - return; - } - - await waiter.Task.WaitAsync(cancellationToken).ConfigureAwait(false); - } - } - - private void SignalOutboundIdle() - { - Interlocked.Increment(ref _outboundIdleVersion); - Volatile.Write(ref _sendPumpAwaitingOutput, 1); - var waiter = Interlocked.Exchange(ref _outboundIdleWaiter, null); - waiter?.TrySetResult(); - } - - private void SignalOutboundBusy() => Volatile.Write(ref _sendPumpAwaitingOutput, 0); - - private static async ValueTask FlushInputAsync( - PipeWriter writer, - CancellationToken token, - TransportIoSession? io) - { - if (io is null) - { - return await writer.FlushAsync(token).ConfigureAwait(false); - } - - var started = Stopwatch.GetTimestamp(); - var vt = writer.FlushAsync(token); - if (vt.IsCompletedSuccessfully) - { - io.RecordRxPipeFlush(started, started, sync: true); - return vt.Result; - } - - var awaitStart = Stopwatch.GetTimestamp(); - var result = await vt.ConfigureAwait(false); - io.RecordRxPipeFlush(started, awaitStart, sync: false); - return result; - } - - private static async ValueTask ReadOutputAsync( - PipeReader reader, - CancellationToken token, - TransportIoSession? io) - { - if (io is null) - { - return await reader.ReadAsync(token).ConfigureAwait(false); - } - - var started = Stopwatch.GetTimestamp(); - var vt = reader.ReadAsync(token); - if (vt.IsCompletedSuccessfully) - { - var completed = vt.Result; - io.RecordTxPipeWait(started, started, sync: true); - io.RecordTxReadShape(completed.Buffer); - return completed; - } - - var awaitStart = Stopwatch.GetTimestamp(); - var result = await vt.ConfigureAwait(false); - io.RecordTxPipeWait(started, awaitStart, sync: false); - io.RecordTxReadShape(result.Buffer); - return result; - } - - private async Task ReceiveAsync() - { - Exception? error = null; - try - { - var transport = _transport ?? throw new InvalidOperationException("Transport missing."); - var writer = _inputPipe.Writer; - var token = _connectionCts.Token; - - var prefix = Interlocked.Exchange(ref _receivePrefix, null); - if (prefix is { Length: > 0 }) - { - var memory = writer.GetMemory(prefix.Length); - prefix.CopyTo(memory); - writer.Advance(prefix.Length); - RecordReceivedBytes(prefix.Length); - var prefixFlush = await FlushInputAsync(writer, token, transport.Io).ConfigureAwait(false); - if (prefixFlush.IsCompleted || prefixFlush.IsCanceled) - { - return; - } - } - - while (!token.IsCancellationRequested) - { - var memory = writer.GetMemory(NntpPipeOptions.MinimumSegmentSize); - var bytes = await transport.ReadAsync(memory, token).ConfigureAwait(false); - if (bytes == 0) - { - if (!token.IsCancellationRequested) - { - NoteDisconnectReason(TcpDisconnectReason.RemoteClosed); - } - - break; - } - - // If Quiesce completed while this read returned, octets belong to the upgrade wrap - // (e.g. TLS ClientHello), not the application Input pipe. - if (!transport.TryCommitReadToApplication(memory.Span[..bytes])) - { - continue; - } - - RecordReceivedBytes(bytes); - writer.Advance(bytes); - var flush = await FlushInputAsync(writer, token, transport.Io).ConfigureAwait(false); - if (flush.IsCompleted || flush.IsCanceled) - { - break; - } - } - } - catch (OperationCanceledException ex) when (_connectionCts.IsCancellationRequested) - { - _ = ex; // observed; intentionally ignored at this boundary - // Expected on shutdown; complete the writer without faulting the reader. - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ex) - { - error = ex; - throw; - } - finally - { - await CompleteInputWriterAsync(error).ConfigureAwait(false); - } - } - - private void RecordReceivedBytes(int bytes) - { - if (bytes > 0 && _feedDiagnostics is { IsEnabled: true }) - { - _feedDiagnostics.RecordTcpBytes(bytes); - } - } - - private async Task SendAsync() - { - var transport = _transport ?? throw new InvalidOperationException("Transport missing."); - var reader = _outputPipe.Reader; - var token = _connectionCts.Token; - var socket = _socket; - var io = transport.Io; - io?.RecordSendLoopStart(); - var iterationEndTs = 0L; - TxWriteCoalescer? coalescer = null; - - while (!token.IsCancellationRequested) - { - SignalOutboundIdle(); - var readIssuedTs = 0L; - if (io is not null) - { - readIssuedTs = Stopwatch.GetTimestamp(); - if (iterationEndTs != 0) - { - io.RecordAdvanceToNextRead(iterationEndTs, readIssuedTs); - } - } - - var result = await ReadOutputAsync(reader, token, io).ConfigureAwait(false); - var readDoneTs = io is not null ? Stopwatch.GetTimestamp() : 0L; - SignalOutboundBusy(); - var buffer = result.Buffer; - var consumed = buffer.Start; - var firstWriteTs = 0L; - var lastWriteEndTs = 0L; - try - { - if (buffer.IsEmpty && result.IsCompleted) - { - if (coalescer is { PendingCount: > 0 }) - { - lastWriteEndTs = await WriteCoalescedPendingAsync( - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - } - - break; - } - - if (TxWriteGranularityExperiment.TryResolveTarget(transport, out var targetBytes)) - { - if (io is not null) - { - io.WriteGranularityTargetBytes = targetBytes; - } - - if (coalescer is null || coalescer.TargetBytes != targetBytes) - { - if (coalescer is { PendingCount: > 0 }) - { - lastWriteEndTs = await WriteCoalescedPendingAsync( - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - firstWriteTs = lastWriteEndTs; - } - - coalescer = new TxWriteCoalescer(targetBytes); - } - - lastWriteEndTs = await CopyAndWriteAggregatesAsync( - transport, - coalescer, - buffer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - } - else - { - if (coalescer is { PendingCount: > 0 }) - { - lastWriteEndTs = await WriteCoalescedPendingAsync( - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - coalescer.Clear(); - coalescer = null; - } - - foreach (var segment in buffer) - { - if (segment.Length == 0) - { - continue; - } - - var writeStartTs = io is not null ? Stopwatch.GetTimestamp() : 0L; - if (io is not null) - { - if (firstWriteTs == 0) - { - firstWriteTs = writeStartTs; - io.RecordReadToFirstWrite(readDoneTs, firstWriteTs); - } - else - { - io.RecordBetweenWrites(lastWriteEndTs, writeStartTs); - } - } - - await transport.WriteAsync(segment, token).ConfigureAwait(false); - if (io is not null) - { - lastWriteEndTs = Stopwatch.GetTimestamp(); - io.RecordTransportWrite(writeStartTs, lastWriteEndTs); - } - } - - // Required for DEFLATE: sync-flush compressed bytes to the peer without ending the stream. - // No-op / cheap for NetworkStream and SslStream over NetworkStream. - await transport.FlushAsync(token).ConfigureAwait(false); - } - - consumed = buffer.End; - } - finally - { - var advanceStartTs = io is not null ? Stopwatch.GetTimestamp() : 0L; - if (io is not null && lastWriteEndTs != 0) - { - io.RecordLastWriteToAdvance(lastWriteEndTs, advanceStartTs); - } - - reader.AdvanceTo(consumed); - if (io is not null) - { - var advanceEndTs = Stopwatch.GetTimestamp(); - io.RecordAdvanceTo(advanceStartTs, advanceEndTs); - iterationEndTs = advanceEndTs; - } - } - - if (coalescer is not null && !token.IsCancellationRequested) - { - var drained = await DrainImmediateReadsAsync( - reader, - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - lastWriteEndTs = drained.LastWriteEndTs; - - if (coalescer.PendingCount > 0) - { - _ = await WriteCoalescedPendingAsync( - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - } - - if (drained.Completed) - { - break; - } - } - - if (result.IsCompleted) - { - break; - } - } - - io?.RecordSendLoopEnd(); - await reader.CompleteAsync().ConfigureAwait(false); - Interlocked.Exchange(ref _outputReaderCompleted, 1); - - // Half-close TCP send only for plaintext end-of-stream; TLS shutdown is via SslStream dispose. - if (socket is not null && !IsTls) - { - try - { - socket.Shutdown(SocketShutdown.Send); - } - catch (SocketException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - } - catch (ObjectDisposedException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - } - } - } - - private static async ValueTask<(long LastWriteEndTs, bool Completed)> DrainImmediateReadsAsync( - PipeReader reader, - ConnectionByteTransport transport, - TxWriteCoalescer coalescer, - TransportIoSession? io, - long readDoneTs, - long firstWriteTs, - long lastWriteEndTs, - CancellationToken token) - { - var completed = false; - while (reader.TryRead(out var extra)) - { - var extraConsumed = extra.Buffer.Start; - try - { - if (extra.Buffer.IsEmpty) - { - completed = extra.IsCompleted; - break; - } - - io?.RecordTxReadShape(extra.Buffer); - lastWriteEndTs = await CopyAndWriteAggregatesAsync( - transport, - coalescer, - extra.Buffer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - extraConsumed = extra.Buffer.End; - completed = extra.IsCompleted; - } - finally - { - reader.AdvanceTo(extraConsumed); - } - - if (completed) - { - break; - } - } - - return (lastWriteEndTs, completed); - } - - private static async ValueTask CopyAndWriteAggregatesAsync( - ConnectionByteTransport transport, - TxWriteCoalescer coalescer, - ReadOnlySequence buffer, - TransportIoSession? io, - long readDoneTs, - long firstWriteTs, - long lastWriteEndTs, - CancellationToken token) - { - foreach (var segment in buffer) - { - if (segment.Length == 0) - { - continue; - } - - var offset = 0; - while (offset < segment.Length) - { - if (coalescer.RemainingCapacity == 0) - { - lastWriteEndTs = await WriteCoalescedPendingAsync( - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - firstWriteTs = firstWriteTs == 0 ? lastWriteEndTs : firstWriteTs; - } - - offset += coalescer.Copy(segment.Span[offset..], io); - } - } - - if (coalescer.IsFull) - { - lastWriteEndTs = await WriteCoalescedPendingAsync( - transport, - coalescer, - io, - readDoneTs, - firstWriteTs, - lastWriteEndTs, - token).ConfigureAwait(false); - } - - return lastWriteEndTs; - } - - private static async ValueTask WriteCoalescedPendingAsync( - ConnectionByteTransport transport, - TxWriteCoalescer coalescer, - TransportIoSession? io, - long readDoneTs, - long firstWriteTs, - long lastWriteEndTs, - CancellationToken token) - { - if (coalescer.PendingCount == 0) - { - return lastWriteEndTs; - } - - var writeStartTs = io is not null ? Stopwatch.GetTimestamp() : 0L; - if (io is not null) - { - if (firstWriteTs == 0 && lastWriteEndTs == 0) - { - io.RecordReadToFirstWrite(readDoneTs, writeStartTs); - } - else - { - io.RecordBetweenWrites(lastWriteEndTs, writeStartTs); - } - } - - await transport.WriteAsync(coalescer.PendingMemory, token).ConfigureAwait(false); - var writeEndTs = io is not null ? Stopwatch.GetTimestamp() : 0L; - if (io is not null) - { - io.RecordTransportWrite(writeStartTs, writeEndTs); - } - - await transport.FlushAsync(token).ConfigureAwait(false); - coalescer.Clear(); - return writeEndTs; - } - - private async Task CloseTransportSocketAsync() - { - ConnectionByteTransport? transport; - Socket? socket; - lock (_completeGate) - { - transport = _transport; - _transport = null; - socket = _socket; - _socket = null; - } - - if (transport is not null) - { - try - { - await transport.DisposeAsync().ConfigureAwait(false); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - } - - if (socket is not null) - { - try - { - socket.Dispose(); - } - // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. - catch (Exception ignored) when (!ExceptionHelpers.IsFatal(ignored)) - { - _ = ignored; // observed; intentionally ignored at this best-effort boundary - // Best-effort. - } - } - } - - private static void ConfigureAcceptedSocket(Socket socket) - { - try - { - socket.NoDelay = true; - } - catch (SocketException ex) - { - _ = ex; // observed; intentionally ignored at this boundary - } - } - - private static EndPoint? TryGetRemote(Socket socket) - { - try - { - return socket.RemoteEndPoint; - } - catch (SocketException) - { - return null; - } - catch (ObjectDisposedException) - { - return null; - } - } - - private static EndPoint? TryGetLocal(Socket socket) - { - try - { - return socket.LocalEndPoint; - } - catch (SocketException) - { - return null; - } - catch (ObjectDisposedException) - { - return null; - } - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/NntpDeflateStream.cs b/src/VectorNNTPD.TransitService/Networking/Transport/NntpDeflateStream.cs deleted file mode 100644 index 7de0187a..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/NntpDeflateStream.cs +++ /dev/null @@ -1,148 +0,0 @@ -using System.IO.Compression; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// Bidirectional raw DEFLATE transform for an NNTP connection byte stream (RFC 8054 / RFC 1951). - /// - /// - /// - /// Wraps an underlying duplex stream (plain or - /// ) with independent compressor and decompressor state. - /// Application writes are compressed toward the peer; application reads are decompressed from the peer. - /// - /// - /// Wire format is raw DEFLATE (zlib windowBits in the negative range), matching - /// RFC 8054 §4. This uses , which does not emit a zlib (RFC 1950) - /// wrapper or a gzip wrapper. Do not substitute or . - /// - /// - /// forwards to - /// . On .NET 10 that - /// implementation uses DEFLATE sync-flush semantics so pending compressed bytes become visible on - /// the underlying stream while retaining the sliding dictionary. Finalization (end-of-stream - /// trailer) occurs only when this stream is disposed. - /// - /// - /// Concurrent and - /// are supported - /// (full-duplex): the inflater only reads the inner stream; the deflater only writes it. - /// - /// - internal sealed class NntpDeflateStream : Stream - { - private readonly Stream _inner; - private readonly DeflateStream _compressor; - private readonly DeflateStream _decompressor; - private bool _disposed; - - /// - /// Creates a duplex raw-DEFLATE layer over . - /// - /// Underlying duplex byte stream; ownership transfers to this instance. - /// zlib-compatible compression level for the outbound compressor. - public NntpDeflateStream(Stream inner, CompressionLevel compressionLevel = CompressionLevel.Optimal) - { - ArgumentNullException.ThrowIfNull(inner); - if (!inner.CanRead || !inner.CanWrite) - { - throw new ArgumentException("Inner stream must be readable and writable.", nameof(inner)); - } - - _inner = inner; - // leaveOpen: true — this type owns and disposes the inner stream exactly once. - _compressor = new DeflateStream(inner, compressionLevel, leaveOpen: true); - _decompressor = new DeflateStream(inner, CompressionMode.Decompress, leaveOpen: true); - } - - /// - public override bool CanRead => !_disposed; - - /// - public override bool CanSeek => false; - - /// - public override bool CanWrite => !_disposed; - - /// - public override long Length => throw new NotSupportedException(); - - /// - public override long Position - { - get => throw new NotSupportedException(); - set => throw new NotSupportedException(); - } - - /// - public override void Flush() => _compressor.Flush(); - - /// - public override Task FlushAsync(CancellationToken cancellationToken) => - _compressor.FlushAsync(cancellationToken); - - /// - public override int Read(byte[] buffer, int offset, int count) => - _decompressor.Read(buffer, offset, count); - - /// - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => - _decompressor.ReadAsync(buffer, offset, count, cancellationToken); - - /// - public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) => - _decompressor.ReadAsync(buffer, cancellationToken); - - /// - public override void Write(byte[] buffer, int offset, int count) => - _compressor.Write(buffer, offset, count); - - /// - public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => - _compressor.WriteAsync(buffer, offset, count, cancellationToken); - - /// - public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) => - _compressor.WriteAsync(buffer, cancellationToken); - - /// - public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); - - /// - public override void SetLength(long value) => throw new NotSupportedException(); - - /// - protected override void Dispose(bool disposing) - { - if (_disposed) - { - return; - } - - if (disposing) - { - // Compressor dispose finalizes the DEFLATE stream (writes remaining bits / end marker). - _compressor.Dispose(); - _decompressor.Dispose(); - _inner.Dispose(); - } - - _disposed = true; - base.Dispose(disposing); - } - - /// - public override async ValueTask DisposeAsync() - { - if (_disposed) - { - return; - } - - _disposed = true; - await _compressor.DisposeAsync().ConfigureAwait(false); - await _decompressor.DisposeAsync().ConfigureAwait(false); - await _inner.DisposeAsync().ConfigureAwait(false); - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/NntpPipeOptions.cs b/src/VectorNNTPD.TransitService/Networking/Transport/NntpPipeOptions.cs deleted file mode 100644 index ecbacd3d..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/NntpPipeOptions.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System.IO.Pipelines; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// Centralized defaults for NNTP connection transports. - /// - /// - /// Per-connection thresholds (not global). Values are engineering defaults for bounded - /// memory under many concurrent clients; they are not a claim about 10/40 Gbps throughput. - /// - public static class NntpPipeOptions - { - /// Pause the pipe writer (and thus receive) when unread bytes reach this size. - public const long PauseWriterThreshold = 64 * 1024; - - /// Resume the pipe writer after unread bytes fall to this size. - public const long ResumeWriterThreshold = 32 * 1024; - - /// Minimum pooled segment size for pipe buffers. - public const int MinimumSegmentSize = 4 * 1024; - - /// Creates reader/writer options for the input (RX) side of a duplex connection transport. - public static PipeOptions Create() => - CreateCore(PauseWriterThreshold, ResumeWriterThreshold, PipeScheduler.ThreadPool, PipeScheduler.ThreadPool); - - /// - /// TX output-pipe options. Same thresholds as unless - /// is configured. Reader and writer schedulers - /// are . Input/RX pipes must keep using - /// . - /// - internal static PipeOptions CreateOutput() - { - if (TxPipePauseExperiment.TryGetEffective(out var pause, out var resume)) - { - return CreateCore(pause, resume, PipeScheduler.Inline, PipeScheduler.Inline); - } - - return CreateCore(PauseWriterThreshold, ResumeWriterThreshold, PipeScheduler.Inline, PipeScheduler.Inline); - } - - private static PipeOptions CreateCore( - long pauseWriterThreshold, - long resumeWriterThreshold, - PipeScheduler readerScheduler, - PipeScheduler writerScheduler) => - new( - pool: null, - readerScheduler: readerScheduler, - writerScheduler: writerScheduler, - pauseWriterThreshold: pauseWriterThreshold, - resumeWriterThreshold: resumeWriterThreshold, - minimumSegmentSize: MinimumSegmentSize, - useSynchronizationContext: false); - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/SocketPayloadSender.cs b/src/VectorNNTPD.TransitService/Networking/Transport/SocketPayloadSender.cs deleted file mode 100644 index c26cd975..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/SocketPayloadSender.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System.Net.Sockets; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// Completes a TCP send of an entire buffer, looping on partial sends. - /// - internal static class SocketPayloadSender - { - /// - /// Sends on until every byte is accepted or the - /// operation fails / is cancelled. - /// - public static ValueTask SendAllAsync( - Socket socket, - ReadOnlyMemory buffer, - CancellationToken cancellationToken) => - SendAllAsync( - static (memory, token, state) => - ((Socket)state!).SendAsync(memory, SocketFlags.None, token), - buffer, - socket, - cancellationToken); - - /// - /// Testable send loop over an arbitrary send delegate (no heap copy of payload). - /// - /// - /// Sends as many bytes as possible from the supplied memory; return value is the count accepted. - /// - /// Payload to transmit completely. - /// Opaque state passed to . - /// Cancellation token. - public static async ValueTask SendAllAsync( - Func, CancellationToken, object?, ValueTask> sendAsync, - ReadOnlyMemory buffer, - object? state, - CancellationToken cancellationToken) - { - ArgumentNullException.ThrowIfNull(sendAsync); - - var remaining = buffer; - while (!remaining.IsEmpty) - { - cancellationToken.ThrowIfCancellationRequested(); - var sent = await sendAsync(remaining, cancellationToken, state).ConfigureAwait(false); - if (sent == 0) - { - throw new IOException("Socket send transferred zero bytes."); - } - - if ((uint)sent > (uint)remaining.Length) - { - throw new IOException( - $"Socket send reported {sent} bytes but only {remaining.Length} were requested."); - } - - remaining = remaining[sent..]; - } - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TcpDisconnectReason.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TcpDisconnectReason.cs deleted file mode 100644 index 2e25d38d..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TcpDisconnectReason.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// Why an established TCP connection completed. Values are only those the existing - /// teardown paths can set from known cause, not inferred from timing. - /// - internal enum TcpDisconnectReason - { - /// No path has recorded a cause yet. - Unspecified = 0, - - /// Receive pump observed a 0-byte read (peer FIN) before local cancellation. - RemoteClosed = 1, - - /// was invoked without an error or prior cause. - LocalClose = 2, - - /// The receive pump faulted. - ReceiveError = 3, - - /// The send pump faulted. - SendError = 4, - - /// was invoked with . - Cancellation = 5, - - /// Listener stop completed remaining connections. - Shutdown = 6, - - /// Session RequestClose (QUIT and other protocol-initiated close). - ProtocolClose = 7, - - /// Completed with an exception that is not a receive/send pump fault. - ConnectionClosed = 8, - - /// No NNTP command was executed within Nntpd:IdleTime. - IdleTimeout = 9, - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TlsNegotiationLogging.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TlsNegotiationLogging.cs deleted file mode 100644 index 07814779..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TlsNegotiationLogging.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Net.Security; -using System.Security.Authentication; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// Formats negotiated parameters for operational logs. - internal static class TlsNegotiationLogging - { - /// Builds the STARTTLS / TLS acceptance detail fragment. - public static string FormatDetail(string tlsVersion, string cipher) => - $"TlsVersion={tlsVersion}, Cipher={cipher}"; - - /// Maps to the operational TLSv1.x label. - public static string FormatSslProtocol(SslProtocols protocol) => - protocol switch - { - SslProtocols.Tls13 => "TLSv1.3", - SslProtocols.Tls12 => "TLSv1.2", - _ => protocol.ToString(), - }; - - /// Formats the negotiated cipher suite name. - public static string FormatCipherSuite(TlsCipherSuite cipherSuite) => cipherSuite.ToString(); - - /// Captures version and cipher labels from an authenticated . - public static void Capture(SslStream sslStream, out string tlsVersion, out string cipher) - { - ArgumentNullException.ThrowIfNull(sslStream); - tlsVersion = FormatSslProtocol(sslStream.SslProtocol); - cipher = FormatCipherSuite(sslStream.NegotiatedCipherSuite); - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoProbe.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoProbe.cs deleted file mode 100644 index 911055f1..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoProbe.cs +++ /dev/null @@ -1,87 +0,0 @@ -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// DIAGNOSTIC-ONLY. Off-by-default ConnectionByteTransport socket/Pipe cadence sampler. - /// - /// - /// Disabled unless VECTORNNTP_TRANSPORT_IO is set or is called. - /// When disabled, no session state is allocated and transport I/O is unchanged. - /// This is not a production architecture change. - /// - internal static class TransportIoProbe - { - /// Environment variable that enables transport I/O cadence sampling. - public const string EnvironmentVariableName = "VECTORNNTP_TRANSPORT_IO"; - - private static ProbeState? _state; - - static TransportIoProbe() - { - var value = Environment.GetEnvironmentVariable(EnvironmentVariableName); - if (string.IsNullOrWhiteSpace(value) || - value.Equals("0", StringComparison.OrdinalIgnoreCase) || - value.Equals("false", StringComparison.OrdinalIgnoreCase)) - { - return; - } - - var dir = value.Equals("1", StringComparison.OrdinalIgnoreCase) || - value.Equals("true", StringComparison.OrdinalIgnoreCase) - ? DefaultDirectory() - : value; - Enable(dir); - } - - /// Gets whether cadence timestamps should be recorded. - public static bool IsEnabled => Volatile.Read(ref _state) is not null; - - /// Gets the directory that receives connection dumps, or . - public static string? OutputDirectory => Volatile.Read(ref _state)?.Directory; - - /// Enables sampling and writes dumps under . - public static void Enable(string outputDirectory) - { - ArgumentException.ThrowIfNullOrWhiteSpace(outputDirectory); - Directory.CreateDirectory(outputDirectory); - Volatile.Write(ref _state, new ProbeState(outputDirectory)); - } - - /// Disables sampling (tests). - public static void Disable() => Volatile.Write(ref _state, null); - - /// Creates a per-connection log when enabled; otherwise . - public static TransportIoSession? CreateSession() => - IsEnabled ? new TransportIoSession() : null; - - /// - /// Repo-local diagnostic dump directory. - /// - /// - /// Deliberately walks from the process working directory toward a folder that - /// contains VectorNNTPD.TransitService.sln or .artifacts/. This is - /// development-only output, off unless is set. - /// It is not application-local runtime state and must not be used for certs/logs. - /// - internal static string DefaultDirectory() - { - var dir = new DirectoryInfo(Directory.GetCurrentDirectory()); - while (dir is not null) - { - if (File.Exists(Path.Join(dir.FullName, "VectorNNTPD.TransitService.sln")) || - Directory.Exists(Path.Join(dir.FullName, ".artifacts"))) - { - return Path.Join(dir.FullName, ".artifacts", "transport-io"); - } - - dir = dir.Parent; - } - - return Path.Join(Directory.GetCurrentDirectory(), ".artifacts", "transport-io"); - } - - private sealed class ProbeState(string directory) - { - public string Directory { get; } = directory; - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoReport.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoReport.cs deleted file mode 100644 index 8ff32581..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoReport.cs +++ /dev/null @@ -1,294 +0,0 @@ -using System.Globalization; -using System.Text; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// DIAGNOSTIC-ONLY formatter for . - internal static class TransportIoReport - { - /// Formats a human-readable cadence report. - public static string Format(in TransportIoSnapshot snapshot, string sessionLabel) - { - var sb = new StringBuilder(); - sb.AppendLine("CONNECTION BYTE TRANSPORT I/O CADENCE — DIAGNOSTIC"); - sb.AppendLine(); - sb.AppendLine("Off-by-default. Production path is unchanged when VECTORNNTP_TRANSPORT_IO is unset."); - sb.AppendLine("Clock: Stopwatch.GetTimestamp (monotonic). Durations in microseconds."); - sb.AppendLine("Stream ops are NetworkStream/SslStream ReadAsync/WriteAsync/FlushAsync, not raw Socket APIs."); - sb.AppendLine("Sync = ValueTask.IsCompletedSuccessfully before await. Await duration is 0 when sync."); - sb.AppendLine("RX pipe flush is PipeWriter.FlushAsync after Advance (may include backpressure wait)."); - sb.AppendLine("TX pipe wait is PipeReader.ReadAsync and includes waiting for the application producer."); - sb.AppendLine(); - sb.AppendLine($"Session: {sessionLabel}"); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $"Connection elapsed: {snapshot.ConnectionElapsed.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $"TX pipe pause: {snapshot.TxPipePauseBytes:N0} bytes")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $"TX pipe resume: {snapshot.TxPipeResumeBytes:N0} bytes")); - var experiment = TxPipePauseExperiment.IsConfigured ? "active (VECTORNNTP_TX_PIPE_PAUSE)" : "inactive (production 64 KiB)"; - sb.AppendLine($"TX pipe experiment: {experiment}"); - var coalesce = snapshot.Coalesce.TargetBytes > 0 - ? $"active (VECTORNNTP_TX_WRITE_GRANULARITY {snapshot.Coalesce.TargetBytes:N0} bytes)" - : "inactive (one WriteAsync per Pipe segment)"; - sb.AppendLine($"TX write coalesce: {coalesce}"); - sb.AppendLine(); - - WriteDirection(sb, "TX (stream WriteAsync)", snapshot.Tx, snapshot.TxWindow); - WriteDirection(sb, "RX (stream ReadAsync)", snapshot.Rx, snapshot.RxWindow); - - sb.AppendLine("STREAM FlushAsync (not included in TX send stats)"); - WriteSimple(sb, snapshot.FlushOps, snapshot.FlushSync, snapshot.FlushAsync, snapshot.FlushUs); - sb.AppendLine(); - - sb.AppendLine("RX PipeWriter.FlushAsync (pipe processing / backpressure)"); - WriteSimple(sb, snapshot.RxPipeFlushes, snapshot.RxPipeSync, snapshot.RxPipeAsync, snapshot.RxPipeFlushUs); - sb.AppendLine(); - - sb.AppendLine("TX PipeReader.ReadAsync (includes producer wait; not socket time)"); - WriteSimple(sb, snapshot.TxPipeWaits, snapshot.TxPipeSync, snapshot.TxPipeAsync, snapshot.TxPipeWaitUs); - sb.AppendLine(); - - WriteReadShape(sb, snapshot.TxRead, snapshot.Tx.Ops); - sb.AppendLine(); - - WriteTxLoop(sb, snapshot); - sb.AppendLine(); - WriteWriteCoalesce(sb, snapshot.Coalesce); - sb.AppendLine(); - - if (snapshot.Tx.Ops > 0 && snapshot.Tx.Bytes > 0) - { - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $"TX ops/MiB: {snapshot.Tx.Ops / (snapshot.Tx.Bytes / (1024.0 * 1024.0)):F2}")); - } - - if (snapshot.Rx.Ops > 0 && snapshot.Rx.Bytes > 0) - { - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $"RX ops/MiB: {snapshot.Rx.Ops / (snapshot.Rx.Bytes / (1024.0 * 1024.0)):F2}")); - } - - return sb.ToString(); - } - - /// Nearest-rank percentile of a sorted sample (microseconds). - internal static double Percentile(int[] sorted, double p) - { - if (sorted.Length == 0) - { - return 0; - } - - if (sorted.Length == 1) - { - return sorted[0]; - } - - var position = p * (sorted.Length - 1); - var lo = (int)position; - var hi = Math.Min(lo + 1, sorted.Length - 1); - var frac = position - lo; - return sorted[lo] + ((sorted[hi] - sorted[lo]) * frac); - } - - internal static (double Mean, double P50, double P95, double P99, int Max) Stats(int[] samples) - { - if (samples.Length == 0) - { - return (0, 0, 0, 0, 0); - } - - var copy = (int[])samples.Clone(); - Array.Sort(copy); - long sum = 0; - for (var i = 0; i < copy.Length; i++) - { - sum += copy[i]; - } - - return (sum / (double)copy.Length, Percentile(copy, 0.50), Percentile(copy, 0.95), Percentile(copy, 0.99), copy[^1]); - } - - private static void WriteDirection(StringBuilder sb, string title, in TransportIoDirection d, TimeSpan window) - { - sb.AppendLine(title); - sb.AppendLine($" operations: {d.Ops:N0}"); - sb.AppendLine($" bytes: {d.Bytes:N0}"); - sb.AppendLine($" requested bytes: {d.Requested:N0}"); - if (d.Ops > 0) - { - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" avg bytes/op: {d.Bytes / (double)d.Ops:F1}")); - sb.AppendLine($" min bytes/op: {d.MinBytes:N0}"); - sb.AppendLine($" max bytes/op: {d.MaxBytes:N0}"); - } - - var gbit = window.TotalSeconds > 0 && d.Bytes > 0 - ? d.Bytes * 8d / window.TotalSeconds / 1_000_000_000d - : 0; - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" I/O window: {window.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" throughput: {gbit:F6} Gbit/s (bytes / I/O window)")); - var total = d.Sync + d.Async; - var syncPct = total > 0 ? 100.0 * d.Sync / total : 0; - var asyncPct = total > 0 ? 100.0 * d.Async / total : 0; - sb.AppendLine($" sync complete: {d.Sync:N0} ({syncPct:F1}%)"); - sb.AppendLine($" async complete: {d.Async:N0} ({asyncPct:F1}%)"); - WriteDuration(sb, " op duration us", d.OpUs); - WriteDuration(sb, " await duration us", d.AwaitUs); - sb.AppendLine(); - } - - private static void WriteSimple(StringBuilder sb, long ops, long sync, long async, int[] samples) - { - sb.AppendLine($" operations: {ops:N0}"); - var total = sync + async; - var syncPct = total > 0 ? 100.0 * sync / total : 0; - var asyncPct = total > 0 ? 100.0 * async / total : 0; - sb.AppendLine($" sync complete: {sync:N0} ({syncPct:F1}%)"); - sb.AppendLine($" async complete: {async:N0} ({asyncPct:F1}%)"); - WriteDuration(sb, " duration us", samples); - } - - private static void WriteReadShape(StringBuilder sb, in TransportIoReadShape shape, long writeOps) - { - sb.AppendLine("TX Pipe ReadResult shape (non-empty; DIAGNOSTIC)"); - sb.AppendLine($" reads: {shape.Reads:N0}"); - sb.AppendLine($" empty reads: {shape.EmptyReads:N0}"); - sb.AppendLine($" bytes: {shape.Bytes:N0}"); - if (shape.Reads > 0) - { - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" avg bytes/read: {shape.Bytes / (double)shape.Reads:F1}")); - sb.AppendLine($" min bytes/read: {shape.MinBytes:N0}"); - sb.AppendLine($" max bytes/read: {shape.MaxBytes:N0}"); - } - - sb.AppendLine($" segments: {shape.Segments:N0} (non-empty)"); - if (shape.Reads > 0) - { - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" avg segments/read: {shape.Segments / (double)shape.Reads:F2}")); - sb.AppendLine($" min segments/read: {shape.MinSegments:N0}"); - sb.AppendLine($" max segments/read: {shape.MaxSegments:N0}"); - } - - if (shape.Segments > 0) - { - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" avg bytes/segment: {shape.Bytes / (double)shape.Segments:F1}")); - sb.AppendLine($" min bytes/segment: {shape.MinSegmentBytes:N0}"); - sb.AppendLine($" max bytes/segment: {shape.MaxSegmentBytes:N0}"); - } - - if (shape.Reads > 0) - { - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" writes/read: {writeOps / (double)shape.Reads:F2} (stream WriteAsync / non-empty ReadResult)")); - } - - sb.AppendLine($" WriteAsync ops: {writeOps:N0}"); - sb.AppendLine($" segments vs writes: {shape.Segments:N0} segments -> {writeOps:N0} WriteAsync"); - } - - private static void WriteTxLoop(StringBuilder sb, in TransportIoSnapshot snapshot) - { - var loop = snapshot.Loop; - sb.AppendLine("TX SendAsync loop stages (DIAGNOSTIC; NntpConnection.SendAsync)"); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" SendAsync elapsed: {loop.SendAsyncElapsed.TotalMilliseconds:F3} ms (pump lifetime, includes idle)")); - sb.AppendLine($" AdvanceTo calls: {loop.AdvanceCount:N0}"); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" AdvanceTo total: {loop.AdvanceTo.TotalMilliseconds:F3} ms")); - sb.AppendLine($" Transport writes: {loop.TransportWriteCount:N0} (ConnectionByteTransport.WriteAsync)"); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" Transport write: {loop.TransportWrite.TotalMilliseconds:F3} ms")); - sb.AppendLine($" Between-write gaps: {loop.BetweenWriteCount:N0}"); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" Between writes: {loop.BetweenWrites.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" Read→first write: {loop.ReadToFirstWrite.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" Last write→Advance: {loop.LastWriteToAdvance.TotalMilliseconds:F3} ms (includes FlushAsync)")); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" Advance→next Read: {loop.AdvanceToNextRead.TotalMilliseconds:F3} ms")); - WriteDuration(sb, " read→write us", loop.ReadToFirstWriteUs); - WriteDuration(sb, " between write us", loop.BetweenWriteUs); - WriteDuration(sb, " write→advance us", loop.LastWriteToAdvanceUs); - WriteDuration(sb, " AdvanceTo us", loop.AdvanceUs); - WriteDuration(sb, " advance→read us", loop.AdvanceToNextReadUs); - WriteDuration(sb, " transport wr us", loop.TransportWriteUs); - - var readWait = SumUs(snapshot.TxPipeWaitUs); - var streamWrite = SumUs(snapshot.Tx.OpUs); - var accounted = loop.ReadToFirstWrite + loop.BetweenWrites + loop.LastWriteToAdvance + - loop.AdvanceTo + loop.AdvanceToNextRead + loop.TransportWrite + - TimeSpan.FromMilliseconds(readWait / 1000.0); - sb.AppendLine(); - sb.AppendLine(" Accounting (accumulated stage totals; ReadAsync wait uses sampled us sum)"); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" ReadAsync waiting: {readWait / 1000.0:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Pipe → first Write: {loop.ReadToFirstWrite.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Transport WriteAsync: {loop.TransportWrite.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" NetworkStream write: {streamWrite / 1000.0:F3} ms (subset of transport write)")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Between writes: {loop.BetweenWrites.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Last write → Advance: {loop.LastWriteToAdvance.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" AdvanceTo: {loop.AdvanceTo.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Advance → next Read: {loop.AdvanceToNextRead.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Copy (in read→write): {snapshot.Coalesce.CopyTime.TotalMilliseconds:F3} ms (subset of Pipe → first Write / between writes)")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" Accounted stages: {accounted.TotalMilliseconds:F3} ms")); - sb.AppendLine(string.Create(CultureInfo.InvariantCulture, $" SendAsync elapsed: {loop.SendAsyncElapsed.TotalMilliseconds:F3} ms")); - if (loop.SendAsyncElapsed > TimeSpan.Zero) - { - var unaccounted = loop.SendAsyncElapsed - accounted; - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" Other/unaccounted: {unaccounted.TotalMilliseconds:F3} ms (idle between commands is expected)")); - } - } - - private static void WriteWriteCoalesce(StringBuilder sb, in TransportIoWriteCoalesce coalesce) - { - sb.AppendLine("TX write granularity (DIAGNOSTIC; VECTORNNTP_TX_WRITE_GRANULARITY)"); - if (coalesce.TargetBytes <= 0) - { - sb.AppendLine(" target: inactive"); - return; - } - - sb.AppendLine($" target: {coalesce.TargetBytes:N0} bytes"); - sb.AppendLine($" copy operations: {coalesce.CopyCount:N0}"); - sb.AppendLine($" copy bytes: {coalesce.CopyBytes:N0}"); - if (coalesce.CopyCount > 0) - { - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" avg copy bytes: {coalesce.CopyBytes / (double)coalesce.CopyCount:F1}")); - } - - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $" copy time: {coalesce.CopyTime.TotalMilliseconds:F3} ms")); - WriteDuration(sb, " copy us", coalesce.CopyUs); - } - - private static long SumUs(int[] samples) - { - long sum = 0; - for (var i = 0; i < samples.Length; i++) - { - sum += samples[i]; - } - - return sum; - } - - private static void WriteDuration(StringBuilder sb, string label, int[] samples) - { - var s = Stats(samples); - sb.AppendLine(string.Create( - CultureInfo.InvariantCulture, - $"{label,-20} n={samples.Length,-7:N0} mean={s.Mean,10:F1} p50={s.P50,10:F1} p95={s.P95,10:F1} p99={s.P99,10:F1} max={s.Max,10:N0}")); - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoSession.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoSession.cs deleted file mode 100644 index 5dfa8fa9..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TransportIoSession.cs +++ /dev/null @@ -1,680 +0,0 @@ -using System.Buffers; -using System.Diagnostics; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// DIAGNOSTIC-ONLY per-connection aggregate of stream Read/Write/Flush and pipe wait times. - /// - /// - /// No per-operation objects. Samples are stored in preallocated arrays - /// (microseconds). Excess operations update counters only. - /// - internal sealed class TransportIoSession - { - /// Maximum duration samples retained per series. - public const int SampleCapacity = 32768; - - private readonly int[] _rxOpUs = new int[SampleCapacity]; - private readonly int[] _rxAwaitUs = new int[SampleCapacity]; - private readonly int[] _txOpUs = new int[SampleCapacity]; - private readonly int[] _txAwaitUs = new int[SampleCapacity]; - private readonly int[] _rxPipeFlushUs = new int[SampleCapacity]; - private readonly int[] _txPipeWaitUs = new int[SampleCapacity]; - private readonly int[] _flushUs = new int[SampleCapacity]; - - private int _rxOpSamples; - private int _txOpSamples; - private int _rxPipeSamples; - private int _txPipeSamples; - private int _flushSamples; - - private long _rxOps; - private long _txOps; - private long _flushOps; - private long _rxPipeFlushes; - private long _txPipeWaits; - private long _txReadShapes; - private long _txReadEmpty; - private long _txReadBytes; - private long _txReadSegments; - private int _txReadMinBytes = int.MaxValue; - private int _txReadMaxBytes; - private int _txReadMinSegments = int.MaxValue; - private int _txReadMaxSegments; - private int _txReadMinSegBytes = int.MaxValue; - private int _txReadMaxSegBytes; - - private long _rxBytes; - private long _txBytes; - private long _rxRequested; - private long _txRequested; - - private int _rxMinBytes = int.MaxValue; - private int _rxMaxBytes; - private int _txMinBytes = int.MaxValue; - private int _txMaxBytes; - - private long _rxSync; - private long _rxAsync; - private long _txSync; - private long _txAsync; - private long _flushSync; - private long _flushAsync; - private long _rxPipeSync; - private long _rxPipeAsync; - private long _txPipeSync; - private long _txPipeAsync; - - private long _firstRxTs; - private long _lastRxTs; - private long _firstTxTs; - private long _lastTxTs; - private readonly long _createdTs = Stopwatch.GetTimestamp(); - - private readonly int[] _readToFirstWriteUs = new int[SampleCapacity]; - private readonly int[] _betweenWritesUs = new int[SampleCapacity]; - private readonly int[] _lastWriteToAdvanceUs = new int[SampleCapacity]; - private readonly int[] _advanceUs = new int[SampleCapacity]; - private readonly int[] _advanceToNextReadUs = new int[SampleCapacity]; - private readonly int[] _transportWriteUs = new int[SampleCapacity]; - private readonly int[] _copyUs = new int[SampleCapacity]; - - private int _readToFirstWriteSamples; - private int _betweenWriteSamples; - private int _lastWriteToAdvanceSamples; - private int _advanceSamples; - private int _advanceToNextReadSamples; - private int _transportWriteSamples; - private int _copySamples; - - private long _sendLoopStartTs; - private long _sendLoopEndTs; - private long _readToFirstWriteTicks; - private long _betweenWriteTicks; - private long _lastWriteToAdvanceTicks; - private long _advanceTicks; - private long _advanceToNextReadTicks; - private long _transportWriteTicks; - private long _copyTicks; - private long _advanceCount; - private long _betweenWriteCount; - private long _transportWriteCount; - private long _copyCount; - private long _copyBytes; - - /// TX output-pipe pause threshold actually used by this connection. - public long TxPipePauseBytes { get; set; } = NntpPipeOptions.PauseWriterThreshold; - - /// TX output-pipe resume threshold actually used by this connection. - public long TxPipeResumeBytes { get; set; } = NntpPipeOptions.ResumeWriterThreshold; - - /// DIAGNOSTIC write-aggregate target, or 0 when the experiment is inactive. - public int WriteGranularityTargetBytes { get; set; } - - /// Records one stream ReadAsync. - public void RecordReceive(int requested, int received, long startedTs, long awaitStartTs, bool sync) - { - var endTs = Stopwatch.GetTimestamp(); - var opUs = ToUs(startedTs, endTs); - var awaitUs = sync ? 0 : ToUs(awaitStartTs, endTs); - Interlocked.Increment(ref _rxOps); - Interlocked.Add(ref _rxBytes, received); - Interlocked.Add(ref _rxRequested, requested); - UpdateMin(ref _rxMinBytes, received); - UpdateMax(ref _rxMaxBytes, received); - if (sync) - { - Interlocked.Increment(ref _rxSync); - } - else - { - Interlocked.Increment(ref _rxAsync); - } - - StampWindow(ref _firstRxTs, ref _lastRxTs, startedTs, endTs); - var index = Interlocked.Increment(ref _rxOpSamples) - 1; - if ((uint)index < SampleCapacity) - { - _rxOpUs[index] = opUs; - _rxAwaitUs[index] = awaitUs; - } - } - - /// Records one stream WriteAsync. - public void RecordSend(int requested, int sent, long startedTs, long awaitStartTs, bool sync) - { - var endTs = Stopwatch.GetTimestamp(); - var opUs = ToUs(startedTs, endTs); - var awaitUs = sync ? 0 : ToUs(awaitStartTs, endTs); - Interlocked.Increment(ref _txOps); - Interlocked.Add(ref _txBytes, sent); - Interlocked.Add(ref _txRequested, requested); - UpdateMin(ref _txMinBytes, sent); - UpdateMax(ref _txMaxBytes, sent); - if (sync) - { - Interlocked.Increment(ref _txSync); - } - else - { - Interlocked.Increment(ref _txAsync); - } - - StampWindow(ref _firstTxTs, ref _lastTxTs, startedTs, endTs); - var index = Interlocked.Increment(ref _txOpSamples) - 1; - if ((uint)index < SampleCapacity) - { - _txOpUs[index] = opUs; - _txAwaitUs[index] = awaitUs; - } - } - - /// Records one stream FlushAsync (not mixed into send stats). - public void RecordFlush(long startedTs, long awaitStartTs, bool sync) - { - var endTs = Stopwatch.GetTimestamp(); - Interlocked.Increment(ref _flushOps); - if (sync) - { - Interlocked.Increment(ref _flushSync); - } - else - { - Interlocked.Increment(ref _flushAsync); - } - - var index = Interlocked.Increment(ref _flushSamples) - 1; - if ((uint)index < SampleCapacity) - { - _flushUs[index] = ToUs(startedTs, endTs); - _ = awaitStartTs; - } - } - - /// Records one application-input PipeWriter.FlushAsync (RX backpressure). - public void RecordRxPipeFlush(long startedTs, long awaitStartTs, bool sync) - { - var endTs = Stopwatch.GetTimestamp(); - Interlocked.Increment(ref _rxPipeFlushes); - if (sync) - { - Interlocked.Increment(ref _rxPipeSync); - } - else - { - Interlocked.Increment(ref _rxPipeAsync); - } - - var index = Interlocked.Increment(ref _rxPipeSamples) - 1; - if ((uint)index < SampleCapacity) - { - _rxPipeFlushUs[index] = ToUs(startedTs, endTs); - _ = awaitStartTs; - } - } - - /// - /// Records one application-output PipeReader.ReadAsync. - /// Includes waiting for the producer; this is not socket time. - /// - public void RecordTxPipeWait(long startedTs, long awaitStartTs, bool sync) - { - var endTs = Stopwatch.GetTimestamp(); - Interlocked.Increment(ref _txPipeWaits); - if (sync) - { - Interlocked.Increment(ref _txPipeSync); - } - else - { - Interlocked.Increment(ref _txPipeAsync); - } - - var index = Interlocked.Increment(ref _txPipeSamples) - 1; - if ((uint)index < SampleCapacity) - { - _txPipeWaitUs[index] = ToUs(startedTs, endTs); - _ = awaitStartTs; - } - } - - /// - /// Records the shape of one non-empty TX from PipeReader.ReadAsync. - /// Empty buffers are counted separately and do not affect averages. - /// - public void RecordTxReadShape(in ReadOnlySequence buffer) - { - if (buffer.IsEmpty) - { - Interlocked.Increment(ref _txReadEmpty); - return; - } - - var bytesLong = buffer.Length; - var bytes = bytesLong > int.MaxValue ? int.MaxValue : (int)bytesLong; - var segments = 0; - var minSeg = int.MaxValue; - var maxSeg = 0; - foreach (var memory in buffer) - { - var length = memory.Length; - if (length == 0) - { - continue; - } - - segments++; - if (length < minSeg) - { - minSeg = length; - } - - if (length > maxSeg) - { - maxSeg = length; - } - } - - Interlocked.Increment(ref _txReadShapes); - Interlocked.Add(ref _txReadBytes, bytesLong); - Interlocked.Add(ref _txReadSegments, segments); - UpdateMin(ref _txReadMinBytes, bytes); - UpdateMax(ref _txReadMaxBytes, bytes); - UpdateMin(ref _txReadMinSegments, segments); - UpdateMax(ref _txReadMaxSegments, segments); - if (segments > 0) - { - UpdateMin(ref _txReadMinSegBytes, minSeg); - UpdateMax(ref _txReadMaxSegBytes, maxSeg); - } - } - - /// Marks NntpConnection.SendAsync entry (monotonic). - public void RecordSendLoopStart() => - Interlocked.CompareExchange(ref _sendLoopStartTs, Stopwatch.GetTimestamp(), 0); - - /// Marks NntpConnection.SendAsync exit after the read loop. - public void RecordSendLoopEnd() => - Volatile.Write(ref _sendLoopEndTs, Stopwatch.GetTimestamp()); - - /// ReadAsync completion → first transport WriteAsync of that iteration. - public void RecordReadToFirstWrite(long readDoneTs, long firstWriteTs) => - RecordSpan( - readDoneTs, - firstWriteTs, - ref _readToFirstWriteTicks, - ref _readToFirstWriteSamples, - _readToFirstWriteUs); - - /// Gap between consecutive transport WriteAsync calls in one ReadResult. - public void RecordBetweenWrites(long previousWriteEndTs, long nextWriteStartTs) - { - Interlocked.Increment(ref _betweenWriteCount); - RecordSpan( - previousWriteEndTs, - nextWriteStartTs, - ref _betweenWriteTicks, - ref _betweenWriteSamples, - _betweenWritesUs); - } - - /// Last WriteAsync completion → AdvanceTo (includes FlushAsync in the current loop). - public void RecordLastWriteToAdvance(long lastWriteEndTs, long advanceStartTs) => - RecordSpan( - lastWriteEndTs, - advanceStartTs, - ref _lastWriteToAdvanceTicks, - ref _lastWriteToAdvanceSamples, - _lastWriteToAdvanceUs); - - /// Times one PipeReader.AdvanceTo. - public void RecordAdvanceTo(long startedTs, long endedTs) - { - Interlocked.Increment(ref _advanceCount); - RecordSpan(startedTs, endedTs, ref _advanceTicks, ref _advanceSamples, _advanceUs); - } - - /// AdvanceTo return → next ReadAsync issued. - public void RecordAdvanceToNextRead(long advanceEndTs, long nextReadIssuedTs) => - RecordSpan( - advanceEndTs, - nextReadIssuedTs, - ref _advanceToNextReadTicks, - ref _advanceToNextReadSamples, - _advanceToNextReadUs); - - /// Times one ConnectionByteTransport.WriteAsync (admission + stream write). - public void RecordTransportWrite(long startedTs, long endedTs) - { - Interlocked.Increment(ref _transportWriteCount); - RecordSpan(startedTs, endedTs, ref _transportWriteTicks, ref _transportWriteSamples, _transportWriteUs); - } - - /// Times one copy into the diagnostic write-aggregate buffer. - public void RecordCopy(int bytes, long startedTs, long endedTs) - { - if (bytes < 0) - { - return; - } - - Interlocked.Increment(ref _copyCount); - Interlocked.Add(ref _copyBytes, bytes); - RecordSpan(startedTs, endedTs, ref _copyTicks, ref _copySamples, _copyUs); - } - - /// Snapshots counters and duration samples for a report. - public TransportIoSnapshot Snapshot() - { - var now = Stopwatch.GetTimestamp(); - return new TransportIoSnapshot( - ConnectionElapsed: Stopwatch.GetElapsedTime(_createdTs, now), - RxWindow: Window(_firstRxTs, _lastRxTs), - TxWindow: Window(_firstTxTs, _lastTxTs), - Rx: Direction( - Volatile.Read(ref _rxOps), - Volatile.Read(ref _rxBytes), - Volatile.Read(ref _rxRequested), - Volatile.Read(ref _rxMinBytes), - Volatile.Read(ref _rxMaxBytes), - Volatile.Read(ref _rxSync), - Volatile.Read(ref _rxAsync), - CopySamples(_rxOpUs, Volatile.Read(ref _rxOpSamples)), - CopySamples(_rxAwaitUs, Volatile.Read(ref _rxOpSamples))), - Tx: Direction( - Volatile.Read(ref _txOps), - Volatile.Read(ref _txBytes), - Volatile.Read(ref _txRequested), - Volatile.Read(ref _txMinBytes), - Volatile.Read(ref _txMaxBytes), - Volatile.Read(ref _txSync), - Volatile.Read(ref _txAsync), - CopySamples(_txOpUs, Volatile.Read(ref _txOpSamples)), - CopySamples(_txAwaitUs, Volatile.Read(ref _txOpSamples))), - FlushOps: Volatile.Read(ref _flushOps), - FlushSync: Volatile.Read(ref _flushSync), - FlushAsync: Volatile.Read(ref _flushAsync), - FlushUs: CopySamples(_flushUs, Volatile.Read(ref _flushSamples)), - RxPipeFlushes: Volatile.Read(ref _rxPipeFlushes), - RxPipeSync: Volatile.Read(ref _rxPipeSync), - RxPipeAsync: Volatile.Read(ref _rxPipeAsync), - RxPipeFlushUs: CopySamples(_rxPipeFlushUs, Volatile.Read(ref _rxPipeSamples)), - TxPipeWaits: Volatile.Read(ref _txPipeWaits), - TxPipeSync: Volatile.Read(ref _txPipeSync), - TxPipeAsync: Volatile.Read(ref _txPipeAsync), - TxPipeWaitUs: CopySamples(_txPipeWaitUs, Volatile.Read(ref _txPipeSamples)), - TxPipePauseBytes: TxPipePauseBytes, - TxPipeResumeBytes: TxPipeResumeBytes, - TxRead: new TransportIoReadShape( - Volatile.Read(ref _txReadShapes), - Volatile.Read(ref _txReadEmpty), - Volatile.Read(ref _txReadBytes), - Volatile.Read(ref _txReadSegments), - MinOrZero(_txReadMinBytes), - Volatile.Read(ref _txReadMaxBytes), - MinOrZero(_txReadMinSegments), - Volatile.Read(ref _txReadMaxSegments), - MinOrZero(_txReadMinSegBytes), - Volatile.Read(ref _txReadMaxSegBytes)), - Loop: new TransportIoTxLoop( - SendAsyncElapsed: Window( - Volatile.Read(ref _sendLoopStartTs), - EndOrNow(Volatile.Read(ref _sendLoopEndTs), now)), - ReadToFirstWrite: TicksToTimeSpan(Volatile.Read(ref _readToFirstWriteTicks)), - BetweenWrites: TicksToTimeSpan(Volatile.Read(ref _betweenWriteTicks)), - LastWriteToAdvance: TicksToTimeSpan(Volatile.Read(ref _lastWriteToAdvanceTicks)), - AdvanceTo: TicksToTimeSpan(Volatile.Read(ref _advanceTicks)), - AdvanceToNextRead: TicksToTimeSpan(Volatile.Read(ref _advanceToNextReadTicks)), - TransportWrite: TicksToTimeSpan(Volatile.Read(ref _transportWriteTicks)), - AdvanceCount: Volatile.Read(ref _advanceCount), - BetweenWriteCount: Volatile.Read(ref _betweenWriteCount), - TransportWriteCount: Volatile.Read(ref _transportWriteCount), - ReadToFirstWriteUs: CopySamples(_readToFirstWriteUs, Volatile.Read(ref _readToFirstWriteSamples)), - BetweenWriteUs: CopySamples(_betweenWritesUs, Volatile.Read(ref _betweenWriteSamples)), - LastWriteToAdvanceUs: CopySamples(_lastWriteToAdvanceUs, Volatile.Read(ref _lastWriteToAdvanceSamples)), - AdvanceUs: CopySamples(_advanceUs, Volatile.Read(ref _advanceSamples)), - AdvanceToNextReadUs: CopySamples(_advanceToNextReadUs, Volatile.Read(ref _advanceToNextReadSamples)), - TransportWriteUs: CopySamples(_transportWriteUs, Volatile.Read(ref _transportWriteSamples))), - Coalesce: new TransportIoWriteCoalesce( - TargetBytes: WriteGranularityTargetBytes, - CopyCount: Volatile.Read(ref _copyCount), - CopyBytes: Volatile.Read(ref _copyBytes), - CopyTime: TicksToTimeSpan(Volatile.Read(ref _copyTicks)), - CopyUs: CopySamples(_copyUs, Volatile.Read(ref _copySamples)))); - } - - /// Writes the connection dump and returns the path. - public string Write(string directory, string sessionLabel) - { - ArgumentException.ThrowIfNullOrWhiteSpace(directory); - ArgumentException.ThrowIfNullOrWhiteSpace(sessionLabel); - Directory.CreateDirectory(directory); - var stamp = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss", System.Globalization.CultureInfo.InvariantCulture); - var safe = Sanitize(sessionLabel); - var path = Path.Join(directory, $"{stamp}-{safe}.txt"); - File.WriteAllText(path, TransportIoReport.Format(Snapshot(), sessionLabel), System.Text.Encoding.UTF8); - return path; - } - - private static TransportIoDirection Direction( - long ops, - long bytes, - long requested, - int minBytes, - int maxBytes, - long sync, - long async, - int[] opUs, - int[] awaitUs) => - new( - ops, - bytes, - requested, - MinOrZero(minBytes), - maxBytes, - sync, - async, - opUs, - awaitUs); - - private static int MinOrZero(int value) => value == int.MaxValue ? 0 : value; - - private static void RecordSpan(long start, long end, ref long ticks, ref int samples, int[] us) - { - if (start <= 0 || end < start) - { - return; - } - - Interlocked.Add(ref ticks, end - start); - var index = Interlocked.Increment(ref samples) - 1; - if ((uint)index < SampleCapacity) - { - us[index] = ToUs(start, end); - } - } - - private static TimeSpan TicksToTimeSpan(long ticks) => - ticks <= 0 ? TimeSpan.Zero : TimeSpan.FromSeconds(ticks / (double)Stopwatch.Frequency); - - private static long EndOrNow(long end, long now) => end > 0 ? end : now; - - private static TimeSpan Window(long first, long last) => - first > 0 && last >= first ? Stopwatch.GetElapsedTime(first, last) : TimeSpan.Zero; - - private static void StampWindow(ref long first, ref long last, long start, long end) - { - if (Interlocked.CompareExchange(ref first, start, 0) == 0) - { - // first write won - } - - long current; - do - { - current = Volatile.Read(ref last); - if (end <= current) - { - return; - } - } - while (Interlocked.CompareExchange(ref last, end, current) != current); - } - - private static int[] CopySamples(int[] source, int written) - { - var count = Math.Min(Math.Max(written, 0), SampleCapacity); - if (count == 0) - { - return []; - } - - var copy = new int[count]; - Array.Copy(source, copy, count); - return copy; - } - - private static void UpdateMin(ref int field, int value) - { - var current = Volatile.Read(ref field); - while (value < current) - { - var original = Interlocked.CompareExchange(ref field, value, current); - if (original == current) - { - return; - } - - current = original; - } - } - - private static void UpdateMax(ref int field, int value) - { - var current = Volatile.Read(ref field); - while (value > current) - { - var original = Interlocked.CompareExchange(ref field, value, current); - if (original == current) - { - return; - } - - current = original; - } - } - - private static int ToUs(long start, long end) - { - if (start <= 0 || end < start) - { - return 0; - } - - var us = Stopwatch.GetElapsedTime(start, end).TotalMicroseconds; - if (us >= int.MaxValue) - { - return int.MaxValue; - } - - if (us <= 0) - { - return 0; - } - - return (int)us; - } - - private static string Sanitize(string value) - { - var chars = value.ToCharArray(); - for (var i = 0; i < chars.Length; i++) - { - if (!char.IsAsciiLetterOrDigit(chars[i]) && chars[i] is not '-' and not '_') - { - chars[i] = '_'; - } - } - - return new string(chars); - } - } - - /// Immutable counters and duration samples for one connection. - internal readonly record struct TransportIoSnapshot( - TimeSpan ConnectionElapsed, - TimeSpan RxWindow, - TimeSpan TxWindow, - TransportIoDirection Rx, - TransportIoDirection Tx, - long FlushOps, - long FlushSync, - long FlushAsync, - int[] FlushUs, - long RxPipeFlushes, - long RxPipeSync, - long RxPipeAsync, - int[] RxPipeFlushUs, - long TxPipeWaits, - long TxPipeSync, - long TxPipeAsync, - int[] TxPipeWaitUs, - long TxPipePauseBytes, - long TxPipeResumeBytes, - TransportIoReadShape TxRead, - TransportIoTxLoop Loop, - TransportIoWriteCoalesce Coalesce); - - /// DIAGNOSTIC write-granularity copy counters (when the experiment is active). - internal readonly record struct TransportIoWriteCoalesce( - int TargetBytes, - long CopyCount, - long CopyBytes, - TimeSpan CopyTime, - int[] CopyUs); - - /// Accumulated NntpConnection.SendAsync loop-stage timings (when probe enabled). - internal readonly record struct TransportIoTxLoop( - TimeSpan SendAsyncElapsed, - TimeSpan ReadToFirstWrite, - TimeSpan BetweenWrites, - TimeSpan LastWriteToAdvance, - TimeSpan AdvanceTo, - TimeSpan AdvanceToNextRead, - TimeSpan TransportWrite, - long AdvanceCount, - long BetweenWriteCount, - long TransportWriteCount, - int[] ReadToFirstWriteUs, - int[] BetweenWriteUs, - int[] LastWriteToAdvanceUs, - int[] AdvanceUs, - int[] AdvanceToNextReadUs, - int[] TransportWriteUs); - - /// Shape of TX PipeReader.ReadAsync buffers (non-empty reads). - internal readonly record struct TransportIoReadShape( - long Reads, - long EmptyReads, - long Bytes, - long Segments, - int MinBytes, - int MaxBytes, - int MinSegments, - int MaxSegments, - int MinSegmentBytes, - int MaxSegmentBytes); - - /// One direction of stream I/O (RX read or TX write). - internal readonly record struct TransportIoDirection( - long Ops, - long Bytes, - long Requested, - int MinBytes, - int MaxBytes, - long Sync, - long Async, - int[] OpUs, - int[] AwaitUs); -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TransportLogMessages.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TransportLogMessages.cs deleted file mode 100644 index f1a8c679..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TransportLogMessages.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Net; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// Source-generated NNTP connection transport log messages. - internal static partial class TransportLogMessages - { - [LoggerMessage( - EventId = 1500, - Level = LogLevel.Debug, - Message = "TLS handshake completed for {Remote}.")] - public static partial void TlsHandshakeCompleted(ILogger logger, EndPoint? Remote); - - [LoggerMessage( - EventId = 1501, - Level = LogLevel.Debug, - Message = "In-place TLS upgrade completed for {Remote}.")] - public static partial void InPlaceTlsUpgradeCompleted(ILogger logger, EndPoint? Remote); - - [LoggerMessage( - EventId = 1502, - Level = LogLevel.Debug, - Message = "In-place DEFLATE compression activated for {Remote}.")] - public static partial void InPlaceDeflateActivated(ILogger logger, EndPoint? Remote); - - [LoggerMessage( - EventId = 1503, - Level = LogLevel.Debug, - Message = "NNTP connection {Pump} pump ended with an error")] - public static partial void PumpEndedWithError(ILogger logger, Exception exception, string Pump); - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TxPipePauseExperiment.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TxPipePauseExperiment.cs deleted file mode 100644 index 0b46c3cf..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TxPipePauseExperiment.cs +++ /dev/null @@ -1,207 +0,0 @@ -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// DIAGNOSTIC-ONLY. Off-by-default TX output-pipe pause-threshold override. - /// - /// - /// - /// Production default remains (64 KiB). - /// This type never changes the input (RX) pipe. It exists only so a SPEEDTEST sweep can - /// vary how far the TX producer may run ahead before FlushAsync backpressures. - /// - /// - /// Activate only with VECTORNNTP_TX_PIPE_PAUSE set to an allowlisted size - /// (64KiB … 8MiB), or via in tests. Unset / empty / 0 - /// / false leave production options unchanged. Any other value is rejected so a typo - /// cannot silently apply a large buffer. - /// - /// - internal static class TxPipePauseExperiment - { - /// Environment variable that selects an experimental TX pipe pause size. - public const string EnvironmentVariableName = "VECTORNNTP_TX_PIPE_PAUSE"; - - /// Allowlisted pause sizes (bytes). 64 KiB is the production control. - public static readonly long[] AllowedPauseBytes = - [ - 64 * 1024, - 128 * 1024, - 256 * 1024, - 512 * 1024, - 1024 * 1024, - 2 * 1024 * 1024, - 4 * 1024 * 1024, - 8 * 1024 * 1024, - ]; - - private static OverrideState? _state; - - static TxPipePauseExperiment() - { - var value = Environment.GetEnvironmentVariable(EnvironmentVariableName); - if (string.IsNullOrWhiteSpace(value) || - value.Equals("0", StringComparison.OrdinalIgnoreCase) || - value.Equals("false", StringComparison.OrdinalIgnoreCase)) - { - return; - } - - if (!TryParseAllowed(value, out var pause)) - { - throw new InvalidOperationException( - EnvironmentVariableName + - " must be an allowlisted size (64KiB, 128KiB, 256KiB, 512KiB, 1MiB, 2MiB, 4MiB, 8MiB) or the equivalent byte count."); - } - - Volatile.Write(ref _state, new OverrideState(pause)); - } - - /// Gets whether an experimental pause size is configured. - public static bool IsConfigured => Volatile.Read(ref _state) is not null; - - /// Gets the configured pause bytes, or when inactive. - public static long? ConfiguredPauseBytes => Volatile.Read(ref _state)?.PauseBytes; - - /// Test/experiment hook. must be allowlisted. - public static void Set(long pauseBytes) - { - if (!IsAllowed(pauseBytes)) - { - throw new ArgumentOutOfRangeException( - nameof(pauseBytes), - pauseBytes, - "TX pipe pause experiment accepts only 64KiB…8MiB allowlisted sizes."); - } - - Volatile.Write(ref _state, new OverrideState(pauseBytes)); - } - - /// Clears the override (tests). Production default applies. - public static void Clear() => Volatile.Write(ref _state, null); - - /// - /// Resolves the TX output-pipe thresholds. When inactive, returns production 64 KiB / 32 KiB - /// and . - /// - public static bool TryGetEffective(out long pauseBytes, out long resumeBytes) - { - var configured = Volatile.Read(ref _state); - if (configured is null) - { - pauseBytes = NntpPipeOptions.PauseWriterThreshold; - resumeBytes = NntpPipeOptions.ResumeWriterThreshold; - return false; - } - - pauseBytes = configured.PauseBytes; - resumeBytes = ResumeFor(configured.PauseBytes); - return true; - } - - /// Resume is half of pause, never below the production 32 KiB resume. - public static long ResumeFor(long pauseBytes) - { - var half = pauseBytes / 2; - return half < NntpPipeOptions.ResumeWriterThreshold - ? NntpPipeOptions.ResumeWriterThreshold - : half; - } - - /// Parses an allowlisted size token or byte count. - public static bool TryParseAllowed(string value, out long pauseBytes) - { - pauseBytes = 0; - if (string.IsNullOrWhiteSpace(value)) - { - return false; - } - - var trimmed = value.Trim(); - if (long.TryParse(trimmed, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var raw) && - IsAllowed(raw)) - { - pauseBytes = raw; - return true; - } - - if (TryParseSizeToken(trimmed, out var parsed) && IsAllowed(parsed)) - { - pauseBytes = parsed; - return true; - } - - return false; - } - - /// Returns whether is on the experiment allowlist. - public static bool IsAllowed(long pauseBytes) - { - for (var i = 0; i < AllowedPauseBytes.Length; i++) - { - if (AllowedPauseBytes[i] == pauseBytes) - { - return true; - } - } - - return false; - } - - private static bool TryParseSizeToken(string value, out long bytes) - { - bytes = 0; - var span = value.AsSpan().Trim(); - var suffixLen = 0; - long multiplier; - if (span.EndsWith("KiB", StringComparison.OrdinalIgnoreCase) || - span.EndsWith("kib", StringComparison.OrdinalIgnoreCase)) - { - suffixLen = 3; - multiplier = 1024; - } - else if (span.EndsWith("MiB", StringComparison.OrdinalIgnoreCase) || - span.EndsWith("mib", StringComparison.OrdinalIgnoreCase)) - { - suffixLen = 3; - multiplier = 1024 * 1024; - } - else if (span.Length > 0 && (span[^1] is 'k' or 'K')) - { - suffixLen = 1; - multiplier = 1024; - } - else if (span.Length > 0 && (span[^1] is 'm' or 'M')) - { - suffixLen = 1; - multiplier = 1024 * 1024; - } - else - { - return false; - } - - var number = span[..^suffixLen]; - if (!long.TryParse(number, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var count) || - count <= 0) - { - return false; - } - - try - { - bytes = checked(count * multiplier); - } - catch (OverflowException) - { - return false; - } - - return true; - } - - private sealed class OverrideState(long pauseBytes) - { - public long PauseBytes { get; } = pauseBytes; - } - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TxWriteCoalescer.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TxWriteCoalescer.cs deleted file mode 100644 index 81930bf5..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TxWriteCoalescer.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System.Diagnostics; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// DIAGNOSTIC-ONLY reusable contiguous buffer for experimental TX write aggregation. - /// - /// - /// One buffer is allocated for the connection lifetime. Copies are measured when a - /// is supplied. The buffer remains valid until - /// after the transport write has completed. - /// - internal sealed class TxWriteCoalescer - { - private readonly byte[] _buffer; - private int _count; - - /// Creates a coalescer whose buffer is exactly . - public TxWriteCoalescer(int targetBytes) - { - if (!TxWriteGranularityExperiment.IsAllowed(targetBytes)) - { - throw new ArgumentOutOfRangeException(nameof(targetBytes), targetBytes, "Target must be an allowlisted write-granularity size."); - } - - TargetBytes = targetBytes; - _buffer = GC.AllocateUninitializedArray(targetBytes); - } - - /// Configured aggregate size in bytes. - public int TargetBytes { get; } - - /// Bytes currently held in the reusable buffer. - public int PendingCount => _count; - - /// Unused bytes before the next transport write must be issued. - public int RemainingCapacity => TargetBytes - _count; - - /// Gets whether the buffer is full and must be written before more copies. - public bool IsFull => _count >= TargetBytes; - - /// Pending bytes. Valid until . - public ReadOnlyMemory PendingMemory => _buffer.AsMemory(0, _count); - - /// - /// Copies as many bytes as fit from . Returns the number copied. - /// - public int Copy(ReadOnlySpan source, TransportIoSession? io) - { - var take = Math.Min(source.Length, RemainingCapacity); - if (take == 0) - { - return 0; - } - - var started = io is null ? 0L : Stopwatch.GetTimestamp(); - source[..take].CopyTo(_buffer.AsSpan(_count, take)); - _count += take; - if (io is not null) - { - io.RecordCopy(take, started, Stopwatch.GetTimestamp()); - } - - return take; - } - - /// Drops pending bytes after a successful transport write (or a terminal failure). - public void Clear() => _count = 0; - } -} diff --git a/src/VectorNNTPD.TransitService/Networking/Transport/TxWriteGranularityExperiment.cs b/src/VectorNNTPD.TransitService/Networking/Transport/TxWriteGranularityExperiment.cs deleted file mode 100644 index 94cdb3f2..00000000 --- a/src/VectorNNTPD.TransitService/Networking/Transport/TxWriteGranularityExperiment.cs +++ /dev/null @@ -1,234 +0,0 @@ -using System.Runtime.CompilerServices; - -namespace VectorNNTPD.TransitService.Networking.Transport -{ - /// - /// DIAGNOSTIC-ONLY. Off-by-default TX write-granularity override. - /// - /// - /// - /// Production default remains one ConnectionByteTransport.WriteAsync per Pipe - /// segment. This type never changes Pipe thresholds, schedulers, Channel, TCS, - /// NetworkStream, or socket options. It exists only so a SPEEDTEST sweep can - /// copy consecutive Pipe segments into one larger transport write. - /// - /// - /// Activate only with VECTORNNTP_TX_WRITE_GRANULARITY set to an allowlisted - /// size (64KiB … 4MiB), or via / - /// in tests. Unset / empty / 0 / false leave the production path - /// unchanged. Any other value is rejected so a typo cannot silently apply a large - /// aggregate. - /// - /// - internal static class TxWriteGranularityExperiment - { - /// Environment variable that selects an experimental TX write aggregate size. - public const string EnvironmentVariableName = "VECTORNNTP_TX_WRITE_GRANULARITY"; - - /// Allowlisted aggregate sizes (bytes). 64 KiB is the instrumented control. - public static readonly int[] AllowedTargetBytes = - [ - 64 * 1024, - 128 * 1024, - 256 * 1024, - 512 * 1024, - 1024 * 1024, - 4 * 1024 * 1024, - ]; - - private static readonly ConditionalWeakTable> PerTransport = []; - - private static OverrideState? _state; - - static TxWriteGranularityExperiment() - { - var value = Environment.GetEnvironmentVariable(EnvironmentVariableName); - if (string.IsNullOrWhiteSpace(value) || - value.Equals("0", StringComparison.OrdinalIgnoreCase) || - value.Equals("false", StringComparison.OrdinalIgnoreCase)) - { - return; - } - - if (!TryParseAllowed(value, out var target)) - { - throw new InvalidOperationException( - EnvironmentVariableName + - " must be an allowlisted size (64KiB, 128KiB, 256KiB, 512KiB, 1MiB, 4MiB) or the equivalent byte count."); - } - - Volatile.Write(ref _state, new OverrideState(target)); - } - - /// Gets whether a process-wide experimental aggregate size is configured. - public static bool IsConfigured => Volatile.Read(ref _state) is not null; - - /// Gets the process-wide target bytes, or when inactive. - public static int? ConfiguredTargetBytes => Volatile.Read(ref _state)?.TargetBytes; - - /// Test/experiment hook. must be allowlisted. - public static void Set(int targetBytes) - { - if (!IsAllowed(targetBytes)) - { - throw new ArgumentOutOfRangeException( - nameof(targetBytes), - targetBytes, - "TX write-granularity experiment accepts only 64KiB…4MiB allowlisted sizes."); - } - - Volatile.Write(ref _state, new OverrideState(targetBytes)); - } - - /// Clears the process-wide override (tests). Production default applies. - public static void Clear() => Volatile.Write(ref _state, null); - - /// - /// Test-only per-transport override. Does not affect other connections. - /// - public static void SetFor(ConnectionByteTransport transport, int targetBytes) - { - ArgumentNullException.ThrowIfNull(transport); - if (!IsAllowed(targetBytes)) - { - throw new ArgumentOutOfRangeException( - nameof(targetBytes), - targetBytes, - "TX write-granularity experiment accepts only 64KiB…4MiB allowlisted sizes."); - } - - PerTransport.AddOrUpdate(transport, new StrongBox(targetBytes)); - } - - /// Removes a per-transport override (tests). - public static void ClearFor(ConnectionByteTransport transport) - { - ArgumentNullException.ThrowIfNull(transport); - PerTransport.Remove(transport); - } - - /// - /// Resolves the aggregate target for this send pump. Per-transport overrides win - /// over the process-wide setting. Returns when inactive. - /// - public static bool TryResolveTarget(ConnectionByteTransport? transport, out int targetBytes) - { - if (transport is not null && PerTransport.TryGetValue(transport, out var box)) - { - targetBytes = box.Value; - return true; - } - - return TryGetTarget(out targetBytes); - } - - /// Gets the process-wide target, or when inactive. - public static bool TryGetTarget(out int targetBytes) - { - var configured = Volatile.Read(ref _state); - if (configured is null) - { - targetBytes = 0; - return false; - } - - targetBytes = configured.TargetBytes; - return true; - } - - /// Parses an allowlisted size token or byte count. - public static bool TryParseAllowed(string value, out int targetBytes) - { - targetBytes = 0; - if (string.IsNullOrWhiteSpace(value)) - { - return false; - } - - var trimmed = value.Trim(); - if (int.TryParse(trimmed, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var raw) && - IsAllowed(raw)) - { - targetBytes = raw; - return true; - } - - if (TryParseSizeToken(trimmed, out var parsed) && IsAllowed(parsed)) - { - targetBytes = parsed; - return true; - } - - return false; - } - - /// Returns whether is on the experiment allowlist. - public static bool IsAllowed(int targetBytes) - { - for (var i = 0; i < AllowedTargetBytes.Length; i++) - { - if (AllowedTargetBytes[i] == targetBytes) - { - return true; - } - } - - return false; - } - - private static bool TryParseSizeToken(string value, out int bytes) - { - bytes = 0; - var span = value.AsSpan().Trim(); - var suffixLen = 0; - int multiplier; - if (span.EndsWith("KiB", StringComparison.OrdinalIgnoreCase)) - { - suffixLen = 3; - multiplier = 1024; - } - else if (span.EndsWith("MiB", StringComparison.OrdinalIgnoreCase)) - { - suffixLen = 3; - multiplier = 1024 * 1024; - } - else if (span.Length > 0 && (span[^1] is 'k' or 'K')) - { - suffixLen = 1; - multiplier = 1024; - } - else if (span.Length > 0 && (span[^1] is 'm' or 'M')) - { - suffixLen = 1; - multiplier = 1024 * 1024; - } - else - { - return false; - } - - var number = span[..^suffixLen]; - if (!int.TryParse(number, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var count) || - count <= 0) - { - return false; - } - - try - { - bytes = checked(count * multiplier); - } - catch (OverflowException) - { - return false; - } - - return true; - } - - private sealed class OverrideState(int targetBytes) - { - public int TargetBytes { get; } = targetBytes; - } - } -} diff --git a/src/VectorNNTPD.TransitService/PostFilter/IPostFilter.cs b/src/VectorNNTPD.TransitService/PostFilter/IPostFilter.cs index 379ceea6..213ea071 100644 --- a/src/VectorNNTPD.TransitService/PostFilter/IPostFilter.cs +++ b/src/VectorNNTPD.TransitService/PostFilter/IPostFilter.cs @@ -1,7 +1,7 @@ using VectorNNTPD.Common.Articles; using VectorNNTPD.TransitService.Authentication; using VectorNNTPD.TransitService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.PostFilter { /// NNTPD POST-only accept filter. Does not own CreateQueued / TryAdmit / History. diff --git a/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandContext.cs b/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandContext.cs index 39c7570f..70033586 100644 --- a/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandContext.cs +++ b/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandContext.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session.Framing; namespace VectorNNTPD.TransitService.Session.CommandProcessor @@ -62,6 +62,13 @@ public NntpCommandContext( /// public string? CompletionDetail { get; set; } + /// + /// When , the handler expects the connection to become completed + /// as a normal outcome (for example QUIT). must not + /// treat that completion as [failed]. Thrown exceptions still log as failed. + /// + public bool ExpectConnectionComplete { get; set; } + /// Gets the underlying transport connection. public INntpConnection Connection => Session.Connection; diff --git a/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandExecution.cs b/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandExecution.cs index 4a5ded7b..0d8d6e01 100644 --- a/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandExecution.cs +++ b/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpCommandExecution.cs @@ -41,8 +41,13 @@ public static async ValueTask RunAsync( // Handler-owned detail (e.g. QUIT peer disconnect, STARTTLS cipher, COMPRESS). detail = context.CompletionDetail; } - else if (!completedBefore && context.Connection.IsCompleted) + else if (!completedBefore + && context.Connection.IsCompleted + && !context.ExpectConnectionComplete) { + // Connection completed during the handler without a handler-owned detail and + // without an explicit expectation (QUIT sets ExpectConnectionComplete). + // STARTTLS handshake abort after 382 still surfaces as failed. detail = "failed"; } } diff --git a/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpPeerDisconnect.cs b/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpPeerDisconnect.cs index c5182d9e..00937de4 100644 --- a/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpPeerDisconnect.cs +++ b/src/VectorNNTPD.TransitService/Session/CommandProcessor/NntpPeerDisconnect.cs @@ -1,5 +1,5 @@ using System.Net.Sockets; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Session.CommandProcessor { diff --git a/src/VectorNNTPD.TransitService/Session/Commands/Posting/PostHeaderNormalizer.cs b/src/VectorNNTPD.TransitService/Session/Commands/Posting/PostHeaderNormalizer.cs index e5256338..ad082b5a 100644 --- a/src/VectorNNTPD.TransitService/Session/Commands/Posting/PostHeaderNormalizer.cs +++ b/src/VectorNNTPD.TransitService/Session/Commands/Posting/PostHeaderNormalizer.cs @@ -2,7 +2,7 @@ using System.Net; using System.Text; using VectorNNTPD.TransitService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Session.Commands.Posting { /// Replaces server-owned POST headers and emits one destuffed article buffer. diff --git a/src/VectorNNTPD.TransitService/Session/Commands/Posting/StreamingPostReadResult.cs b/src/VectorNNTPD.TransitService/Session/Commands/Posting/StreamingPostReadResult.cs index 2d0c0a9c..a252d16a 100644 --- a/src/VectorNNTPD.TransitService/Session/Commands/Posting/StreamingPostReadResult.cs +++ b/src/VectorNNTPD.TransitService/Session/Commands/Posting/StreamingPostReadResult.cs @@ -1,6 +1,6 @@ using VectorNNTPD.TransitService.Moderation; using VectorNNTPD.TransitService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Session.Commands.Posting { /// What the POST command should do with a completed receive. diff --git a/src/VectorNNTPD.TransitService/Session/Commands/Quit.cs b/src/VectorNNTPD.TransitService/Session/Commands/Quit.cs index c766a9c0..e5cae598 100644 --- a/src/VectorNNTPD.TransitService/Session/Commands/Quit.cs +++ b/src/VectorNNTPD.TransitService/Session/Commands/Quit.cs @@ -30,6 +30,7 @@ private static async ValueTask ExecuteAsync(NntpCommandContext context, Cancella { // Valid QUIT accepted: after this point the session must become terminal and no further // NNTP commands/responses should be processed (aside from finishing this 205 write). + context.ExpectConnectionComplete = true; var responseWritten = false; var idleVersionBefore205 = context.Connection.OutboundIdleVersion; try @@ -88,6 +89,13 @@ await context.Connection { context.CompletionDetail = "peer disconnected"; } + + // StatusLine is normally noted only when Debug command logging is enabled; ensure TX + // completion can still show 205 after a successful write. + if (responseWritten) + { + context.StatusLine ??= NntpResponseStatus.ConnectionClosing; + } } } } diff --git a/src/VectorNNTPD.TransitService/Session/Commands/StartTls.cs b/src/VectorNNTPD.TransitService/Session/Commands/StartTls.cs index 46a70518..8d91882e 100644 --- a/src/VectorNNTPD.TransitService/Session/Commands/StartTls.cs +++ b/src/VectorNNTPD.TransitService/Session/Commands/StartTls.cs @@ -1,8 +1,8 @@ using System.IO.Pipelines; using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Session.Commands { /// diff --git a/src/VectorNNTPD.TransitService/Session/NntpSession.cs b/src/VectorNNTPD.TransitService/Session/NntpSession.cs index 9d406fcf..277e47fe 100644 --- a/src/VectorNNTPD.TransitService/Session/NntpSession.cs +++ b/src/VectorNNTPD.TransitService/Session/NntpSession.cs @@ -8,9 +8,9 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.SessionState.BytesAccounting; -using VectorNNTPD.TransitService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.TransitService.Authentication; using VectorNNTPD.TransitService.SessionState; using VectorNNTPD.TransitService.Session.Authentication; @@ -25,7 +25,8 @@ using VectorNNTPD.TransitService.RabbitMq.ArticleWork; using VectorNNTPD.TransitService.Storage; using VectorNNTPD.TransitService.Transport.Vatp; - +using VectorNNTPD.TransitService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Session { using VectorNNTPD.Common.Core; diff --git a/src/VectorNNTPD.TransitService/SessionState/DistributedSessionStateTracker.cs b/src/VectorNNTPD.TransitService/SessionState/DistributedSessionStateTracker.cs index ec513214..59b00c5b 100644 --- a/src/VectorNNTPD.TransitService/SessionState/DistributedSessionStateTracker.cs +++ b/src/VectorNNTPD.TransitService/SessionState/DistributedSessionStateTracker.cs @@ -1,8 +1,8 @@ using System.Collections.Concurrent; using System.Net; using VectorNNTPD.TransitService.SessionState.BytesAccounting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.TransitService.SessionState.RateLimiting; - namespace VectorNNTPD.TransitService.SessionState { /// diff --git a/src/VectorNNTPD.TransitService/SessionState/InMemorySessionStateTracker.cs b/src/VectorNNTPD.TransitService/SessionState/InMemorySessionStateTracker.cs index 3277e7a8..88411a8f 100644 --- a/src/VectorNNTPD.TransitService/SessionState/InMemorySessionStateTracker.cs +++ b/src/VectorNNTPD.TransitService/SessionState/InMemorySessionStateTracker.cs @@ -1,6 +1,6 @@ using System.Net; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.TransitService.SessionState.RateLimiting; - namespace VectorNNTPD.TransitService.SessionState { /// diff --git a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/AccountRateAllocator.cs b/src/VectorNNTPD.TransitService/SessionState/RateLimiting/AccountRateAllocator.cs index b82032e8..bfde333e 100644 --- a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/AccountRateAllocator.cs +++ b/src/VectorNNTPD.TransitService/SessionState/RateLimiting/AccountRateAllocator.cs @@ -1,5 +1,7 @@ using System.Collections.Concurrent; +using VectorNNTPD.Common.Networking.RateLimiting; + namespace VectorNNTPD.TransitService.SessionState.RateLimiting { /// diff --git a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IAccountRateAllocator.cs b/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IAccountRateAllocator.cs index 5c24a6d4..ff8c6a04 100644 --- a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IAccountRateAllocator.cs +++ b/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IAccountRateAllocator.cs @@ -1,3 +1,5 @@ +using VectorNNTPD.Common.Networking.RateLimiting; + namespace VectorNNTPD.TransitService.SessionState.RateLimiting { /// diff --git a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IOutboundRateCap.cs b/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IOutboundRateCap.cs deleted file mode 100644 index dcb8ad25..00000000 --- a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/IOutboundRateCap.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace VectorNNTPD.TransitService.SessionState.RateLimiting -{ - /// Dynamically adjustable outbound bytes-per-second cap. - public interface IOutboundRateCap - { - /// - /// Gets the current cap. 0 is unlimited. < 0 blocks writes - /// until a later update (not unlimited). - /// - long MaxSendBytesPerSecond { get; } - - /// Replaces the cap. Existing and future writes observe the new value. - void UpdateMaxSendBytesPerSecond(long bytesPerSecond); - } -} diff --git a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/OutboundRateLimiter.cs b/src/VectorNNTPD.TransitService/SessionState/RateLimiting/OutboundRateLimiter.cs deleted file mode 100644 index 1898d375..00000000 --- a/src/VectorNNTPD.TransitService/SessionState/RateLimiting/OutboundRateLimiter.cs +++ /dev/null @@ -1,199 +0,0 @@ -namespace VectorNNTPD.TransitService.SessionState.RateLimiting -{ - /// - /// Stream wrapper that throttles writes to a dynamically adjustable bytes/sec cap. - /// Reads, flushes, and cap == 0 are passthrough. cap < 0 blocks - /// until the cap is updated. - /// - /// - /// Adapted from Vector.NNTP DynamicSendRateLimitedStream: one-second write window, - /// 25 ms backoff, live during writes. - /// Writes are serialized. Cancellation aborts the backoff. Dispose does not wait - /// for an in-flight write; the write token must be cancelled by the connection. - /// - public sealed class OutboundRateLimiter : Stream, IOutboundRateCap - { - private readonly Stream _inner; - private readonly bool _leaveInnerOpen; - private readonly TimeProvider _time; - private readonly SemaphoreSlim _writeGate = new(1, 1); - private long _maxSendBytesPerSecond; - private long _windowStartMs; - private long _bytesInWindow; - private int _disposed; - - /// Initializes a limiter over . - public OutboundRateLimiter( - Stream inner, - long initialMaxSendBytesPerSecond, - bool leaveInnerOpen, - TimeProvider? timeProvider = null) - { - ArgumentNullException.ThrowIfNull(inner); - _inner = inner; - _leaveInnerOpen = leaveInnerOpen; - _time = timeProvider ?? TimeProvider.System; - _maxSendBytesPerSecond = initialMaxSendBytesPerSecond; - _windowStartMs = NowMs(); - } - - /// - public override bool CanRead => _inner.CanRead; - - /// - public override bool CanSeek => false; - - /// - public override bool CanWrite => _inner.CanWrite; - - /// - public override long Length => throw new NotSupportedException(); - - /// - public override long Position - { - get => throw new NotSupportedException(); - set => throw new NotSupportedException(); - } - - /// - public long MaxSendBytesPerSecond => Interlocked.Read(ref _maxSendBytesPerSecond); - - /// - public void UpdateMaxSendBytesPerSecond(long bytesPerSecond) => - Interlocked.Exchange(ref _maxSendBytesPerSecond, bytesPerSecond); - - /// - public override void Flush() => _inner.Flush(); - - /// - public override Task FlushAsync(CancellationToken cancellationToken) => - _inner.FlushAsync(cancellationToken); - - /// - public override int Read(byte[] buffer, int offset, int count) => - throw new NotSupportedException(); - - /// - public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) => - _inner.ReadAsync(buffer, cancellationToken); - - /// - public override void Write(byte[] buffer, int offset, int count) => - throw new NotSupportedException(); - - /// - public override async ValueTask WriteAsync( - ReadOnlyMemory buffer, - CancellationToken cancellationToken = default) - { - ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); - var cap = Interlocked.Read(ref _maxSendBytesPerSecond); - if (buffer.IsEmpty || cap == 0) - { - await _inner.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); - return; - } - - await _writeGate.WaitAsync(cancellationToken).ConfigureAwait(false); - try - { - ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); - var offset = 0; - while (offset < buffer.Length) - { - cap = Interlocked.Read(ref _maxSendBytesPerSecond); - if (cap == 0) - { - await _inner.WriteAsync(buffer[offset..], cancellationToken).ConfigureAwait(false); - return; - } - - if (cap < 0) - { - await Task.Delay(TimeSpan.FromMilliseconds(25), _time, cancellationToken) - .ConfigureAwait(false); - continue; - } - - var sliceBytes = buffer.Length - offset; - if (sliceBytes > cap) - { - sliceBytes = cap > int.MaxValue ? int.MaxValue : (int)cap; - } - - await ThrottleAsync(sliceBytes, cap, cancellationToken).ConfigureAwait(false); - ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); - await _inner.WriteAsync(buffer.Slice(offset, sliceBytes), cancellationToken) - .ConfigureAwait(false); - _bytesInWindow += sliceBytes; - offset += sliceBytes; - } - } - finally - { - _writeGate.Release(); - } - } - - /// - public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); - - /// - public override void SetLength(long value) => throw new NotSupportedException(); - - /// - protected override void Dispose(bool disposing) - { - if (disposing && Interlocked.Exchange(ref _disposed, 1) == 0) - { - _writeGate.Dispose(); - if (!_leaveInnerOpen) - { - _inner.Dispose(); - } - } - - base.Dispose(disposing); - } - - /// - public override async ValueTask DisposeAsync() - { - if (Interlocked.Exchange(ref _disposed, 1) == 0) - { - _writeGate.Dispose(); - if (!_leaveInnerOpen) - { - await _inner.DisposeAsync().ConfigureAwait(false); - } - } - - await base.DisposeAsync().ConfigureAwait(false); - } - - private async Task ThrottleAsync(int nextBytes, long cap, CancellationToken cancellationToken) - { - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) == 1, this); - var now = NowMs(); - if (now - _windowStartMs >= 1000) - { - _windowStartMs = now; - _bytesInWindow = 0; - } - - if (_bytesInWindow + nextBytes <= cap) - { - return; - } - - await Task.Delay(TimeSpan.FromMilliseconds(25), _time, cancellationToken).ConfigureAwait(false); - } - } - - private long NowMs() => _time.GetUtcNow().ToUnixTimeMilliseconds(); - } -} diff --git a/src/VectorNNTPD.TransitService/Transit/TransitConnectionAdmission.cs b/src/VectorNNTPD.TransitService/Transit/TransitConnectionAdmission.cs index b72edde4..5f3f23a4 100644 --- a/src/VectorNNTPD.TransitService/Transit/TransitConnectionAdmission.cs +++ b/src/VectorNNTPD.TransitService/Transit/TransitConnectionAdmission.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.CommandProcessor; diff --git a/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs b/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs new file mode 100644 index 00000000..70d68e57 --- /dev/null +++ b/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs @@ -0,0 +1,288 @@ +using System.Buffers; +using System.IO.Pipelines; +using System.Net; +using VectorNNTPD.Common.Networking.Certificates; +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.Transport; + +namespace VectorNNTPD.Common.Tests.Networking.Transport +{ + /// + /// Focused regressions for pipe ownership and cancellation. + /// + public sealed class NntpConnectionStreamTests + { + /// + /// must still pair with AdvanceTo so a later + /// read can observe newly written bytes. + /// + [Fact] + public async Task ReadAsync_CancelPendingRead_advances_and_subsequent_read_works() + { + await using var duplex = new PipeDuplex(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + duplex.InboundReader.CancelPendingRead(); + var cancelBuffer = new byte[4]; +#pragma warning disable CA2022 // Exception path: read is expected to throw before filling the buffer. + await Assert.ThrowsAnyAsync( + async () => await stream.ReadAsync(cancelBuffer)); +#pragma warning restore CA2022 + + await duplex.Inbound.WriteAsync(new byte[] { 9 }); + await duplex.Inbound.FlushAsync(); + var buffer = new byte[1]; + Assert.Equal(1, await stream.ReadAsync(buffer.AsMemory())); + Assert.Equal(9, buffer[0]); + } + + /// + /// Cancellation while data is buffered must not consume those bytes; the next read returns them. + /// + [Fact] + public async Task ReadAsync_cancel_with_buffered_data_does_not_skip_bytes() + { + await using var duplex = new PipeDuplex(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + await duplex.Inbound.WriteAsync(new byte[] { 1, 2, 3, 4 }); + await duplex.Inbound.FlushAsync(); + duplex.InboundReader.CancelPendingRead(); + + var cancelBuffer = new byte[4]; +#pragma warning disable CA2022 // Exception path: read is expected to throw before filling the buffer. + await Assert.ThrowsAnyAsync( + async () => await stream.ReadAsync(cancelBuffer)); +#pragma warning restore CA2022 + + var buffer = new byte[4]; + var offset = 0; + while (offset < buffer.Length) + { + var read = await stream.ReadAsync(buffer.AsMemory(offset)); + Assert.True(read > 0); + offset += read; + } + + Assert.Equal(new byte[] { 1, 2, 3, 4 }, buffer); + } + + /// Peer-complete (empty completed read) returns 0 and stays at EOF. + [Fact] + public async Task ReadAsync_completed_empty_pipe_returns_zero() + { + await using var duplex = new PipeDuplex(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + await duplex.Inbound.CompleteAsync(); + Assert.Equal(0, await stream.ReadAsync(new byte[8])); + Assert.Equal(0, await stream.ReadAsync(new byte[8])); + } + + /// Partial consume leaves remainder for the next read. + [Fact] + public async Task ReadAsync_partial_consume_preserves_remainder() + { + await using var duplex = new PipeDuplex(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + await duplex.Inbound.WriteAsync(new byte[] { 10, 20, 30 }); + await duplex.Inbound.FlushAsync(); + + var first = new byte[2]; + Assert.Equal(2, await stream.ReadAsync(first)); + Assert.Equal(new byte[] { 10, 20 }, first); + + var second = new byte[2]; + Assert.Equal(1, await stream.ReadAsync(second)); + Assert.Equal(30, second[0]); + } + + /// Write into a completed output pipe fails without hanging. + [Fact] + public async Task WriteAsync_completed_output_throws_IOException() + { + await using var duplex = new PipeDuplex(); + await duplex.OutboundReader.CompleteAsync(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + await Assert.ThrowsAsync( + async () => await stream.WriteAsync(new byte[] { 1, 2, 3 })); + } + + /// Large write chunks and is fully readable from the outbound pipe. + [Fact] + public async Task WriteAsync_large_payload_is_fully_flushed() + { + await using var duplex = new PipeDuplex(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + var payload = new byte[200 * 1024]; + Random.Shared.NextBytes(payload); + var readTask = ReadExactAsync(duplex.OutboundReader, payload.Length); + + await stream.WriteAsync(payload); + + var got = await readTask.WaitAsync(TimeSpan.FromSeconds(5)); + Assert.Equal(payload, got); + } + + /// Pre-cancelled write token fails without hanging the caller. + [Fact] + public async Task WriteAsync_pre_cancelled_token_throws() + { + await using var duplex = new PipeDuplex(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + using var cts = new CancellationTokenSource(); + cts.Cancel(); + await Assert.ThrowsAnyAsync( + async () => await stream.WriteAsync(new byte[] { 1, 2, 3 }, cts.Token)); + } + + /// + /// After a chunk is Advanced, completing the outbound reader fails the in-flight + /// flush, but already-committed bytes remain — the adapter cannot roll them back. + /// + [Fact] + public async Task WriteAsync_failure_after_advance_leaves_committed_bytes_readable() + { + var options = new PipeOptions( + pauseWriterThreshold: 64, + resumeWriterThreshold: 32, + useSynchronizationContext: false); + await using var duplex = new PipeDuplex(options); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + var payload = new byte[256]; + payload.AsSpan().Fill(0xAB); + var writeTask = stream.WriteAsync(payload).AsTask(); + + // First Advance is visible; examine without consuming so FlushAsync stays paused. + var first = await duplex.OutboundReader.ReadAsync().AsTask() + .WaitAsync(TimeSpan.FromSeconds(5)); + Assert.False(first.Buffer.IsEmpty); + var committed = (int)first.Buffer.Length; + var committedBytes = new byte[committed]; + first.Buffer.Slice(0, committed).CopyTo(committedBytes); + duplex.OutboundReader.AdvanceTo(first.Buffer.Start, first.Buffer.End); + + await duplex.OutboundReader.CompleteAsync(); + + await Assert.ThrowsAsync( + () => writeTask.WaitAsync(TimeSpan.FromSeconds(5))); + + Assert.True(committed > 0); + Assert.All(committedBytes, b => Assert.Equal((byte)0xAB, b)); + } + + private static async Task ReadExactAsync(PipeReader reader, int count) + { + var buffer = new byte[count]; + var offset = 0; + while (offset < count) + { + var result = await reader.ReadAsync(); + var sequence = result.Buffer; + var toCopy = (int)Math.Min(sequence.Length, count - offset); + sequence.Slice(0, toCopy).CopyTo(buffer.AsMemory(offset).Span); + offset += toCopy; + reader.AdvanceTo(sequence.GetPosition(toCopy)); + if (toCopy == 0 && result.IsCompleted) + { + throw new EndOfStreamException(); + } + } + + return buffer; + } + + private sealed class PipeDuplex : INntpConnection + { + private readonly Pipe _inbound; + private readonly Pipe _outbound; + private readonly CancellationTokenSource _cts = new(); + private int _completed; + + public PipeDuplex(PipeOptions? options = null) + { + options ??= new PipeOptions(useSynchronizationContext: false); + _inbound = new Pipe(options); + _outbound = new Pipe(options); + ClientIdentity = ConnectionClientIdentity.Direct(new IPEndPoint(IPAddress.Loopback, 0)); + } + + public PipeWriter Inbound => _inbound.Writer; + + public PipeReader InboundReader => _inbound.Reader; + + public PipeReader OutboundReader => _outbound.Reader; + + public PipeReader Input => _inbound.Reader; + + public PipeWriter Output => _outbound.Writer; + + public EndPoint? RemoteEndPoint => ClientIdentity.TcpPeer; + + public EndPoint? LocalEndPoint => null; + + public ConnectionClientIdentity ClientIdentity { get; } + + public bool IsTls => false; + + public bool IsCompressed => false; + + public CancellationToken ConnectionClosed => _cts.Token; + + public bool IsCompleted => Volatile.Read(ref _completed) == 1; + + public long OutboundIdleVersion => 0; + + public bool TryGetNegotiatedTlsParameters(out string tlsVersion, out string cipher) + { + tlsVersion = string.Empty; + cipher = string.Empty; + return false; + } + + public Task PauseReadsAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; + + public Task WaitForOutboundDeliveryAsync( + long outboundIdleVersionBeforeFlush, + CancellationToken cancellationToken = default) => + Task.CompletedTask; + + public Task WaitForOutboundDeliveryAndPauseReadsAsync( + long outboundIdleVersionBeforeFlush, + CancellationToken cancellationToken = default) => + Task.CompletedTask; + + public Task CompleteAsync(Exception? exception = null) + { + if (Interlocked.Exchange(ref _completed, 1) == 0) + { + _cts.Cancel(); + } + + return Task.CompletedTask; + } + + public Task UpgradeToTlsAsync( + ITlsCertificateContextProvider certificateProvider, + CancellationToken cancellationToken = default) => + throw new NotSupportedException(); + + public Task UpgradeToDeflateAsync(CancellationToken cancellationToken = default) => + throw new NotSupportedException(); + + public async ValueTask DisposeAsync() + { + await _inbound.Writer.CompleteAsync(); + await _inbound.Reader.CompleteAsync(); + await _outbound.Writer.CompleteAsync(); + await _outbound.Reader.CompleteAsync(); + _cts.Dispose(); + } + } + } +} diff --git a/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs b/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs new file mode 100644 index 00000000..aac927ac --- /dev/null +++ b/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs @@ -0,0 +1,221 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; +using Microsoft.Extensions.Logging.Abstractions; +using VectorNNTPD.Common.Networking.Listeners; +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.Transport; + +namespace VectorNNTPD.Common.Tests.Networking.Transport +{ + /// + /// Exercises the shared + pipeline path. + /// + public sealed class SocketAcceptListenerAndPipelineTransportTests + { + /// Accepts one client, reads payload via pipes/stream adapter, and echoes an acknowledgement. + [Fact] + public async Task Plain_accept_delivers_bytes_through_pipeline_and_stream_adapter() + { + var payload = Encoding.ASCII.GetBytes("hello-pipeline\r\n"); + var received = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + await using var listener = new SocketAcceptListener( + new ListenBinding(IPAddress.Loopback, Port: 0, DualMode: false), + async (socket, cancellationToken) => + { + var peer = (IPEndPoint)socket.RemoteEndPoint!; + await using var connection = NntpConnection.StartPlain( + socket, + ConnectionClientIdentity.Direct(peer), + NullLogger.Instance); + await using var stream = new NntpConnectionStream(connection, ownsConnection: false); + var buffer = new byte[64]; + var read = await stream.ReadAsync(buffer.AsMemory(), cancellationToken).ConfigureAwait(false); + received.TrySetResult(buffer.AsSpan(0, read).ToArray()); + + var echo = Encoding.ASCII.GetBytes("ack\r\n"); + await stream.WriteAsync(echo, cancellationToken).ConfigureAwait(false); + }, + NullLogger.Instance); + + listener.Start(); + var endpoint = listener.LocalEndPoint; + + using var client = new TcpClient(); + await client.ConnectAsync(endpoint, CancellationToken.None); + await using var clientStream = client.GetStream(); + await clientStream.WriteAsync(payload); + + var got = await received.Task.WaitAsync(TimeSpan.FromSeconds(5)); + Assert.Equal(payload, got); + + var response = new byte[16]; + var responseCount = await clientStream.ReadAsync(response.AsMemory()); + Assert.Equal("ack\r\n", Encoding.ASCII.GetString(response.AsSpan(0, responseCount))); + } + + /// Admits several concurrent clients on one listener without serializing accepts. + [Fact] + public async Task Accept_loop_supports_concurrent_clients() + { + const int clients = 8; + var admitted = 0; + var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + await using var listener = new SocketAcceptListener( + new ListenBinding(IPAddress.Loopback, Port: 0, DualMode: false), + async (socket, cancellationToken) => + { + Interlocked.Increment(ref admitted); + var peer = (IPEndPoint)socket.RemoteEndPoint!; + await using var connection = NntpConnection.StartPlain( + socket, + ConnectionClientIdentity.Direct(peer), + NullLogger.Instance); + await using var stream = new NntpConnectionStream(connection, ownsConnection: false); + await gate.Task.WaitAsync(cancellationToken).ConfigureAwait(false); + var buffer = new byte[1]; + _ = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); + }, + NullLogger.Instance); + + listener.Start(); + var endpoint = listener.LocalEndPoint; + + var sockets = new List(); + try + { + for (var i = 0; i < clients; i++) + { + var client = new TcpClient(); + await client.ConnectAsync(endpoint, CancellationToken.None); + sockets.Add(client); + } + + await WaitUntilAsync(() => Volatile.Read(ref admitted) == clients, TimeSpan.FromSeconds(5)); + gate.TrySetResult(); + + foreach (var client in sockets) + { + await client.GetStream().WriteAsync(new byte[] { 0x2E }); + } + } + finally + { + foreach (var client in sockets) + { + client.Dispose(); + } + } + } + + /// Stop completes the accept loop without leaving it running. + [Fact] + public async Task Cancellation_during_accept_stops_loop() + { + await using var listener = new SocketAcceptListener( + new ListenBinding(IPAddress.Loopback, Port: 0, DualMode: false), + static (_, _) => ValueTask.CompletedTask, + NullLogger.Instance); + + listener.Start(); + Assert.True(listener.AcceptLoopActive); + await listener.StopAsync(); + Assert.False(listener.AcceptLoopActive); + Assert.True(listener.AcceptLoopTask!.IsCompletedSuccessfully); + } + + /// Large payload traverses the pipe stream adapter without truncating or hanging. + [Fact] + public async Task Large_payload_roundtrip_through_pipeline_stream() + { + var payload = new byte[256 * 1024]; + Random.Shared.NextBytes(payload); + var received = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + await using var listener = new SocketAcceptListener( + new ListenBinding(IPAddress.Loopback, Port: 0, DualMode: false), + async (socket, cancellationToken) => + { + var peer = (IPEndPoint)socket.RemoteEndPoint!; + await using var connection = NntpConnection.StartPlain( + socket, + ConnectionClientIdentity.Direct(peer), + NullLogger.Instance); + await using var stream = new NntpConnectionStream(connection, ownsConnection: false); + var buffer = new byte[payload.Length]; + var offset = 0; + while (offset < buffer.Length) + { + var read = await stream.ReadAsync(buffer.AsMemory(offset), cancellationToken) + .ConfigureAwait(false); + if (read == 0) + { + break; + } + + offset += read; + } + + received.TrySetResult(buffer.AsSpan(0, offset).ToArray()); + await stream.WriteAsync(payload, cancellationToken).ConfigureAwait(false); + }, + NullLogger.Instance); + + listener.Start(); + using var client = new TcpClient(); + await client.ConnectAsync(listener.LocalEndPoint, CancellationToken.None); + await using var clientStream = client.GetStream(); + await clientStream.WriteAsync(payload); + + var got = await received.Task.WaitAsync(TimeSpan.FromSeconds(10)); + Assert.Equal(payload, got); + + var echo = new byte[payload.Length]; + var echoOffset = 0; + while (echoOffset < echo.Length) + { + var read = await clientStream.ReadAsync(echo.AsMemory(echoOffset)); + Assert.True(read > 0); + echoOffset += read; + } + + Assert.Equal(payload, echo); + } + + /// Exclusive bind rejects a second listener on the same endpoint. + [Fact] + public async Task Exclusive_bind_rejects_second_listener_on_same_port() + { + await using var owner = new SocketAcceptListener( + new ListenBinding(IPAddress.Loopback, Port: 0, DualMode: false), + static (_, _) => ValueTask.CompletedTask, + NullLogger.Instance, + reuseAddress: false); + owner.Start(); + var port = owner.LocalEndPoint.Port; + + await using var intruder = new SocketAcceptListener( + new ListenBinding(IPAddress.Loopback, port, DualMode: false), + static (_, _) => ValueTask.CompletedTask, + NullLogger.Instance, + reuseAddress: false); + Assert.Throws(() => intruder.Start()); + } + + private static async Task WaitUntilAsync(Func condition, TimeSpan timeout) + { + var start = Environment.TickCount64; + while (!condition()) + { + if (Environment.TickCount64 - start > timeout.TotalMilliseconds) + { + throw new TimeoutException("Condition was not met in time."); + } + + await Task.Delay(10); + } + } + } +} diff --git a/tests/VectorNNTPD.ReaderService.Tests/AccountBytes/AccountByteExhaustionTests.cs b/tests/VectorNNTPD.ReaderService.Tests/AccountBytes/AccountByteExhaustionTests.cs index fbb47840..eaf3acf5 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/AccountBytes/AccountByteExhaustionTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/AccountBytes/AccountByteExhaustionTests.cs @@ -4,16 +4,17 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.SessionState; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.AccountBytes; public sealed class AccountByteExhaustionTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs index 52fe73fe..b507d3e0 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs @@ -5,7 +5,7 @@ using VectorNNTPD.ReaderService.ArticleIngestion; using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.ArticleIngestion; public sealed class ArticleRecordIngressTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs index 436ff814..e8ff8236 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs @@ -9,7 +9,7 @@ using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.Session.Framing; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.ArticleIngestion; public sealed class IhaveArticleInterpreterTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs index 93e5c511..5c5044fc 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs @@ -10,7 +10,7 @@ using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.ArticleIngestion; /// Post-queue news disposition and INN news writer contracts. diff --git a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs index a95eabb9..19feeec2 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs @@ -10,7 +10,7 @@ using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.ArticleIngestion; /// Post-queue Path-survey capture from canonical ArticleRecord.Path. diff --git a/tests/VectorNNTPD.ReaderService.Tests/Authentication/AccountPolicyMatrixTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Authentication/AccountPolicyMatrixTests.cs index a449f007..8bd3b3f7 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Authentication/AccountPolicyMatrixTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Authentication/AccountPolicyMatrixTests.cs @@ -4,9 +4,9 @@ using VectorNNTPD.ReaderService.NntpDb; using VectorNNTPD.ReaderService.SessionState; using VectorNNTPD.ReaderService.SessionState.BytesAccounting; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Tests.Authentication; public sealed class AccountPolicyMatrixTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Authentication/AuthenticatedSessionTeardownTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Authentication/AuthenticatedSessionTeardownTests.cs index 124398d1..5b144eee 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Authentication/AuthenticatedSessionTeardownTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Authentication/AuthenticatedSessionTeardownTests.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.SessionState; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Authentication/NntpReaderAuthenticationTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Authentication/NntpReaderAuthenticationTests.cs index d92fe8c1..bfc8a0a8 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Authentication/NntpReaderAuthenticationTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Authentication/NntpReaderAuthenticationTests.cs @@ -13,7 +13,7 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.NntpDb; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; @@ -21,7 +21,7 @@ using VectorNNTPD.ReaderService.Session.Framing; using VectorNNTPD.ReaderService.Tests.Session; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Authentication; [Collection(nameof(NntpCommandLoggerCollection))] diff --git a/tests/VectorNNTPD.ReaderService.Tests/Fixtures/CanonicalArticleText.cs b/tests/VectorNNTPD.ReaderService.Tests/Fixtures/CanonicalArticleText.cs index aa178b2a..13dd33d9 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Fixtures/CanonicalArticleText.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Fixtures/CanonicalArticleText.cs @@ -3,7 +3,7 @@ using VectorNNTPD.Common.Articles.Parsing; using VectorNNTPD.ReaderService.ArticleIngestion; using VectorNNTPD.ReaderService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Fixtures; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs index 72756e62..9df7f190 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs @@ -5,7 +5,7 @@ using VectorNNTPD.ReaderService.Moderation; using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.Tests.Email; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Moderation; public sealed class EmailModerationSubmissionServiceTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs index 01f78ac7..e36184ff 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs @@ -10,12 +10,13 @@ using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Listeners; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.ReaderService.Tests.Networking.Listeners; /// Connection-acceptance log format contract (TCP peer vs PROXY effective client; TLS params). diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Proxy/ProxyProtocolTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Proxy/ProxyProtocolTests.cs index 71bcbe2c..f054c930 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Proxy/ProxyProtocolTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Proxy/ProxyProtocolTests.cs @@ -9,12 +9,12 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Tests.Networking.Transport; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Networking.Proxy; public sealed class TrustedProxyHostsTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs index 43957973..8d2bc1f9 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs @@ -9,7 +9,7 @@ public sealed class ConnectionByteTransportQuiescenceTests public async Task AfterPauseReads_TryCommitReadToApplication_StashesClientHello() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -28,7 +28,7 @@ public async Task Quiesce_DuringHistoricalAdmitWindow_DoesNotStartStreamIoAfterI // Reproduces the pre-fix window: WaitUntilActive observed Active, then Quiesce ran and // declared idle before outstanding++. With atomic admission, stream I/O must not start. await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -56,7 +56,7 @@ public async Task Quiesce_DuringHistoricalAdmitWindow_DoesNotStartStreamIoAfterI public async Task Quiesce_WaitsForAdmittedRead_UntilStreamReadReturns() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -102,7 +102,7 @@ public async Task Quiesce_WaitsForAdmittedRead_UntilStreamReadReturns() public async Task Quiesce_WaitsForAdmittedWrite_UntilStreamWriteReturns() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -134,7 +134,7 @@ public async Task Quiesce_WaitsForAdmittedWrite_UntilStreamWriteReturns() public async Task QuiescedIdle_SimulatedHandshake_CannotOverlapPlaintextStreamIo() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -173,7 +173,7 @@ public async Task Quiesce_RepeatedAdmitRace_NeverStartsIoAfterIdle() for (var iteration = 0; iteration < 64; iteration++) { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -200,7 +200,7 @@ public async Task Quiesce_RepeatedAdmitRace_NeverStartsIoAfterIdle() public async Task QuiescedIdle_WriteAdmitRace_NeverStartsWriteAfterIdle() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.ReaderService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs index dbf54979..e1e4bb8c 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs @@ -4,7 +4,7 @@ using System.Security.Authentication; using System.Text; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs index d72ce685..757e2946 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; using System.Net.Sockets; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs index 3a68cc56..49cc696c 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs @@ -1,7 +1,7 @@ using System.Net.Sockets; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Tests.Fixtures; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs index b50c25e6..45ac1a3e 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpDeflateStreamTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpDeflateStreamTests.cs index 9a32a20e..c6d1f0c8 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpDeflateStreamTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpDeflateStreamTests.cs @@ -73,8 +73,8 @@ public async Task RoundTrip_LargeStreaming() public async Task Flush_MakesCompressedBytesVisibleWithoutDispose() { await using var peer = await ConnectedStreamPair.CreateAsync(); - await using var server = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(peer.Server); - await using var client = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(peer.Client); + await using var server = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Server); + await using var client = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Client); var payload = "flush-boundary-test-payload"u8.ToArray(); await server.WriteAsync(payload); @@ -97,7 +97,7 @@ public async Task Flush_MakesCompressedBytesVisibleWithoutDispose() public async Task MalformedCompressedInput_ThrowsWithoutSilentCorruption() { await using var peer = await ConnectedStreamPair.CreateAsync(); - await using var server = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(peer.Server); + await using var server = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Server); // Write non-DEFLATE junk on the wire (peer.Client is the raw NetworkStream). await peer.Client.WriteAsync(new byte[] { 0x00, 0x01, 0x02, 0xFF, 0xFE }); await peer.Client.FlushAsync(); @@ -114,10 +114,10 @@ public async Task IndependentStreams_DoNotShareDictionaryState() { await using var a = await ConnectedStreamPair.CreateAsync(); await using var b = await ConnectedStreamPair.CreateAsync(); - await using var aServer = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(a.Server); - await using var aClient = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(a.Client); - await using var bServer = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(b.Server); - await using var bClient = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(b.Client); + await using var aServer = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(a.Server); + await using var aClient = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(a.Client); + await using var bServer = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(b.Server); + await using var bClient = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(b.Client); await aServer.WriteAsync("connection-A-payload"u8.ToArray()); await aServer.FlushAsync(); @@ -135,8 +135,8 @@ public async Task IndependentStreams_DoNotShareDictionaryState() private static async Task AssertRoundTripAsync(byte[] plain, int writeChunk = 0, int readChunk = 0) { await using var peer = await ConnectedStreamPair.CreateAsync(); - await using var server = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(peer.Server); - await using var client = new VectorNNTPD.ReaderService.Networking.Transport.NntpDeflateStream(peer.Client); + await using var server = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Server); + await using var client = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Client); if (writeChunk <= 0) { diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpTransportTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpTransportTests.cs index 3a769163..6c54d542 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpTransportTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/NntpTransportTests.cs @@ -14,7 +14,7 @@ using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Listeners; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Tests.Acme; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SendPipeAdvancementTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SendPipeAdvancementTests.cs index 034fd5c9..d31f09e5 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SendPipeAdvancementTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SendPipeAdvancementTests.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; using System.Net.Sockets; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SocketPayloadSenderTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SocketPayloadSenderTests.cs index f50da116..bb4c07e3 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SocketPayloadSenderTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/SocketPayloadSenderTests.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs index 62c3bc34..b446eb95 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs @@ -3,10 +3,10 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking; using VectorNNTPD.ReaderService.Networking.Listeners; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs index 72c7c82b..9f1d8138 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs @@ -4,8 +4,8 @@ using System.Security.Authentication; using System.Text; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; - +using VectorNNTPD.Common.Networking.Transport; +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; public sealed class TlsUpgradeTransportTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TransportIoSessionTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TransportIoSessionTests.cs index 3ff61938..fe5502bb 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TransportIoSessionTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TransportIoSessionTests.cs @@ -1,6 +1,6 @@ using System.Buffers; using System.IO.Pipelines; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs index 612c075c..95a43160 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs @@ -1,5 +1,5 @@ using System.IO.Pipelines; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteCoalescerTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteCoalescerTests.cs index 2197cc2e..b2446262 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteCoalescerTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteCoalescerTests.cs @@ -1,5 +1,5 @@ using System.Buffers; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs index 8f995743..96c24ff1 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; using System.Net.Sockets; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs index 286cde33..a20a3dba 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterEvaluatorTests.cs b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterEvaluatorTests.cs index 8461c4f2..e60e8e74 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterEvaluatorTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterEvaluatorTests.cs @@ -6,7 +6,7 @@ using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.PostFilter.Quota; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.PostFilter; public sealed class PostFilterEvaluatorTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterLiveClusterQuotaTests.cs b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterLiveClusterQuotaTests.cs index 487eb957..edd4ac31 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterLiveClusterQuotaTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterLiveClusterQuotaTests.cs @@ -3,7 +3,7 @@ using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.PostFilter.Quota; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.PostFilter; /// @@ -115,7 +115,7 @@ public static PostFilterRequest Create(string account) return new PostFilterRequest( article, account, - VectorNNTPD.ReaderService.Networking.Proxy.ConnectionClientIdentity.Direct( + VectorNNTPD.Common.Networking.Proxy.ConnectionClientIdentity.Direct( new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 119)), accountPolicy: null, Now, diff --git a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostCommandTests.cs index d35bf6fc..25622db4 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostCommandTests.cs @@ -9,7 +9,7 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.PostFilter.Quota; using VectorNNTPD.ReaderService.Session; @@ -18,7 +18,7 @@ using VectorNNTPD.ReaderService.Session.Commands.Posting; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.PostFilter; public sealed class PostFilterPostCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostHarness.cs b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostHarness.cs index ba4924e6..c034e950 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostHarness.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterPostHarness.cs @@ -8,7 +8,7 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.PostFilter.Quota; using VectorNNTPD.ReaderService.Session; @@ -16,7 +16,7 @@ using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Session.Commands.Posting; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.PostFilter; /// Shared POST-boundary harness for PostFilter policy-matrix tests. diff --git a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterSpamAssassinEligibilityTests.cs b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterSpamAssassinEligibilityTests.cs index 27306dbb..66723f37 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterSpamAssassinEligibilityTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/PostFilter/PostFilterSpamAssassinEligibilityTests.cs @@ -5,7 +5,7 @@ using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.PostFilter.Quota; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.PostFilter; public sealed class PostFilterSpamAssassinEligibilityTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalCommandTests.cs index 34d2ea02..96d26416 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalCommandTests.cs @@ -8,7 +8,7 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.RabbitMq.ArticleWork; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; @@ -16,7 +16,7 @@ using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.RabbitMq; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalVatpCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalVatpCommandTests.cs index 5d25aeed..0dcec1d3 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalVatpCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/ArticleRetrievalVatpCommandTests.cs @@ -13,7 +13,7 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.History; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.RabbitMq; using VectorNNTPD.ReaderService.RabbitMq.ArticleWork; using VectorNNTPD.ReaderService.Session; @@ -24,7 +24,7 @@ using VectorNNTPD.ReaderService.Tests.RabbitMq; using VectorNNTPD.ReaderService.Tests.TestDoubles; using VectorNNTPD.ReaderService.Transport.Vatp; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/BenchItCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/BenchItCommandTests.cs index e492998e..dfedea9e 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/BenchItCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/BenchItCommandTests.cs @@ -4,11 +4,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// Internal BENCHIT benchmark facility contracts (not a production protocol feature). diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/Commands/NntpPhaseCResponseTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/Commands/NntpPhaseCResponseTests.cs index ea581a71..a4a40541 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/Commands/NntpPhaseCResponseTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/Commands/NntpPhaseCResponseTests.cs @@ -7,13 +7,13 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.Session; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session.Commands; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/GroupCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/GroupCommandTests.cs index 721a6c01..ffe898c6 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/GroupCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/GroupCommandTests.cs @@ -6,13 +6,13 @@ using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.NntpDb; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; using VectorNNTPD.Common.Configuration; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class GroupCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/HelpCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/HelpCommandTests.cs index 0a5d3d48..d9cb5114 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/HelpCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/HelpCommandTests.cs @@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// HELP command — static syntax listing (RFC 3977 §7.2) with ABNF-consistent notation. diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/IHaveCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/IHaveCommandTests.cs index 31fcf553..9407101b 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/IHaveCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/IHaveCommandTests.cs @@ -8,7 +8,7 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; @@ -17,7 +17,7 @@ using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class IHaveCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/ListCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/ListCommandTests.cs index 5c05149f..f8632eb6 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/ListCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/ListCommandTests.cs @@ -4,11 +4,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class ListCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/ModeratedPostCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/ModeratedPostCommandTests.cs index 6b9b94e3..2cc5be28 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/ModeratedPostCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/ModeratedPostCommandTests.cs @@ -9,7 +9,7 @@ using VectorNNTPD.ReaderService.Moderation; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.Session; @@ -20,7 +20,7 @@ using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.Moderation; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class ModeratedPostCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs index 744d6b5d..e2499c48 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs @@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpAuthinfoTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpAuthinfoTests.cs index 61ba636a..2efffcf6 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpAuthinfoTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpAuthinfoTests.cs @@ -6,12 +6,12 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; [Collection(nameof(NntpCommandLoggerCollection))] diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandInventoryTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandInventoryTests.cs index c585bc93..84a3d277 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandInventoryTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandInventoryTests.cs @@ -2,11 +2,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class NntpCommandInventoryTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLineLengthTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLineLengthTests.cs index 1bf853fe..696cb43d 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLineLengthTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLineLengthTests.cs @@ -1,5 +1,5 @@ using System.IO.Pipelines; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLoggingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLoggingTests.cs index a14c81c5..426082dc 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLoggingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandLoggingTests.cs @@ -13,14 +13,14 @@ using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Tests.TestDoubles; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// Command RX/TX logging contract (ownership, redaction, timing shape). diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandParserTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandParserTests.cs index 1d8a1474..35d5b49b 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandParserTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandParserTests.cs @@ -6,11 +6,11 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandTxStatusLoggingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandTxStatusLoggingTests.cs index 57d60267..435d9be8 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandTxStatusLoggingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCommandTxStatusLoggingTests.cs @@ -8,11 +8,11 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCompressTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCompressTests.cs index 9a6ce766..3cbbfda8 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCompressTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpCompressTests.cs @@ -8,13 +8,13 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// Session-level COMPRESS DEFLATE (RFC 8054) regressions over the real transport stack. @@ -683,7 +683,7 @@ public PipeConnection( Output = output; _pauseReadsFails = pauseReadsFails; _upgradeFails = upgradeFails; - ClientIdentity = VectorNNTPD.ReaderService.Networking.Proxy.ConnectionClientIdentity.Direct( + ClientIdentity = VectorNNTPD.Common.Networking.Proxy.ConnectionClientIdentity.Direct( new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 119)); } @@ -691,7 +691,7 @@ public PipeConnection( public System.IO.Pipelines.PipeWriter Output { get; } public System.Net.EndPoint? RemoteEndPoint => ClientIdentity.TcpPeer; public System.Net.EndPoint? LocalEndPoint => null; - public VectorNNTPD.ReaderService.Networking.Proxy.ConnectionClientIdentity ClientIdentity { get; } + public VectorNNTPD.Common.Networking.Proxy.ConnectionClientIdentity ClientIdentity { get; } public bool IsTls => false; public bool TryGetNegotiatedTlsParameters(out string tlsVersion, out string cipher) diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionFoundationTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionFoundationTests.cs index acdf425b..249d6ad3 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionFoundationTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionFoundationTests.cs @@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class NntpSessionFoundationTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionIdleTimeoutTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionIdleTimeoutTests.cs index 6fe22cac..43297665 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionIdleTimeoutTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionIdleTimeoutTests.cs @@ -7,13 +7,13 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Session.Framing; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs index 6f46e84a..1d1d1fe3 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs @@ -2,7 +2,7 @@ using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpStartTlsTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpStartTlsTests.cs index d30192b7..9ae17243 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/NntpStartTlsTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/NntpStartTlsTests.cs @@ -3,7 +3,7 @@ using System.Security.Authentication; using System.Text; using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/PostArticleValidationTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/PostArticleValidationTests.cs index 53ea9141..f657b969 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/PostArticleValidationTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/PostArticleValidationTests.cs @@ -4,7 +4,7 @@ using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.Session.Commands.Posting; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class PostArticleValidationTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/PostCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/PostCommandTests.cs index 35a078ca..6e2a5bf8 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/PostCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/PostCommandTests.cs @@ -9,7 +9,7 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.PostFilter; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; @@ -17,7 +17,7 @@ using VectorNNTPD.ReaderService.Session.Commands.Posting; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class PostCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/PostNewsgroupCatalogueTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/PostNewsgroupCatalogueTests.cs index e534fdff..a1fe591d 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/PostNewsgroupCatalogueTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/PostNewsgroupCatalogueTests.cs @@ -7,14 +7,14 @@ using VectorNNTPD.ReaderService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Newsgroups; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Session.Commands.Posting; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class PostNewsgroupCatalogueTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/QuitCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/QuitCommandTests.cs index 6aa2a884..ff31f525 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/QuitCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/QuitCommandTests.cs @@ -5,12 +5,12 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; /// QUIT command — RFC 3977 §5.4 and peer-disappears-during-205 race. @@ -196,9 +196,9 @@ public async Task Quit_Socket_ImmediateClientCloseAfterQuit_IsNotApplicationErro recording.Messages, m => m.Contains("TX: QUIT", StringComparison.Ordinal) && m.Contains("executed in", StringComparison.Ordinal)); - // Immediate RST can complete the connection after a successful 205 write; the - // existing RunAsync IsCompleted heuristic may then append [failed]. That is - // not an application error when 205 was selected for TX. + // Immediate RST can win before NntpCommandReply assigns StatusLine, in which case the + // IsCompleted heuristic may still append [failed]. When 205 was selected for TX, that is + // not an application-level QUIT failure. Assert.True( tx.Contains("[205 Connection closing]", StringComparison.Ordinal) || !tx.Contains("[failed]", StringComparison.Ordinal), diff --git a/tests/VectorNNTPD.ReaderService.Tests/Session/StreamingPostArticleReaderTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Session/StreamingPostArticleReaderTests.cs index 78cde440..90e0abf1 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Session/StreamingPostArticleReaderTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Session/StreamingPostArticleReaderTests.cs @@ -6,7 +6,7 @@ using VectorNNTPD.ReaderService.Networking.Proxy; using VectorNNTPD.ReaderService.Session.Commands.Posting; using VectorNNTPD.ReaderService.Session.Framing; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Session; public sealed class StreamingPostArticleReaderTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateAllocatorInvariantTests.cs b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateAllocatorInvariantTests.cs index 4123916d..564d5a71 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateAllocatorInvariantTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateAllocatorInvariantTests.cs @@ -1,9 +1,9 @@ using System.Net; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.SessionState; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Tests.SessionState.RateLimiting; public sealed class AccountRateAllocatorInvariantTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateFormulaTests.cs b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateFormulaTests.cs index 1f9eef29..03ee5553 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateFormulaTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/AccountRateFormulaTests.cs @@ -1,7 +1,7 @@ using VectorNNTPD.ReaderService.Authentication; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Tests.SessionState.RateLimiting; public sealed class AccountRateFormulaTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/OutboundRateLimiterTests.cs b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/OutboundRateLimiterTests.cs index 79bc2934..a7ad3a28 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/OutboundRateLimiterTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/OutboundRateLimiterTests.cs @@ -1,5 +1,5 @@ +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.SessionState.RateLimiting; - namespace VectorNNTPD.ReaderService.Tests.SessionState.RateLimiting; public sealed class OutboundRateLimiterTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingRedisTests.cs b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingRedisTests.cs index f51ab8a7..9ec57110 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingRedisTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingRedisTests.cs @@ -1,9 +1,9 @@ using System.Net; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.SessionState; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Tests.Fixtures; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Tests.SessionState.RateLimiting; public sealed class SessionStateRateLimitingRedisTests : IClassFixture diff --git a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingTests.cs index a25bde3b..be119ce4 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/SessionState/RateLimiting/SessionStateRateLimitingTests.cs @@ -2,9 +2,9 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.Authentication; using VectorNNTPD.ReaderService.SessionState; -using VectorNNTPD.ReaderService.SessionState.RateLimiting; +using VectorNNTPD.Common.Networking.RateLimiting; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Tests.SessionState.RateLimiting; public sealed class SessionStateRateLimitingTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/SessionState/SessionStateBytesAccountingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/SessionState/SessionStateBytesAccountingTests.cs index 5329e3d1..0f4d2a8b 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/SessionState/SessionStateBytesAccountingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/SessionState/SessionStateBytesAccountingTests.cs @@ -9,7 +9,7 @@ using VectorNNTPD.ReaderService.SessionState.BytesAccounting; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.ReaderService.SessionState.RateLimiting; namespace VectorNNTPD.ReaderService.Tests.SessionState; [Collection(SerilogCollection.Name)] diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitAuthinfoAndCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitAuthinfoAndCommandTests.cs index ad530e5b..ba589a6e 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitAuthinfoAndCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitAuthinfoAndCommandTests.cs @@ -8,7 +8,7 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.Commands; @@ -16,7 +16,7 @@ using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.Session; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; public sealed class TransitAuthinfoAndCommandTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs index 1863f54b..a37629a5 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs @@ -7,14 +7,14 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.History; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Redis; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Transit; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitHotReloadTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitHotReloadTests.cs index 9504922f..44bb7685 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitHotReloadTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitHotReloadTests.cs @@ -9,12 +9,12 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Commands; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; public sealed class TransitHotReloadTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs index d7a2e78f..6fe24dae 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs @@ -1,10 +1,10 @@ using System.Net; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; public sealed class TransitPeerIdentificationAndLimitTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs index 5764ef4a..51cc2e02 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs @@ -3,12 +3,12 @@ using System.Net.Sockets; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; public sealed class TransitPeerStateLifecycleTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs index 5f126013..6625b834 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs @@ -1,11 +1,11 @@ using System.Net; using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Transit; using Microsoft.Extensions.Logging.Abstractions; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; public sealed class TransitPeerStateTrackerTests diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs index 75471318..b9499308 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs @@ -6,7 +6,7 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.Authentication; using VectorNNTPD.ReaderService.Session.CommandProcessor; @@ -14,7 +14,7 @@ using VectorNNTPD.ReaderService.Tests.Session; using VectorNNTPD.ReaderService.Tests.TestDoubles; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceivePolicyCommandTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceivePolicyCommandTests.cs index 118173e5..11d6ae60 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceivePolicyCommandTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceivePolicyCommandTests.cs @@ -6,12 +6,12 @@ using VectorNNTPD.ReaderService.ArticleIngestion; using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.NntpDb; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceiveSizeAccountingTests.cs b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceiveSizeAccountingTests.cs index b285522c..f2fbf219 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceiveSizeAccountingTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/Transit/TransitReceiveSizeAccountingTests.cs @@ -8,13 +8,13 @@ using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.History; using VectorNNTPD.ReaderService.Networking.Proxy; -using VectorNNTPD.ReaderService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.ReaderService.Session; using VectorNNTPD.ReaderService.Session.CommandProcessor; using VectorNNTPD.ReaderService.Tests.Fixtures; using VectorNNTPD.ReaderService.Tests.TestDoubles; using VectorNNTPD.ReaderService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.ReaderService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs index e0de96d7..107fcdc4 100644 --- a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/ArticleRecordIngressTests.cs @@ -5,7 +5,7 @@ using VectorNNTPD.TransitService.ArticleIngestion; using VectorNNTPD.TransitService.Networking.Proxy; using VectorNNTPD.TransitService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.ArticleIngestion; public sealed class ArticleRecordIngressTests diff --git a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs index 43df675b..47f01d36 100644 --- a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IhaveArticleInterpreterTests.cs @@ -9,7 +9,7 @@ using VectorNNTPD.TransitService.Networking.Proxy; using VectorNNTPD.TransitService.Session.Framing; using VectorNNTPD.TransitService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.ArticleIngestion; public sealed class IhaveArticleInterpreterTests diff --git a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs index 52354be8..0157e8e8 100644 --- a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolNewsLogTests.cs @@ -10,7 +10,7 @@ using VectorNNTPD.TransitService.Newsgroups; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.ArticleIngestion; /// Post-queue news disposition and INN news writer contracts. diff --git a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs index d890d63a..0aa3a863 100644 --- a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/IncomingSpoolPathSurveyTests.cs @@ -10,7 +10,7 @@ using VectorNNTPD.TransitService.Newsgroups; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.ArticleIngestion; /// Post-queue Path-survey capture from canonical ArticleRecord.Path. diff --git a/tests/VectorNNTPD.TransitService.Tests/Diagnostics/FeedDiagnosticsTests.cs b/tests/VectorNNTPD.TransitService.Tests/Diagnostics/FeedDiagnosticsTests.cs index 09c70db4..ed6b03b7 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Diagnostics/FeedDiagnosticsTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Diagnostics/FeedDiagnosticsTests.cs @@ -6,11 +6,11 @@ using VectorNNTPD.TransitService.Diagnostics; using VectorNNTPD.TransitService.History; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Tests.Transit; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Diagnostics; public sealed class FeedDiagnosticsTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Fixtures/CanonicalArticleText.cs b/tests/VectorNNTPD.TransitService.Tests/Fixtures/CanonicalArticleText.cs index 29f6b3ff..0c77b10c 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Fixtures/CanonicalArticleText.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Fixtures/CanonicalArticleText.cs @@ -3,7 +3,7 @@ using VectorNNTPD.Common.Articles.Parsing; using VectorNNTPD.TransitService.ArticleIngestion; using VectorNNTPD.TransitService.Networking.Proxy; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Fixtures; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/History/HistoryCheckCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/History/HistoryCheckCommandTests.cs index ea3f5e16..f5b7d39a 100644 --- a/tests/VectorNNTPD.TransitService.Tests/History/HistoryCheckCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/History/HistoryCheckCommandTests.cs @@ -7,7 +7,7 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Redis; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; @@ -15,7 +15,7 @@ using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Tests.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.History; public sealed class HistoryCheckCommandTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs b/tests/VectorNNTPD.TransitService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs index ac0cf6a1..61c0da6a 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Moderation/EmailModerationSubmissionServiceTests.cs @@ -5,7 +5,7 @@ using VectorNNTPD.TransitService.Moderation; using VectorNNTPD.TransitService.Networking.Proxy; using VectorNNTPD.TransitService.Tests.Email; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Moderation; public sealed class EmailModerationSubmissionServiceTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs index fec79daa..16bb4eaf 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Listeners/ConnectionAcceptanceLoggingTests.cs @@ -10,12 +10,13 @@ using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Listeners; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.TransitService.Tests.Networking.Listeners; /// Connection-acceptance log format contract (TCP peer vs PROXY effective client; TLS params). diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Proxy/ProxyProtocolTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Proxy/ProxyProtocolTests.cs index cbbb8a49..47476555 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Proxy/ProxyProtocolTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Proxy/ProxyProtocolTests.cs @@ -9,12 +9,12 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Tests.Networking.Transport; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Networking.Proxy; public sealed class TrustedProxyHostsTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs index f9959577..45843950 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/ConnectionByteTransportQuiescenceTests.cs @@ -9,7 +9,7 @@ public sealed class ConnectionByteTransportQuiescenceTests public async Task AfterPauseReads_TryCommitReadToApplication_StashesClientHello() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -28,7 +28,7 @@ public async Task Quiesce_DuringHistoricalAdmitWindow_DoesNotStartStreamIoAfterI // Reproduces the pre-fix window: WaitUntilActive observed Active, then Quiesce ran and // declared idle before outstanding++. With atomic admission, stream I/O must not start. await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -56,7 +56,7 @@ public async Task Quiesce_DuringHistoricalAdmitWindow_DoesNotStartStreamIoAfterI public async Task Quiesce_WaitsForAdmittedRead_UntilStreamReadReturns() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -102,7 +102,7 @@ public async Task Quiesce_WaitsForAdmittedRead_UntilStreamReadReturns() public async Task Quiesce_WaitsForAdmittedWrite_UntilStreamWriteReturns() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -134,7 +134,7 @@ public async Task Quiesce_WaitsForAdmittedWrite_UntilStreamWriteReturns() public async Task QuiescedIdle_SimulatedHandshake_CannotOverlapPlaintextStreamIo() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -173,7 +173,7 @@ public async Task Quiesce_RepeatedAdmitRace_NeverStartsIoAfterIdle() for (var iteration = 0; iteration < 64; iteration++) { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); @@ -200,7 +200,7 @@ public async Task Quiesce_RepeatedAdmitRace_NeverStartsIoAfterIdle() public async Task QuiescedIdle_WriteAdmitRace_NeverStartsWriteAfterIdle() { await using var stream = new ControllableStream(); - await using var transport = new VectorNNTPD.TransitService.Networking.Transport.ConnectionByteTransport( + await using var transport = new VectorNNTPD.Common.Networking.Transport.ConnectionByteTransport( stream, isTls: false); diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs index 7eb39090..10f7e2cf 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/DeflateUpgradeTransportTests.cs @@ -4,7 +4,7 @@ using System.Security.Authentication; using System.Text; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs index f6dfc7b5..391cfde0 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputReaderOwnershipTests.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; using System.Net.Sockets; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs index ae1d1993..314f9e2e 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionInputWriterOwnershipTests.cs @@ -1,7 +1,7 @@ using System.Net.Sockets; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Tests.Fixtures; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs index 94f0a427..4430d199 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpConnectionOutputReaderOwnershipTests.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpDeflateStreamTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpDeflateStreamTests.cs index c9ff7498..9590102e 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpDeflateStreamTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpDeflateStreamTests.cs @@ -73,8 +73,8 @@ public async Task RoundTrip_LargeStreaming() public async Task Flush_MakesCompressedBytesVisibleWithoutDispose() { await using var peer = await ConnectedStreamPair.CreateAsync(); - await using var server = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(peer.Server); - await using var client = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(peer.Client); + await using var server = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Server); + await using var client = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Client); var payload = "flush-boundary-test-payload"u8.ToArray(); await server.WriteAsync(payload); @@ -97,7 +97,7 @@ public async Task Flush_MakesCompressedBytesVisibleWithoutDispose() public async Task MalformedCompressedInput_ThrowsWithoutSilentCorruption() { await using var peer = await ConnectedStreamPair.CreateAsync(); - await using var server = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(peer.Server); + await using var server = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Server); // Write non-DEFLATE junk on the wire (peer.Client is the raw NetworkStream). await peer.Client.WriteAsync(new byte[] { 0x00, 0x01, 0x02, 0xFF, 0xFE }); await peer.Client.FlushAsync(); @@ -114,10 +114,10 @@ public async Task IndependentStreams_DoNotShareDictionaryState() { await using var a = await ConnectedStreamPair.CreateAsync(); await using var b = await ConnectedStreamPair.CreateAsync(); - await using var aServer = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(a.Server); - await using var aClient = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(a.Client); - await using var bServer = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(b.Server); - await using var bClient = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(b.Client); + await using var aServer = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(a.Server); + await using var aClient = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(a.Client); + await using var bServer = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(b.Server); + await using var bClient = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(b.Client); await aServer.WriteAsync("connection-A-payload"u8.ToArray()); await aServer.FlushAsync(); @@ -135,8 +135,8 @@ public async Task IndependentStreams_DoNotShareDictionaryState() private static async Task AssertRoundTripAsync(byte[] plain, int writeChunk = 0, int readChunk = 0) { await using var peer = await ConnectedStreamPair.CreateAsync(); - await using var server = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(peer.Server); - await using var client = new VectorNNTPD.TransitService.Networking.Transport.NntpDeflateStream(peer.Client); + await using var server = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Server); + await using var client = new VectorNNTPD.Common.Networking.Transport.NntpDeflateStream(peer.Client); if (writeChunk <= 0) { diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpTransportTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpTransportTests.cs index 9d2fd584..1b716f76 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpTransportTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/NntpTransportTests.cs @@ -14,7 +14,7 @@ using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Listeners; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Tests.Acme; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SendPipeAdvancementTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SendPipeAdvancementTests.cs index 9fec0d82..a1159510 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SendPipeAdvancementTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SendPipeAdvancementTests.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; using System.Net.Sockets; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SocketPayloadSenderTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SocketPayloadSenderTests.cs index 5a0ac3bf..321d38ca 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SocketPayloadSenderTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/SocketPayloadSenderTests.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs index 8298bc0d..de3d96d0 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TcpConnectionDisconnectLoggingTests.cs @@ -3,10 +3,10 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking; using VectorNNTPD.TransitService.Networking.Listeners; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs index 7555bcf2..3a719b05 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TlsUpgradeTransportTests.cs @@ -4,8 +4,8 @@ using System.Security.Authentication; using System.Text; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; - +using VectorNNTPD.Common.Networking.Transport; +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; public sealed class TlsUpgradeTransportTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TransportIoSessionTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TransportIoSessionTests.cs index d9d5feaf..764262cf 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TransportIoSessionTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TransportIoSessionTests.cs @@ -1,6 +1,6 @@ using System.Buffers; using System.IO.Pipelines; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs index 8d3b8a42..4e3966cf 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxPipePauseExperimentTests.cs @@ -1,5 +1,5 @@ using System.IO.Pipelines; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteCoalescerTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteCoalescerTests.cs index 45571d2d..9ac6907f 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteCoalescerTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteCoalescerTests.cs @@ -1,5 +1,5 @@ using System.Buffers; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs index 554da27f..5c872954 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityConnectionTests.cs @@ -1,6 +1,6 @@ using System.IO.Pipelines; using System.Net.Sockets; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs index d6df7ed9..a70534d2 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Networking/Transport/TxWriteGranularityExperimentTests.cs @@ -1,4 +1,4 @@ -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; namespace VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/BenchItCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/BenchItCommandTests.cs index d3b58c3a..ef9f6027 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/BenchItCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/BenchItCommandTests.cs @@ -4,11 +4,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// Internal BENCHIT benchmark facility contracts (not a production protocol feature). diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/CheckCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/CheckCommandTests.cs index f6087876..8e48c974 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/CheckCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/CheckCommandTests.cs @@ -6,13 +6,13 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTcpTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTcpTests.cs index b7288ba7..d93cc821 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTcpTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTcpTests.cs @@ -2,13 +2,13 @@ using System.Text; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.TransitService.History; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Networking.Transport; using VectorNNTPD.TransitService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTests.cs index 6e4f6886..ef67dcac 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/CheckPipelineTests.cs @@ -7,7 +7,7 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Redis; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; @@ -16,7 +16,7 @@ using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Tests.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; public sealed class CheckPipelineTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/Commands/NntpPhaseCResponseTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/Commands/NntpPhaseCResponseTests.cs index 0d201c57..a6478403 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/Commands/NntpPhaseCResponseTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/Commands/NntpPhaseCResponseTests.cs @@ -7,13 +7,13 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.Session; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session.Commands; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/HelpCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/HelpCommandTests.cs index 9369aaa7..fff1688f 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/HelpCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/HelpCommandTests.cs @@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// HELP command — static syntax listing (RFC 3977 §7.2) with ABNF-consistent notation. diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/IHaveCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/IHaveCommandTests.cs index 0917c267..1b52ca97 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/IHaveCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/IHaveCommandTests.cs @@ -8,7 +8,7 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.PostFilter; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; @@ -17,7 +17,7 @@ using VectorNNTPD.TransitService.Newsgroups; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; public sealed class IHaveCommandTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/IHaveTcpTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/IHaveTcpTests.cs index 7b2741c2..0ff52e50 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/IHaveTcpTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/IHaveTcpTests.cs @@ -5,7 +5,7 @@ using VectorNNTPD.TransitService.ArticleIngestion; using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.Tests.Fixtures; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs index 1700edee..0b6d5117 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NewNewsAndNewGroupsRemovedTests.cs @@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpAuthinfoTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpAuthinfoTests.cs index 4dcd83b0..fcc5f448 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpAuthinfoTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpAuthinfoTests.cs @@ -6,12 +6,12 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; [Collection(nameof(NntpCommandLoggerCollection))] diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandInventoryTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandInventoryTests.cs index 951f1428..dab22b9b 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandInventoryTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandInventoryTests.cs @@ -2,11 +2,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; public sealed class NntpCommandInventoryTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLineLengthTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLineLengthTests.cs index 27d7ed64..7fe0b184 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLineLengthTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLineLengthTests.cs @@ -1,5 +1,5 @@ using System.IO.Pipelines; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLoggingTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLoggingTests.cs index 973fc164..9ec94b44 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLoggingTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandLoggingTests.cs @@ -13,14 +13,14 @@ using VectorNNTPD.TransitService.Newsgroups; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// Command RX/TX logging contract (ownership, redaction, timing shape). diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandParserTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandParserTests.cs index 0a8b05f8..7491d1d7 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandParserTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandParserTests.cs @@ -6,11 +6,11 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandTxStatusLoggingTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandTxStatusLoggingTests.cs index 0a8f838c..e65df482 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandTxStatusLoggingTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCommandTxStatusLoggingTests.cs @@ -8,11 +8,11 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCompressTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCompressTests.cs index 41fdfc85..d4a2809c 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpCompressTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpCompressTests.cs @@ -8,13 +8,13 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// Session-level COMPRESS DEFLATE (RFC 8054) regressions over the real transport stack. @@ -684,7 +684,7 @@ public PipeConnection( Output = output; _pauseReadsFails = pauseReadsFails; _upgradeFails = upgradeFails; - ClientIdentity = VectorNNTPD.TransitService.Networking.Proxy.ConnectionClientIdentity.Direct( + ClientIdentity = VectorNNTPD.Common.Networking.Proxy.ConnectionClientIdentity.Direct( new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 119)); } @@ -692,7 +692,7 @@ public PipeConnection( public System.IO.Pipelines.PipeWriter Output { get; } public System.Net.EndPoint? RemoteEndPoint => ClientIdentity.TcpPeer; public System.Net.EndPoint? LocalEndPoint => null; - public VectorNNTPD.TransitService.Networking.Proxy.ConnectionClientIdentity ClientIdentity { get; } + public VectorNNTPD.Common.Networking.Proxy.ConnectionClientIdentity ClientIdentity { get; } public bool IsTls => false; public bool TryGetNegotiatedTlsParameters(out string tlsVersion, out string cipher) diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionFoundationTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionFoundationTests.cs index d1a5812d..f045b986 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionFoundationTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionFoundationTests.cs @@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; public sealed class NntpSessionFoundationTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionIdleTimeoutTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionIdleTimeoutTests.cs index 4a71dd4b..1002e1f0 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionIdleTimeoutTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionIdleTimeoutTests.cs @@ -7,13 +7,13 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Session.Framing; using VectorNNTPD.TransitService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs index 06207404..846981a7 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpSessionInputReaderLifecycleTests.cs @@ -2,7 +2,7 @@ using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/NntpStartTlsTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/NntpStartTlsTests.cs index a7d13c6c..be0f43cc 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/NntpStartTlsTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/NntpStartTlsTests.cs @@ -3,7 +3,7 @@ using System.Security.Authentication; using System.Text; using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Tests.Networking.Transport; diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/PostArticleValidationTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/PostArticleValidationTests.cs index 41546a81..caffe0fa 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/PostArticleValidationTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/PostArticleValidationTests.cs @@ -4,7 +4,7 @@ using VectorNNTPD.TransitService.Networking.Proxy; using VectorNNTPD.TransitService.Session.Commands.Posting; using VectorNNTPD.TransitService.Tests.Fixtures; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; public sealed class PostArticleValidationTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/QuitCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/QuitCommandTests.cs index 6a46db4d..b4ac2e0f 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/QuitCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/QuitCommandTests.cs @@ -5,12 +5,12 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Networking.Transport; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// QUIT command — RFC 3977 §5.4 and peer-disappears-during-205 race. @@ -196,9 +196,9 @@ public async Task Quit_Socket_ImmediateClientCloseAfterQuit_IsNotApplicationErro recording.Messages, m => m.Contains("TX: QUIT", StringComparison.Ordinal) && m.Contains("executed in", StringComparison.Ordinal)); - // Immediate RST can complete the connection after a successful 205 write; the - // existing RunAsync IsCompleted heuristic may then append [failed]. That is - // not an application error when 205 was selected for TX. + // Immediate RST can win before NntpCommandReply assigns StatusLine, in which case the + // IsCompleted heuristic may still append [failed]. When 205 was selected for TX, that is + // not an application-level QUIT failure. Assert.True( tx.Contains("[205 Connection closing]", StringComparison.Ordinal) || !tx.Contains("[failed]", StringComparison.Ordinal), diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/SpeedTest/SpeedTestCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/SpeedTest/SpeedTestCommandTests.cs index 7e300609..2c1ff435 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/SpeedTest/SpeedTestCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/SpeedTest/SpeedTestCommandTests.cs @@ -7,14 +7,14 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Session.SpeedTest; using VectorNNTPD.TransitService.Tests.Transit; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session.SpeedTest; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/StreamingPostArticleReaderTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/StreamingPostArticleReaderTests.cs index fc45b115..09f0ef41 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/StreamingPostArticleReaderTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/StreamingPostArticleReaderTests.cs @@ -6,7 +6,7 @@ using VectorNNTPD.TransitService.Networking.Proxy; using VectorNNTPD.TransitService.Session.Commands.Posting; using VectorNNTPD.TransitService.Session.Framing; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; public sealed class StreamingPostArticleReaderTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisCommandTests.cs index a8ab25b2..36b2cf6d 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisCommandTests.cs @@ -8,7 +8,7 @@ using VectorNNTPD.TransitService.Authentication; using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.PostFilter; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; @@ -20,7 +20,7 @@ using VectorNNTPD.TransitService.Tests.Networking.Transport; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Tests.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// RFC 4644 TAKETHIS streaming / pipelining / ingestion contracts. diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisPipelineTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisPipelineTests.cs index 7c2fd0c8..fad9d7f2 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisPipelineTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/TakeThisPipelineTests.cs @@ -10,7 +10,7 @@ using VectorNNTPD.TransitService.History; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Redis; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; @@ -19,7 +19,7 @@ using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Tests.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/TransitPeerAuthorizationTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/TransitPeerAuthorizationTests.cs index f2b89f83..292e05af 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/TransitPeerAuthorizationTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/TransitPeerAuthorizationTests.cs @@ -7,14 +7,14 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.Networking.Transport; using VectorNNTPD.TransitService.Tests.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// Connection-time transit/streaming peer ACL (top-level named Transit peers). diff --git a/tests/VectorNNTPD.TransitService.Tests/Session/TransitReaderCommandIsolationTests.cs b/tests/VectorNNTPD.TransitService.Tests/Session/TransitReaderCommandIsolationTests.cs index 3bcc8622..e272f09a 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Session/TransitReaderCommandIsolationTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Session/TransitReaderCommandIsolationTests.cs @@ -3,10 +3,10 @@ using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.CommandProcessor; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Session; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Telemetry/ApplicationTelemetryTests.cs b/tests/VectorNNTPD.TransitService.Tests/Telemetry/ApplicationTelemetryTests.cs index 14ebb1e2..8ae027e3 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Telemetry/ApplicationTelemetryTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Telemetry/ApplicationTelemetryTests.cs @@ -11,14 +11,14 @@ using VectorNNTPD.TransitService.Diagnostics; using VectorNNTPD.TransitService.History; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Telemetry; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Tests.Transit; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Telemetry; public sealed class ApplicationTelemetryTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitAuthinfoAndCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitAuthinfoAndCommandTests.cs index f986fc23..fc68e7fe 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitAuthinfoAndCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitAuthinfoAndCommandTests.cs @@ -8,7 +8,7 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Session.Commands; @@ -16,7 +16,7 @@ using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.Session; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; public sealed class TransitAuthinfoAndCommandTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs index 997cf8e7..c4862b7c 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitDeferOnDuplicateCommandTests.cs @@ -7,14 +7,14 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.History; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Redis; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Transit; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitHotReloadTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitHotReloadTests.cs index 18907e8a..196b186a 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitHotReloadTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitHotReloadTests.cs @@ -9,12 +9,12 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Commands; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; public sealed class TransitHotReloadTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs index 2ea0a45a..761f74dc 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerIdentificationAndLimitTests.cs @@ -1,10 +1,10 @@ using System.Net; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; public sealed class TransitPeerIdentificationAndLimitTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs index 5bcd5c27..a4dda629 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateLifecycleTests.cs @@ -3,12 +3,12 @@ using System.Net.Sockets; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; public sealed class TransitPeerStateLifecycleTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs index 593b1058..3808e2e6 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitPeerState/TransitPeerStateTrackerTests.cs @@ -1,11 +1,11 @@ using System.Net; using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Transit; using Microsoft.Extensions.Logging.Abstractions; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; public sealed class TransitPeerStateTrackerTests diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs index 09d881c1..cf8e68ab 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReaderAdmissionReclassificationTests.cs @@ -6,7 +6,7 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.Common.Networking.Certificates; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.Authentication; using VectorNNTPD.TransitService.Session.CommandProcessor; @@ -14,7 +14,7 @@ using VectorNNTPD.TransitService.Tests.Session; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceivePolicyCommandTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceivePolicyCommandTests.cs index ba4a2c8b..03952dc6 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceivePolicyCommandTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceivePolicyCommandTests.cs @@ -9,13 +9,13 @@ using VectorNNTPD.TransitService.ArticleIngestion; using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.NntpDb; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; /// diff --git a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceiveSizeAccountingTests.cs b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceiveSizeAccountingTests.cs index d02aac80..580035ce 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceiveSizeAccountingTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Transit/TransitReceiveSizeAccountingTests.cs @@ -8,13 +8,13 @@ using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.History; using VectorNNTPD.TransitService.Networking.Proxy; -using VectorNNTPD.TransitService.Networking.Transport; +using VectorNNTPD.Common.Networking.Transport; using VectorNNTPD.TransitService.Session; using VectorNNTPD.TransitService.Session.CommandProcessor; using VectorNNTPD.TransitService.Tests.Fixtures; using VectorNNTPD.TransitService.Tests.TestDoubles; using VectorNNTPD.TransitService.Transit; - +using VectorNNTPD.Common.Networking.Proxy; namespace VectorNNTPD.TransitService.Tests.Transit; /// From b4fcc43cde6dd16a88a004a5e8447496d54d5ac7 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Thu, 29 Oct 2026 20:21:25 +0200 Subject: [PATCH 2/6] Fix CodeQL empty catches and resource disposal --- .../Listener/CacheListenerService.cs | 8 ++- .../Transport/NntpConnectionStream.cs | 10 ++-- .../Listener/StorageNntpListenerService.cs | 4 +- ...AcceptListenerAndPipelineTransportTests.cs | 54 ++++++++++++------- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs index bfaba99f..f67b7bd3 100644 --- a/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs +++ b/src/VectorNNTPD.BackFillerService/Listener/CacheListenerService.cs @@ -747,8 +747,10 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke { accepted.Dispose(); } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { + // Accept path may race with a test hook or failed option setup that already disposed. + _ = ex; // observed; intentionally ignored at this boundary } } @@ -840,8 +842,10 @@ private async Task CloseListenSocketsAsync() { await listener.DisposeAsync().ConfigureAwait(false); } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { + // Concurrent stop/dispose can already have released the listen socket. + _ = ex; // observed; intentionally ignored at this boundary } catch (SocketException ex) { diff --git a/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs b/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs index 978b0d34..7eb0709e 100644 --- a/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs @@ -189,18 +189,22 @@ public override async ValueTask DisposeAsync() if (_ownsConnection) { + // CompleteAsync is best-effort before DisposeAsync; teardown races must not skip disposal. try { await _connection.CompleteAsync().ConfigureAwait(false); } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { + _ = ex; // observed; intentionally ignored at this boundary } - catch (IOException) + catch (IOException ex) { + _ = ex; // observed; intentionally ignored at this best-effort boundary } - catch (SocketException) + catch (SocketException ex) { + _ = ex; // observed; intentionally ignored at this best-effort boundary } await _connection.DisposeAsync().ConfigureAwait(false); diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs index 37e148a9..097a7367 100644 --- a/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs @@ -484,8 +484,10 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke { accepted.Dispose(); } - catch (ObjectDisposedException) + catch (ObjectDisposedException ex) { + // Handshake failure or concurrent teardown may already have disposed the socket. + ExpectedException.Ignore(ex); } } diff --git a/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs b/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs index aac927ac..1965d9a5 100644 --- a/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs +++ b/tests/VectorNNTPD.Common.Tests/Networking/Transport/SocketAcceptListenerAndPipelineTransportTests.cs @@ -83,30 +83,21 @@ public async Task Accept_loop_supports_concurrent_clients() listener.Start(); var endpoint = listener.LocalEndPoint; - var sockets = new List(); - try + // Hold every client open until all connects, admissions, and writes complete. + using var sockets = new TcpClientGroup(); + for (var i = 0; i < clients; i++) { - for (var i = 0; i < clients; i++) - { - var client = new TcpClient(); - await client.ConnectAsync(endpoint, CancellationToken.None); - sockets.Add(client); - } + var client = new TcpClient(); + await client.ConnectAsync(endpoint, CancellationToken.None); + sockets.Add(client); + } - await WaitUntilAsync(() => Volatile.Read(ref admitted) == clients, TimeSpan.FromSeconds(5)); - gate.TrySetResult(); + await WaitUntilAsync(() => Volatile.Read(ref admitted) == clients, TimeSpan.FromSeconds(5)); + gate.TrySetResult(); - foreach (var client in sockets) - { - await client.GetStream().WriteAsync(new byte[] { 0x2E }); - } - } - finally + foreach (var client in sockets.Items) { - foreach (var client in sockets) - { - client.Dispose(); - } + await client.GetStream().WriteAsync(new byte[] { 0x2E }); } } @@ -217,5 +208,28 @@ private static async Task WaitUntilAsync(Func condition, TimeSpan timeout) await Task.Delay(10); } } + + /// Owns connected instances until the enclosing using ends. + private sealed class TcpClientGroup : IDisposable + { + private readonly List _clients = []; + + /// Clients still owned by this group. + public IReadOnlyList Items => _clients; + + /// Takes ownership of . + public void Add(TcpClient client) => _clients.Add(client); + + /// + public void Dispose() + { + foreach (var client in _clients) + { + client.Dispose(); + } + + _clients.Clear(); + } + } } } From 0fd512b43137744f6fbc245134d2b4bb082f07e8 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Fri, 30 Oct 2026 07:18:43 +0200 Subject: [PATCH 3/6] Add Linux test hang diagnostics --- .github/workflows/build.yml | 50 +++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3f206470..a67ab60b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -108,42 +108,47 @@ jobs: shell: bash run: | set -euo pipefail - # Only the BackFiller test project references coverlet.collector. Passing - # --collect to a project without that package makes VSTest fail before - # the tests run. Coverage is collected for projects that already - # reference the collector. Adding it to the other test projects is a - # separate change. + # Collect coverage only when the project references coverlet.collector. + # Passing --collect without that package makes VSTest fail before tests run. + # --blame-hang-timeout is a per-test inactivity bound (renewed each case for + # xUnit). Healthy Linux CI durations: Reader ~40s / Transit ~30s; local + # Transit ~55-70s. Longest intentional WaitAsync seen in these suites is 60s. + # A 3m hang bound fails a stuck host with a sequence/mini-dump, lets the + # sequential loop continue to later projects, and avoids burning the whole + # 25m step with a missing TRX (PR #258 run 40930050020: Transit hung once, + # Reader hung on the failed-job rerun; no specific test identified). failed=0 while IFS= read -r proj; do if ! grep -q 'true' "$proj"; then continue fi name="$(basename "$proj" .csproj)" - echo "Testing $name" + echo "Testing $name at $(date -u +%Y-%m-%dT%H:%M:%SZ)" + common_args=( + --configuration Release + -p:Platform=x64 + --no-build + --no-restore + --results-directory ./TestResults + --logger "trx;LogFileName=${name}.trx" + --blame-hang-timeout 3m + --blame-hang-dump-type mini + ) if grep -q 'coverlet.collector' "$proj"; then if ! dotnet test "$proj" \ - --configuration Release \ - -p:Platform=x64 \ - --no-build \ - --no-restore \ - --collect:"XPlat Code Coverage" \ - --results-directory ./TestResults \ - --logger "trx;LogFileName=${name}.trx"; then - echo "Tests failed: $name" + "${common_args[@]}" \ + --collect:"XPlat Code Coverage"; then + echo "Tests failed: $name (dotnet test exit non-zero; hang dumps under TestResults if blame fired)" failed=1 fi else if ! dotnet test "$proj" \ - --configuration Release \ - -p:Platform=x64 \ - --no-build \ - --no-restore \ - --results-directory ./TestResults \ - --logger "trx;LogFileName=${name}.trx"; then - echo "Tests failed: $name" + "${common_args[@]}"; then + echo "Tests failed: $name (dotnet test exit non-zero; hang dumps under TestResults if blame fired)" failed=1 fi fi + echo "Finished $name at $(date -u +%Y-%m-%dT%H:%M:%SZ)" done < <(find src tests -name '*.csproj' | sort) exit "$failed" @@ -227,7 +232,8 @@ jobs: uses: actions/upload-artifact@v7 with: name: linux-test-results - path: TestResults/**/*.trx + # TRX plus blame hang sequence/dump attachments when a test host stalls. + path: TestResults/** if-no-files-found: error retention-days: 14 From dc46bef225d0e8ac98036842d0e09cb1085781dc Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Fri, 30 Oct 2026 07:39:16 +0200 Subject: [PATCH 4/6] Fix peer disconnect handling in StorageService --- .../Transport/NntpConnectionStream.cs | 23 ++++++++++++++----- .../Listener/StorageNntpFeedSession.cs | 18 +++++++++++---- .../StorageNntpFeedSessionEndReason.cs | 21 +++++++++++++++++ .../Listener/StorageNntpListenerService.cs | 8 ++++++- .../Transport/NntpConnectionStreamTests.cs | 17 ++++++++++++++ 5 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSessionEndReason.cs diff --git a/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs b/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs index 7eb0709e..b79ab51b 100644 --- a/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs +++ b/src/VectorNNTPD.Common/Networking/Transport/NntpConnectionStream.cs @@ -158,13 +158,24 @@ public override async ValueTask WriteAsync( FlushResult flush; do { - var memory = _connection.Output.GetMemory(Math.Min(remaining.Length, WriteChunkBytes)); - var toCopy = Math.Min(memory.Length, remaining.Length); - remaining.Span[..toCopy].CopyTo(memory.Span); - _connection.Output.Advance(toCopy); - remaining = remaining[toCopy..]; + try + { + var memory = _connection.Output.GetMemory(Math.Min(remaining.Length, WriteChunkBytes)); + var toCopy = Math.Min(memory.Length, remaining.Length); + remaining.Span[..toCopy].CopyTo(memory.Span); + _connection.Output.Advance(toCopy); + remaining = remaining[toCopy..]; + + flush = await _connection.Output.FlushAsync(cancellationToken).ConfigureAwait(false); + } + catch (InvalidOperationException ex) + { + // Peer reset / CompleteAsync can finish the output writer while a session write is + // in flight. Surface that as IOException so listeners classify peer disconnect + // rather than an operational failure. + throw new IOException("NNTP connection output completed while writing.", ex); + } - flush = await _connection.Output.FlushAsync(cancellationToken).ConfigureAwait(false); if (flush.IsCanceled) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs index ff41ef89..974aab84 100644 --- a/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs @@ -94,8 +94,14 @@ public StorageNntpFeedSession( ArticlePrefix.Length + NntpMessageIdValidation.MaxMessageIdLength + ArticleFollowsSuffix.Length]; } - /// Sends the greeting, then reads commands until QUIT, peer close, or cancellation. - public async Task RunAsync(CancellationToken cancellationToken) + /// + /// Sends the greeting, then reads commands until QUIT, peer close, or cancellation. + /// + /// + /// when a command read observes + /// EOF; otherwise . + /// + public async Task RunAsync(CancellationToken cancellationToken) { await WriteFullyAsync(Greeting, cancellationToken).ConfigureAwait(false); while (!cancellationToken.IsCancellationRequested) @@ -103,7 +109,7 @@ public async Task RunAsync(CancellationToken cancellationToken) var command = await ReadCommandAsync(cancellationToken).ConfigureAwait(false); if (command.Kind == CommandReadKind.Disconnected) { - return; + return StorageNntpFeedSessionEndReason.PeerDisconnected; } if (command.Kind == CommandReadKind.Overlong) @@ -122,7 +128,7 @@ public async Task RunAsync(CancellationToken cancellationToken) } await WriteFullyAsync(Closing, cancellationToken).ConfigureAwait(false); - return; + return StorageNntpFeedSessionEndReason.Closed; } if (TryMatchKeyword(line, TakeThisKeyword, out var takeRest)) @@ -132,7 +138,7 @@ public async Task RunAsync(CancellationToken cancellationToken) var keepOpen = await HandleTakeThisAsync(takeArg, cancellationToken).ConfigureAwait(false); if (!keepOpen) { - return; + return StorageNntpFeedSessionEndReason.Closed; } continue; @@ -148,6 +154,8 @@ public async Task RunAsync(CancellationToken cancellationToken) await WriteFullyAsync(UnknownCommand, cancellationToken).ConfigureAwait(false); } + + return StorageNntpFeedSessionEndReason.Closed; } /// diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSessionEndReason.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSessionEndReason.cs new file mode 100644 index 00000000..8b0ff3bd --- /dev/null +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSessionEndReason.cs @@ -0,0 +1,21 @@ +namespace VectorNNTPD.StorageService.Listener +{ + /// + /// How a Storage NNTP feed session finished after + /// . + /// + public enum StorageNntpFeedSessionEndReason + { + /// + /// Normal close: QUIT, intentional session teardown after a command, or + /// cancellation exited the command loop without a peer EOF. + /// + Closed = 0, + + /// + /// Peer closed the connection while the session was reading the next command + /// (transport read returned EOF). + /// + PeerDisconnected = 1, + } +} diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs index 097a7367..07520f41 100644 --- a/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs @@ -449,7 +449,13 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke resolveRetrievableArticle: _resolveRetrievableArticle, afterArticleDelivered: _afterArticleDelivered, acquireArticleReadLease: _acquireArticleReadLease); - await session.RunAsync(cancellationToken).ConfigureAwait(false); + var endReason = await session.RunAsync(cancellationToken).ConfigureAwait(false); + if (endReason == StorageNntpFeedSessionEndReason.PeerDisconnected) + { + // Shared transport surfaces peer close as EOF on the next command read; + // that path completes RunAsync without throwing IOException. + StorageNntpListenerLogMessages.ConnectionPeerDisconnected(_logger, remote); + } } } catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) diff --git a/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs b/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs index 70d68e57..810065fb 100644 --- a/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs +++ b/tests/VectorNNTPD.Common.Tests/Networking/Transport/NntpConnectionStreamTests.cs @@ -110,6 +110,23 @@ await Assert.ThrowsAsync( async () => await stream.WriteAsync(new byte[] { 1, 2, 3 })); } + /// + /// Completing the output (peer reset / connection teardown) must + /// surface as , not , so + /// Storage/BackFiller listeners classify peer disconnect correctly. + /// + [Fact] + public async Task WriteAsync_completed_output_writer_throws_IOException() + { + await using var duplex = new PipeDuplex(); + await duplex.Output.CompleteAsync(); + await using var stream = new NntpConnectionStream(duplex, ownsConnection: false); + + var ex = await Assert.ThrowsAsync( + async () => await stream.WriteAsync(new byte[] { 1, 2, 3 })); + Assert.IsType(ex.InnerException); + } + /// Large write chunks and is fully readable from the outbound pipe. [Fact] public async Task WriteAsync_large_payload_is_fully_flushed() From 88f9331d2ec9fe8afbfb29529cb66a3f1a0007e9 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Fri, 30 Oct 2026 08:06:37 +0200 Subject: [PATCH 5/6] Fix queue drain concurrency tests --- .../ArticleIngestion/QueueDrain20Tests.cs | 79 ++++++++++++++++++- .../ArticleIngestion/QueueDrain20Tests.cs | 78 +++++++++++++++++- 2 files changed, 153 insertions(+), 4 deletions(-) diff --git a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/QueueDrain20Tests.cs b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/QueueDrain20Tests.cs index d17e7468..f5559d42 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/QueueDrain20Tests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/QueueDrain20Tests.cs @@ -28,11 +28,72 @@ public async Task TwentyWorkers_ConsumeEveryArticleExactlyOnce() [Fact] public async Task TwentyWorkers_CallDequeueAsyncConcurrently_AndAllParticipate() { + // Multi-reader Channel does not promise fair item distribution: one consumer may + // drain every article when WaitToReadAsync completes synchronously while data + // remains (observed on Linux CI in ~60ms with a single participant). The contract + // under test is concurrent DequeueAsync — all workers must be able to wait in + // DequeueAsync together before work is published — not scheduler-fair sharing. const int articleCount = 400; var queue = CreateQueue(articleCount); var seen = new ConcurrentDictionary(StringComparer.Ordinal); var hits = new int[WorkerCount]; - var workers = StartWorkers(queue, WorkerCount, seen, hits); + using var enteredDequeue = new CountdownEvent(WorkerCount); + var concurrentDequeues = 0; + var maxConcurrentDequeues = 0; + + var workers = new Task[WorkerCount]; + for (var i = 0; i < WorkerCount; i++) + { + var workerIndex = i; + workers[workerIndex] = Task.Run(async () => + { + var signaledEntry = false; + while (true) + { + InboundArticle? article; + try + { + var inflight = Interlocked.Increment(ref concurrentDequeues); + UpdateMax(ref maxConcurrentDequeues, inflight); + try + { + var dequeue = queue.DequeueAsync(CancellationToken.None); + if (!signaledEntry) + { + signaledEntry = true; + enteredDequeue.Signal(); + } + + article = await dequeue.ConfigureAwait(false); + } + finally + { + Interlocked.Decrement(ref concurrentDequeues); + } + } + catch (OperationCanceledException) + { + return; + } + + if (article is null) + { + return; + } + + seen.AddOrUpdate(article.MessageId, 1, static (_, count) => count + 1); + Interlocked.Increment(ref hits[workerIndex]); + } + }); + } + + Assert.True( + enteredDequeue.Wait(TimeSpan.FromSeconds(5)), + "all workers must enter DequeueAsync before articles are published"); + Assert.True( + Volatile.Read(ref maxConcurrentDequeues) >= WorkerCount, + "DequeueAsync must support WorkerCount concurrent waiters on an empty queue"); + EnqueueUnique(queue, articleCount); queue.Complete(); await WaitWorkersAsync(workers); @@ -40,7 +101,6 @@ public async Task TwentyWorkers_CallDequeueAsyncConcurrently_AndAllParticipate() Assert.Equal(articleCount, seen.Count); Assert.All(seen.Values, static count => Assert.Equal(1, count)); Assert.Equal(articleCount, hits.Sum()); - Assert.True(hits.Count(static h => h > 0) > 1); Assert.Equal(0, queue.QueuedBytes); Assert.Equal(0, queue.Count); } @@ -433,6 +493,21 @@ private static async Task WaitWorkersAsync(Task[] workers) await Task.WhenAll(workers).WaitAsync(safety.Token); } + private static void UpdateMax(ref int location, int candidate) + { + var current = Volatile.Read(ref location); + while (candidate > current) + { + var observed = Interlocked.CompareExchange(ref location, candidate, current); + if (observed == current) + { + return; + } + + current = observed; + } + } + private static InboundArticle Article(string messageId) => CanonicalArticleText.CreateQueued(messageId, InboundArticleProducer.IHave); } diff --git a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/QueueDrain20Tests.cs b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/QueueDrain20Tests.cs index 1525e5c1..74c57476 100644 --- a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/QueueDrain20Tests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/QueueDrain20Tests.cs @@ -28,11 +28,71 @@ public async Task TwentyWorkers_ConsumeEveryArticleExactlyOnce() [Fact] public async Task TwentyWorkers_CallDequeueAsyncConcurrently_AndAllParticipate() { + // Multi-reader Channel does not promise fair item distribution: one consumer may + // drain every article when WaitToReadAsync completes synchronously while data + // remains (observed on Linux CI for the Reader twin). Concurrent DequeueAsync + // entry is the portable contract; fair sharing is not. const int articleCount = 400; var queue = CreateQueue(articleCount); var seen = new ConcurrentDictionary(StringComparer.Ordinal); var hits = new int[WorkerCount]; - var workers = StartWorkers(queue, WorkerCount, seen, hits); + using var enteredDequeue = new CountdownEvent(WorkerCount); + var concurrentDequeues = 0; + var maxConcurrentDequeues = 0; + + var workers = new Task[WorkerCount]; + for (var i = 0; i < WorkerCount; i++) + { + var workerIndex = i; + workers[workerIndex] = Task.Run(async () => + { + var signaledEntry = false; + while (true) + { + InboundArticle? article; + try + { + var inflight = Interlocked.Increment(ref concurrentDequeues); + UpdateMax(ref maxConcurrentDequeues, inflight); + try + { + var dequeue = queue.DequeueAsync(CancellationToken.None); + if (!signaledEntry) + { + signaledEntry = true; + enteredDequeue.Signal(); + } + + article = await dequeue.ConfigureAwait(false); + } + finally + { + Interlocked.Decrement(ref concurrentDequeues); + } + } + catch (OperationCanceledException) + { + return; + } + + if (article is null) + { + return; + } + + seen.AddOrUpdate(article.MessageId, 1, static (_, count) => count + 1); + Interlocked.Increment(ref hits[workerIndex]); + } + }); + } + + Assert.True( + enteredDequeue.Wait(TimeSpan.FromSeconds(5)), + "all workers must enter DequeueAsync before articles are published"); + Assert.True( + Volatile.Read(ref maxConcurrentDequeues) >= WorkerCount, + "DequeueAsync must support WorkerCount concurrent waiters on an empty queue"); + EnqueueUnique(queue, articleCount); queue.Complete(); await WaitWorkersAsync(workers); @@ -40,7 +100,6 @@ public async Task TwentyWorkers_CallDequeueAsyncConcurrently_AndAllParticipate() Assert.Equal(articleCount, seen.Count); Assert.All(seen.Values, static count => Assert.Equal(1, count)); Assert.Equal(articleCount, hits.Sum()); - Assert.True(hits.Count(static h => h > 0) > 1); Assert.Equal(0, queue.QueuedBytes); Assert.Equal(0, queue.Count); } @@ -433,6 +492,21 @@ private static async Task WaitWorkersAsync(Task[] workers) await Task.WhenAll(workers).WaitAsync(safety.Token); } + private static void UpdateMax(ref int location, int candidate) + { + var current = Volatile.Read(ref location); + while (candidate > current) + { + var observed = Interlocked.CompareExchange(ref location, candidate, current); + if (observed == current) + { + return; + } + + current = observed; + } + } + private static InboundArticle Article(string messageId) => CanonicalArticleText.CreateQueued(messageId, InboundArticleProducer.IHave); } From 90b2643511178f9cc504e45f59e481a88851e005 Mon Sep 17 00:00:00 2001 From: "savage@savage.za.org" Date: Fri, 30 Oct 2026 16:19:20 +0200 Subject: [PATCH 6/6] Fix publisher pool shutdown and stabilize reconnect tests --- .../OverviewDb/OverviewDbPublisherPool.cs | 39 +++++++- .../OverviewDb/OverviewDbPublisherPool.cs | 39 +++++++- .../OverviewDbPublisherStageTests.cs | 91 ++++++++++++++++++- .../RabbitMq/RabbitMqServiceTests.cs | 30 ++++-- .../OverviewDbPublisherStageTests.cs | 91 ++++++++++++++++++- .../RabbitMq/RabbitMqServiceTests.cs | 30 ++++-- .../ArticlePlacementConnectionTests.cs | 45 ++++++--- 7 files changed, 332 insertions(+), 33 deletions(-) diff --git a/src/VectorNNTPD.ReaderService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs b/src/VectorNNTPD.ReaderService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs index bd0771a3..c28bf63a 100644 --- a/src/VectorNNTPD.ReaderService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs +++ b/src/VectorNNTPD.ReaderService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs @@ -1,3 +1,4 @@ +using VectorNNTPD.Common.Core; using VectorNNTPD.ReaderService.ArticleIngestion; using VectorNNTPD.ReaderService.Configuration; using VectorNNTPD.ReaderService.Diagnostics; @@ -344,6 +345,7 @@ private async Task DrainRemainingAsync() OverviewDbWorkItem? item; try { + // Channel is completed; None only ends the read when the writer is done and empty. item = await _queue.DequeueAsync(CancellationToken.None).ConfigureAwait(false); } catch (OperationCanceledException) @@ -356,7 +358,7 @@ private async Task DrainRemainingAsync() break; } - await PublishOneAsync(item, CancellationToken.None).ConfigureAwait(false); + await PublishAcceptedDuringDrainAsync(item).ConfigureAwait(false); } DrainPublishFailures(); @@ -369,11 +371,19 @@ private async Task PublishOneAsync(OverviewDbWorkItem item, CancellationToken ca // Does not await broker confirmation — only outstanding-window capacity + write. await _publisher.PublishAsync(item, cancellationToken).ConfigureAwait(false); } - catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) + catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested && _queue.IsAccepting) + { + _ = ex; // observed; intentionally ignored at this boundary + // Queue still accepting: cooperative cancel abandons this in-flight publish. + // Do not claim the OverviewDB work queue is unavailable — cancellation is not + // a queue-admission failure. + } + catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested && !_queue.IsAccepting) { _ = ex; // observed; intentionally ignored at this boundary - // Shutdown: abandon in-flight work. Do not claim the OverviewDB work queue - // is unavailable — cancellation is not a queue-admission failure. + // Queue completed: this item was already dequeued. DrainRemainingAsync only + // covers items still in the channel; finish this one under the drain budget. + await PublishAcceptedDuringDrainAsync(item).ConfigureAwait(false); } // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. catch (Exception ex) @@ -385,6 +395,27 @@ private async Task PublishOneAsync(OverviewDbWorkItem item, CancellationToken ca } } + /// + /// Publishes one accepted item after using the + /// configured OverviewDB publisher shutdown budget instead of an uncancellable token. + /// + private async Task PublishAcceptedDuringDrainAsync(OverviewDbWorkItem item) + { + var drainSeconds = Math.Max(1, _options.OverviewDbPublisherShutdownSeconds); + using var drainCts = new CancellationTokenSource(TimeSpan.FromSeconds(drainSeconds)); + try + { + await _publisher.PublishAsync(item, drainCts.Token).ConfigureAwait(false); + } + // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. + catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex)) + { + // Timed-out or failed drain publish: requeue only while still accepting; otherwise log. + OverviewDbHandoffLogMessages.PublishFailed(_logger, ex, item.MessageId, item.ByteLength); + await TryRequeueFailureAsync(item).ConfigureAwait(false); + } + } + private void DrainPublishFailures() { while (_publisher.TryDequeuePublishFailure(out var failed)) diff --git a/src/VectorNNTPD.TransitService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs b/src/VectorNNTPD.TransitService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs index db26b29e..f0f6804d 100644 --- a/src/VectorNNTPD.TransitService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs +++ b/src/VectorNNTPD.TransitService/ArticleIngestion/OverviewDb/OverviewDbPublisherPool.cs @@ -1,3 +1,4 @@ +using VectorNNTPD.Common.Core; using VectorNNTPD.TransitService.ArticleIngestion; using VectorNNTPD.TransitService.Configuration; using VectorNNTPD.TransitService.Diagnostics; @@ -344,6 +345,7 @@ private async Task DrainRemainingAsync() OverviewDbWorkItem? item; try { + // Channel is completed; None only ends the read when the writer is done and empty. item = await _queue.DequeueAsync(CancellationToken.None).ConfigureAwait(false); } catch (OperationCanceledException) @@ -356,7 +358,7 @@ private async Task DrainRemainingAsync() break; } - await PublishOneAsync(item, CancellationToken.None).ConfigureAwait(false); + await PublishAcceptedDuringDrainAsync(item).ConfigureAwait(false); } DrainPublishFailures(); @@ -369,11 +371,19 @@ private async Task PublishOneAsync(OverviewDbWorkItem item, CancellationToken ca // Does not await broker confirmation — only outstanding-window capacity + write. await _publisher.PublishAsync(item, cancellationToken).ConfigureAwait(false); } - catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) + catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested && _queue.IsAccepting) + { + _ = ex; // observed; intentionally ignored at this boundary + // Queue still accepting: cooperative cancel abandons this in-flight publish. + // Do not claim the OverviewDB work queue is unavailable — cancellation is not + // a queue-admission failure. + } + catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested && !_queue.IsAccepting) { _ = ex; // observed; intentionally ignored at this boundary - // Shutdown: abandon in-flight work. Do not claim the OverviewDB work queue - // is unavailable — cancellation is not a queue-admission failure. + // Queue completed: this item was already dequeued. DrainRemainingAsync only + // covers items still in the channel; finish this one under the drain budget. + await PublishAcceptedDuringDrainAsync(item).ConfigureAwait(false); } // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. catch (Exception ex) @@ -385,6 +395,27 @@ private async Task PublishOneAsync(OverviewDbWorkItem item, CancellationToken ca } } + /// + /// Publishes one accepted item after using the + /// configured OverviewDB publisher shutdown budget instead of an uncancellable token. + /// + private async Task PublishAcceptedDuringDrainAsync(OverviewDbWorkItem item) + { + var drainSeconds = Math.Max(1, _options.OverviewDbPublisherShutdownSeconds); + using var drainCts = new CancellationTokenSource(TimeSpan.FromSeconds(drainSeconds)); + try + { + await _publisher.PublishAsync(item, drainCts.Token).ConfigureAwait(false); + } + // codeql[cs/catch-of-all-exceptions] Service/process boundary must observe every non-fatal failure without aborting the host. + catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex)) + { + // Timed-out or failed drain publish: requeue only while still accepting; otherwise log. + OverviewDbHandoffLogMessages.PublishFailed(_logger, ex, item.MessageId, item.ByteLength); + await TryRequeueFailureAsync(item).ConfigureAwait(false); + } + } + private void DrainPublishFailures() { while (_publisher.TryDequeuePublishFailure(out var failed)) diff --git a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs index dec8535e..7e4fd55f 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs @@ -274,12 +274,61 @@ public async Task PublisherPool_DrainsQueuedWorkOnComplete_BeforeExit() await Task.Delay(10, wait.Token); } + // Complete first, then cancel while the in-flight publish is still gated. + // Production must finish that item (and drain the queued peer) after Complete(), + // not abandon the in-flight publish when the worker token cancels. work.Complete(); - gate.TrySetResult(); await cts.CancelAsync(); + gate.TrySetResult(); await run.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(2, publisher.ConfirmCount); + // At least one attempt for each accepted item. A cancel-vs-gate race may skip the + // drain retry for the first item when WaitAsync observes the completed gate first. + Assert.InRange(publisher.Started, 2, 3); + Assert.Equal(0, work.Count); + } + + [Fact] + public async Task PublisherPool_DrainPublishFailure_AfterComplete_ContinuesDrainWithoutHang() + { + var work = new OverviewDbWorkQueue(memoryLimitBytes: 1024 * 1024); + var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var publisher = new GatedFailOnceOverviewDbHandoffPublisher(gate); + var options = new ArticleIngestionOptions + { + OverviewDbMinPublisherWorkers = 1, + OverviewDbMaxPublisherWorkers = 1, + ScaleIntervalSeconds = 3600, + OverviewDbPublisherShutdownSeconds = 2, + }; + + using var cts = new CancellationTokenSource(); + var pool = new OverviewDbPublisherPool(work, publisher, options, NullLogger.Instance); + var run = pool.RunAsync(cts.Token); + + Assert.Equal( + ArticleEnqueueResult.Accepted, + await work.EnqueueAsync(new OverviewDbWorkItem("fail-a"u8.ToArray(), ""), CancellationToken.None)); + Assert.Equal( + ArticleEnqueueResult.Accepted, + await work.EnqueueAsync(new OverviewDbWorkItem("fail-b"u8.ToArray(), ""), CancellationToken.None)); + + using var wait = new CancellationTokenSource(TimeSpan.FromSeconds(5)); + while (publisher.Started < 1) + { + wait.Token.ThrowIfCancellationRequested(); + await Task.Delay(10, wait.Token); + } + + work.Complete(); + await cts.CancelAsync(); + gate.TrySetResult(); + await run.WaitAsync(TimeSpan.FromSeconds(5)); + + // First item's drain retry fails (cannot requeue after Complete); second item still publishes. + Assert.Equal(1, publisher.ConfirmCount); + Assert.Equal(1, publisher.DrainFailureCount); Assert.Equal(0, work.Count); } @@ -313,6 +362,46 @@ public void AbandonOutstanding() } } + private sealed class GatedFailOnceOverviewDbHandoffPublisher(TaskCompletionSource gate) : IOverviewDbHandoffPublisher + { + private int _started; + private int _confirms; + private int _drainFailures; + private int _postGateFailures; + + public int Started => Volatile.Read(ref _started); + + public int ConfirmCount => Volatile.Read(ref _confirms); + + public int DrainFailureCount => Volatile.Read(ref _drainFailures); + + public int OutstandingCount => 0; + + public async Task PublishAsync(OverviewDbWorkItem item, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(item); + Interlocked.Increment(ref _started); + await gate.Task.WaitAsync(cancellationToken).ConfigureAwait(false); + if (Interlocked.CompareExchange(ref _postGateFailures, 1, 0) == 0) + { + Interlocked.Increment(ref _drainFailures); + throw new InvalidOperationException("simulated OverviewDB drain publish failure"); + } + + Interlocked.Increment(ref _confirms); + } + + public bool TryDequeuePublishFailure(out OverviewDbWorkItem item) + { + item = null!; + return false; + } + + public void AbandonOutstanding() + { + } + } + private sealed class CollectingLogger : ILogger { private readonly object _gate = new(); diff --git a/tests/VectorNNTPD.ReaderService.Tests/RabbitMq/RabbitMqServiceTests.cs b/tests/VectorNNTPD.ReaderService.Tests/RabbitMq/RabbitMqServiceTests.cs index a2a993cb..ea4c15d8 100644 --- a/tests/VectorNNTPD.ReaderService.Tests/RabbitMq/RabbitMqServiceTests.cs +++ b/tests/VectorNNTPD.ReaderService.Tests/RabbitMq/RabbitMqServiceTests.cs @@ -194,14 +194,23 @@ public async Task Reconnect_FailsThenSucceeds() await service.StartAsync(CancellationToken.None); var first = factory.LastConnection!; factory.RemainingConnectFailures = 2; - var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - factory.Connected = recovered; + // Connected fires when the factory returns a connection; generation is published after that. + // Wait on ConnectionReplaced so the assert observes the installed generation. + var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + service.ConnectionReplaced += (_, args) => + { + if (args.IsReplacement) + { + recovered.TrySetResult(args.ConnectionGeneration); + } + }; first.SimulateLost(); using var safety = new CancellationTokenSource(TimeSpan.FromSeconds(2)); - await recovered.Task.WaitAsync(safety.Token); + var generation = await recovered.Task.WaitAsync(safety.Token); + Assert.Equal(2, generation); Assert.Equal(2, service.ConnectionGeneration); Assert.True(service.IsReady); Assert.Equal(1 + 2 + 1, factory.AttemptCount); @@ -219,8 +228,16 @@ public async Task Reconnect_Continues_BeyondFormerAbandonThreshold() await service.StartAsync(CancellationToken.None); var failures = formerAbandonThreshold + 3; factory.RemainingConnectFailures = failures; - var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - factory.Connected = recovered; + // Connected fires when the factory returns a connection; generation is published after that. + // Wait on ConnectionReplaced so the assert observes the installed generation. + var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + service.ConnectionReplaced += (_, args) => + { + if (args.IsReplacement) + { + recovered.TrySetResult(args.ConnectionGeneration); + } + }; factory.LastConnection!.SimulateLost(); using var safety = new CancellationTokenSource(TimeSpan.FromSeconds(5)); @@ -229,8 +246,9 @@ public async Task Reconnect_Continues_BeyondFormerAbandonThreshold() Assert.Equal(1, service.ConnectionGeneration); Assert.False(service.Execution!.IsCompleted); - await recovered.Task.WaitAsync(safety.Token); + var generation = await recovered.Task.WaitAsync(safety.Token); + Assert.Equal(2, generation); Assert.Equal(2, service.ConnectionGeneration); Assert.True(service.IsReady); Assert.Equal(1 + failures + 1, factory.AttemptCount); diff --git a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs index 502dabeb..861c85da 100644 --- a/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/ArticleIngestion/OverviewDbPublisherStageTests.cs @@ -274,12 +274,61 @@ public async Task PublisherPool_DrainsQueuedWorkOnComplete_BeforeExit() await Task.Delay(10, wait.Token); } + // Complete first, then cancel while the in-flight publish is still gated. + // Production must finish that item (and drain the queued peer) after Complete(), + // not abandon the in-flight publish when the worker token cancels. work.Complete(); - gate.TrySetResult(); await cts.CancelAsync(); + gate.TrySetResult(); await run.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(2, publisher.ConfirmCount); + // At least one attempt for each accepted item. A cancel-vs-gate race may skip the + // drain retry for the first item when WaitAsync observes the completed gate first. + Assert.InRange(publisher.Started, 2, 3); + Assert.Equal(0, work.Count); + } + + [Fact] + public async Task PublisherPool_DrainPublishFailure_AfterComplete_ContinuesDrainWithoutHang() + { + var work = new OverviewDbWorkQueue(memoryLimitBytes: 1024 * 1024); + var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var publisher = new GatedFailOnceOverviewDbHandoffPublisher(gate); + var options = new ArticleIngestionOptions + { + OverviewDbMinPublisherWorkers = 1, + OverviewDbMaxPublisherWorkers = 1, + ScaleIntervalSeconds = 3600, + OverviewDbPublisherShutdownSeconds = 2, + }; + + using var cts = new CancellationTokenSource(); + var pool = new OverviewDbPublisherPool(work, publisher, options, NullLogger.Instance); + var run = pool.RunAsync(cts.Token); + + Assert.Equal( + ArticleEnqueueResult.Accepted, + await work.EnqueueAsync(new OverviewDbWorkItem("fail-a"u8.ToArray(), ""), CancellationToken.None)); + Assert.Equal( + ArticleEnqueueResult.Accepted, + await work.EnqueueAsync(new OverviewDbWorkItem("fail-b"u8.ToArray(), ""), CancellationToken.None)); + + using var wait = new CancellationTokenSource(TimeSpan.FromSeconds(5)); + while (publisher.Started < 1) + { + wait.Token.ThrowIfCancellationRequested(); + await Task.Delay(10, wait.Token); + } + + work.Complete(); + await cts.CancelAsync(); + gate.TrySetResult(); + await run.WaitAsync(TimeSpan.FromSeconds(5)); + + // First item's drain retry fails (cannot requeue after Complete); second item still publishes. + Assert.Equal(1, publisher.ConfirmCount); + Assert.Equal(1, publisher.DrainFailureCount); Assert.Equal(0, work.Count); } @@ -313,6 +362,46 @@ public void AbandonOutstanding() } } + private sealed class GatedFailOnceOverviewDbHandoffPublisher(TaskCompletionSource gate) : IOverviewDbHandoffPublisher + { + private int _started; + private int _confirms; + private int _drainFailures; + private int _postGateFailures; + + public int Started => Volatile.Read(ref _started); + + public int ConfirmCount => Volatile.Read(ref _confirms); + + public int DrainFailureCount => Volatile.Read(ref _drainFailures); + + public int OutstandingCount => 0; + + public async Task PublishAsync(OverviewDbWorkItem item, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(item); + Interlocked.Increment(ref _started); + await gate.Task.WaitAsync(cancellationToken).ConfigureAwait(false); + if (Interlocked.CompareExchange(ref _postGateFailures, 1, 0) == 0) + { + Interlocked.Increment(ref _drainFailures); + throw new InvalidOperationException("simulated OverviewDB drain publish failure"); + } + + Interlocked.Increment(ref _confirms); + } + + public bool TryDequeuePublishFailure(out OverviewDbWorkItem item) + { + item = null!; + return false; + } + + public void AbandonOutstanding() + { + } + } + private sealed class CollectingLogger : ILogger { private readonly object _gate = new(); diff --git a/tests/VectorNNTPD.TransitService.Tests/RabbitMq/RabbitMqServiceTests.cs b/tests/VectorNNTPD.TransitService.Tests/RabbitMq/RabbitMqServiceTests.cs index 7630e4fb..90d6fc67 100644 --- a/tests/VectorNNTPD.TransitService.Tests/RabbitMq/RabbitMqServiceTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/RabbitMq/RabbitMqServiceTests.cs @@ -194,14 +194,23 @@ public async Task Reconnect_FailsThenSucceeds() await service.StartAsync(CancellationToken.None); var first = factory.LastConnection!; factory.RemainingConnectFailures = 2; - var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - factory.Connected = recovered; + // Connected fires when the factory returns a connection; generation is published after that. + // Wait on ConnectionReplaced so the assert observes the installed generation. + var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + service.ConnectionReplaced += (_, args) => + { + if (args.IsReplacement) + { + recovered.TrySetResult(args.ConnectionGeneration); + } + }; first.SimulateLost(); using var safety = new CancellationTokenSource(TimeSpan.FromSeconds(2)); - await recovered.Task.WaitAsync(safety.Token); + var generation = await recovered.Task.WaitAsync(safety.Token); + Assert.Equal(2, generation); Assert.Equal(2, service.ConnectionGeneration); Assert.True(service.IsReady); Assert.Equal(1 + 2 + 1, factory.AttemptCount); @@ -219,8 +228,16 @@ public async Task Reconnect_Continues_BeyondFormerAbandonThreshold() await service.StartAsync(CancellationToken.None); var failures = formerAbandonThreshold + 3; factory.RemainingConnectFailures = failures; - var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - factory.Connected = recovered; + // Connected fires when the factory returns a connection; generation is published after that. + // Wait on ConnectionReplaced so the assert observes the installed generation. + var recovered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + service.ConnectionReplaced += (_, args) => + { + if (args.IsReplacement) + { + recovered.TrySetResult(args.ConnectionGeneration); + } + }; factory.LastConnection!.SimulateLost(); using var safety = new CancellationTokenSource(TimeSpan.FromSeconds(5)); @@ -229,8 +246,9 @@ public async Task Reconnect_Continues_BeyondFormerAbandonThreshold() Assert.Equal(1, service.ConnectionGeneration); Assert.False(service.Execution!.IsCompleted); - await recovered.Task.WaitAsync(safety.Token); + var generation = await recovered.Task.WaitAsync(safety.Token); + Assert.Equal(2, generation); Assert.Equal(2, service.ConnectionGeneration); Assert.True(service.IsReady); Assert.Equal(1 + failures + 1, factory.AttemptCount); diff --git a/tests/VectorNNTPD.TransitService.Tests/Storage/ArticlePlacementConnectionTests.cs b/tests/VectorNNTPD.TransitService.Tests/Storage/ArticlePlacementConnectionTests.cs index 574e7d38..671bad4f 100644 --- a/tests/VectorNNTPD.TransitService.Tests/Storage/ArticlePlacementConnectionTests.cs +++ b/tests/VectorNNTPD.TransitService.Tests/Storage/ArticlePlacementConnectionTests.cs @@ -3,6 +3,7 @@ using System.Net.Security; using System.Net.Sockets; using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Articles; using VectorNNTPD.Common.Transport.ArticleTransfer; @@ -226,7 +227,9 @@ public async Task Stream_ids_are_not_reused_on_a_live_connection() [Fact] public async Task Cancel_during_dial_does_not_open_a_second_connection() { - await using var server = StoreServer.Start(handshakeDelay: TimeSpan.FromMilliseconds(400)); + // Hold TLS handshake with an explicit gate so dial stays shared without racing the + // client's 2s TLS timeout (RSA cert creation + wall-clock delay was flaky under Linux CI load). + await using var server = StoreServer.Start(holdHandshake: true); await using var client = Client(maxOutstanding: 2); var target = Entry(server.Port); using var cts = new CancellationTokenSource(); @@ -234,11 +237,11 @@ public async Task Cancel_during_dial_does_not_open_a_second_connection() var secondArticle = CanonicalArticleText.CreateQueued("", InboundArticleProducer.TakeThis); var first = client.PlaceAsync(firstArticle.Record, target, cts.Token).AsTask(); var started = DateTime.UtcNow; - while (client.DialAttempts < 1) + while (server.AcceptedConnections < 1) { if (DateTime.UtcNow - started > TimeSpan.FromSeconds(2)) { - throw new TimeoutException("Dial did not start."); + throw new TimeoutException("Dial did not reach the held handshake."); } await Task.Delay(5); @@ -246,8 +249,10 @@ public async Task Cancel_during_dial_does_not_open_a_second_connection() var second = client.PlaceAsync(secondArticle.Record, target, CancellationToken.None).AsTask(); await cts.CancelAsync(); - var firstResult = await first; - var secondResult = await second; + server.ReleaseHandshake(); + using var safety = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + var firstResult = await first.WaitAsync(safety.Token); + var secondResult = await second.WaitAsync(safety.Token); Assert.Equal(ArticlePlacementKind.Cancelled, firstResult.Kind); Assert.Equal(ArticlePlacementKind.Accepted, secondResult.Kind); Assert.Equal(1, client.ConnectCount); @@ -407,11 +412,15 @@ private static StorageServerFleetEntry Entry(int port, string fqdn = "cache01.us private sealed class StoreServer : IAsyncDisposable { + private static readonly X509Certificate2 SharedServerCertificate = + VatpTestCertificates.CreateServerCertificate(); + private readonly TcpListener _listener; private readonly CancellationTokenSource _stop = new(); private readonly Task _accept; private readonly ConcurrentDictionary _streams = new(); private readonly ConcurrentQueue _streamIds = new(); + private readonly TaskCompletionSource? _handshakeHold; private int _outstanding; private int _peak; private int _stores; @@ -430,7 +439,8 @@ private StoreServer( int closeAfterAccepted, bool grantWindows, int initialCredit, - TimeSpan handshakeDelay) + TimeSpan handshakeDelay, + bool holdHandshake) { _listener = listener; HoldResult = holdResult; @@ -441,6 +451,11 @@ private StoreServer( GrantWindows = grantWindows; InitialCredit = initialCredit; HandshakeDelay = handshakeDelay; + if (holdHandshake) + { + _handshakeHold = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + } + Port = ((IPEndPoint)listener.LocalEndpoint).Port; _accept = Task.Run(AcceptLoop); } @@ -489,7 +504,8 @@ public static StoreServer Start( int closeAfterAccepted = 0, bool grantWindows = true, int initialCredit = 1024 * 1024, - TimeSpan handshakeDelay = default) + TimeSpan handshakeDelay = default, + bool holdHandshake = false) { var listener = new TcpListener(IPAddress.Loopback, 0); listener.Start(); @@ -502,7 +518,8 @@ public static StoreServer Start( closeAfterAccepted, grantWindows, initialCredit, - handshakeDelay); + handshakeDelay, + holdHandshake); } public void DropConnections() @@ -535,8 +552,11 @@ private static async Task WaitForAsync(Func read, int count, string message public void ReleaseResults() => ResultsReleased.TrySetResult(); + public void ReleaseHandshake() => _handshakeHold?.TrySetResult(); + public async ValueTask DisposeAsync() { + _handshakeHold?.TrySetCanceled(); await _stop.CancelAsync(); _listener.Stop(); try @@ -570,21 +590,24 @@ private async Task AcceptLoop() private async Task HandleAsync(TcpClient tcp) { - var certificate = VatpTestCertificates.CreateServerCertificate(); using var tcpDisposable = tcp; // codeql[cs/local-not-disposed] SslStream ownership transfers to the VATP/TLS session fixture for connection lifetime. var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false); _streams.TryAdd(ssl, 0); try { - if (HandshakeDelay > TimeSpan.Zero) + if (_handshakeHold is not null) + { + await _handshakeHold.Task.WaitAsync(_stop.Token); + } + else if (HandshakeDelay > TimeSpan.Zero) { await Task.Delay(HandshakeDelay, _stop.Token); } await ssl.AuthenticateAsServerAsync(new SslServerAuthenticationOptions { - ServerCertificate = certificate, + ServerCertificate = SharedServerCertificate, EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, }); var buffer = new byte[64 * 1024];