diff --git a/README.md b/README.md
index b92585e1..a73b419a 100644
--- a/README.md
+++ b/README.md
@@ -5,7 +5,7 @@ Standalone VectorNNTPD hosts extracted from VectorNNTP.NNTPD. This repository cu
## Structure
- `src/VectorNNTPD.BackFillerService/VectorNNTPD.BackFillerService.csproj` - BackFiller worker app (net10.0)
-- `src/VectorNNTPD.StorageService/VectorNNTPD.StorageService.csproj` - StorageService / VATP cache host (net10.0)
+- `src/VectorNNTPD.StorageService/VectorNNTPD.StorageService.csproj` - StorageService / NNTP TAKETHIS feed host (net10.0)
- `src/VectorNNTPD.Common/VectorNNTPD.Common.csproj` - shared library
- `tests/VectorNNTPD.BackFillerService.Tests/` / `tests/VectorNNTPD.StorageService.Tests/` / `tests/VectorNNTPD.Common.Tests/` - xUnit suites
- `VectorNNTPD.slnx` - solution including apps + tests
diff --git a/docs/common/vatp.md b/docs/common/vatp.md
index 6a113910..c3f2c479 100644
--- a/docs/common/vatp.md
+++ b/docs/common/vatp.md
@@ -193,17 +193,15 @@ and requires removal after terminalization before reuse.
NNTPD retrieval already opens many OPEN streams on one pooled TLS connection, up to
`MaxStreamsPerConnection` (64). BackFiller schedules their DATA frames one at a time on
-a ready ring. StorageServer accepts concurrent STORE streams on one connection the same
-way, and writes RESULT after the article is enqueued. `ArticlePlacementClient` keeps one TLS connection per target and sends at most
-`Nntpd:ArticleIngestion:MaxOutstandingStoresPerConnection` STOREs on it (default 2, allowed 1 or 2).
-One writer emits complete frames. A lost RESULT after END is acknowledgement-not-observed and is not
-retried. The spool writer still does not call placement. Retrieval OPEN multiplexing is unchanged.
+a ready ring. StorageService article *ingestion* is NNTP RFC 4644 `TAKETHIS` (not VATP STORE);
+the Common advertisement field `vatpPort` still names the TLS feed listen port for placement.
+Retrieval OPEN multiplexing is unchanged. VATP STORE multiplexing language that formerly
+described StorageServer ingestion no longer applies to this host.
## Not implemented yet (later phases)
- Numeric ARTICLE/HEAD/BODY/STAT (article-number / current-article forms)
- OverviewDB article-number resolution
-- Storage VATP consumers
- Outbound VATP server-to-server transfers
## NNTP message-id retrieval (Phase 3B)
diff --git a/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs b/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs
index 183e153b..60297944 100644
--- a/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs
+++ b/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs
@@ -6,7 +6,7 @@ namespace VectorNNTPD.StorageService.Configuration
///
///
///
- /// VATP RESULT is still sent when an article enters AcceptedArticleQueue.
+ /// TAKETHIS 239 is still sent when an article enters AcceptedArticleQueue.
/// These settings bound later disk work. They do not make that RESULT durable.
///
///
diff --git a/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs b/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs
index 8db90224..6b21d95b 100644
--- a/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs
+++ b/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs
@@ -112,7 +112,7 @@ ServerId is { } serverId
public int BindPort { get; set; }
///
- /// Gets or sets the TLS TCP port used by the VATP listener.
+ /// Gets or sets the TLS TCP port used by the NNTP feed listener.
///
///
/// Required. Range 1–65535. StorageServer is TLS-only; there is no cleartext fallback.
diff --git a/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs b/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs
index ea542980..42086907 100644
--- a/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs
+++ b/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs
@@ -35,7 +35,7 @@ public static class StorageServerServiceCollectionExtensions
///
///
/// Application service start order:
- /// NntpDB → nntpsharedconfig → Cloudflare DNS → RabbitMQ (connectivity only) → ACME → segment-pool validation → spool shard initialization → article index → block-device monitor → accepted-article worker → VATP listener → AdvertisementPublisher.
+ /// NntpDB → nntpsharedconfig → Cloudflare DNS → RabbitMQ (connectivity only) → ACME → segment-pool validation → spool shard initialization → article index → block-device monitor → accepted-article worker → NNTP feed listener → AdvertisementPublisher.
/// Shutdown is that order reversed, so advertisement stops before the listener, and the listener completes the queue before the worker stops.
/// Topology, queues, and consumers are not registered here. NntpDB does not migrate schema.
///
@@ -138,8 +138,6 @@ public static HostApplicationBuilder AddStorageServerHosting(this HostApplicatio
builder.Services.TryAddSingleton();
builder.Services.TryAddSingleton();
builder.Services.TryAddSingleton();
- builder.Services.TryAddSingleton();
-
builder.Services.TryAddSingleton(static sp =>
new DelegateRabbitMqConnectionNameProvider(() =>
RabbitMqRuntimeOptions.GetDefaultConnectionName(
@@ -204,19 +202,33 @@ public static HostApplicationBuilder AddStorageServerHosting(this HostApplicatio
{
var listener = provider.GetRequiredService>().Value.Listener;
var dns = provider.GetRequiredService();
- return new StorageVatpListenerService(
+ var index = provider.GetService();
+ Func? articlePresent = index is null
+ ? null
+ : id =>
+ {
+ try
+ {
+ return index.IsReady && index.Contains(id);
+ }
+ catch (InvalidOperationException)
+ {
+ return false;
+ }
+ };
+ return new StorageNntpListenerService(
provider.GetRequiredService(),
provider.GetRequiredService(),
- provider.GetRequiredService(),
provider.GetRequiredService(),
- provider.GetRequiredService>(),
+ provider.GetRequiredService>(),
provider.GetRequiredService(),
new StoreAssemblyAdmission(listener.MaxActiveConnections),
+ articlePresent: articlePresent,
publishAfterBind: dns.PublishAfterListenerBoundAsync);
});
builder.Services.TryAddEnumerable(
- ServiceDescriptor.Singleton(static provider =>
- provider.GetRequiredService()));
+ ServiceDescriptor.Singleton(static provider =>
+ provider.GetRequiredService()));
builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton(static sp =>
sp.GetRequiredService()));
diff --git a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs
index dae55b45..d8b6f8b1 100644
--- a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs
+++ b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs
@@ -4,7 +4,7 @@ namespace VectorNNTPD.StorageService.Ingress
{
///
- /// One accepted VATP article handed to in-memory consumers before Path rewrite.
+ /// One accepted feed article handed to in-memory consumers before Path rewrite.
///
/// Article identity from the accepted inbound bytes.
/// Hash of , matching the inbound META hash.
diff --git a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs
index 6ff87ac6..8646178a 100644
--- a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs
+++ b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs
@@ -15,7 +15,7 @@ namespace VectorNNTPD.StorageService.Ingress
///
///
/// The default budget is one gibibyte. At 768,000-byte articles that is about 1,398 articles.
- /// VATP RESULT is sent only after a true enqueue. A false enqueue is the overload signal
+ /// TAKETHIS 239 is sent only after a true enqueue. A false enqueue is the overload signal
/// and is not a durable-storage acknowledgement.
///
///
diff --git a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs
index 46225c58..98bd3246 100644
--- a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs
+++ b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs
@@ -22,7 +22,7 @@ namespace VectorNNTPD.StorageService.Ingress
/// Each worker holds at most one dequeued article. A waiting-for-capacity result keeps that
/// article off the queue and retries after .
/// Shutdown cancels in-flight publishes. An article already dequeued and not yet published is
- /// logged and dropped. VATP RESULT was sent at enqueue and is not repeated here.
+ /// logged and dropped. TAKETHIS 239 was sent at enqueue and is not repeated here.
///
///
internal sealed class AcceptedArticleWorker : IApplicationService
diff --git a/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs b/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs
index 143044ba..1b5601b2 100644
--- a/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs
+++ b/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs
@@ -9,7 +9,7 @@ namespace VectorNNTPD.StorageService.Ingress
public readonly record struct AcceptedArticleDelivery(bool HasArticle, AcceptedArticle Article);
///
- /// In-memory handoff of accepted VATP articles. Multiple sessions may enqueue,
+ /// In-memory handoff of accepted feed articles. Multiple sessions may enqueue,
/// and multiple consumers may dequeue. The queue does not persist articles.
///
public interface IAcceptedArticleQueue
diff --git a/src/VectorNNTPD.StorageService/Listener/IStorageArticleOpenBoundary.cs b/src/VectorNNTPD.StorageService/Listener/IStorageArticleOpenBoundary.cs
deleted file mode 100644
index c54516cf..00000000
--- a/src/VectorNNTPD.StorageService/Listener/IStorageArticleOpenBoundary.cs
+++ /dev/null
@@ -1,78 +0,0 @@
-using VectorNNTPD.Common.Articles;
-using VectorNNTPD.Common.Articles.Parsing;
-
-namespace VectorNNTPD.StorageService.Listener
-{
-
- ///
- /// Boundary between the VATP OPEN path and article bytes.
- ///
- ///
- /// Spool files are not an OPEN index. The host registration still rejects every OPEN.
- /// RequestId is correlation metadata. Null and rejecting implementations remain for tests.
- ///
- public interface IStorageArticleOpenBoundary
- {
- ///
- /// Attempts to open a transfer for the given RequestId and ArticleId.
- ///
- /// Client OPEN RequestId. Not consulted as a storage key.
- /// Client OPEN ArticleId.
- ///
- /// Accepted result carrying the canonical record, or a rejected result with no record.
- ///
- StorageArticleOpenResult TryOpen(Guid requestId, in ArticleId articleId);
- }
-
- /// Result of an article-open attempt.
- /// Whether OPEN can be served.
- /// Optional human-readable rejection reason (ASCII-safe, no secrets).
- /// Canonical record when is true; otherwise default.
- /// Date-family header used to build VATP META when accepted.
- public readonly record struct StorageArticleOpenResult(
- bool Accepted,
- string? Reason = null,
- ArticleRecord Record = default,
- NntpArticleHeaderName SelectedDateHeaderName = NntpArticleHeaderName.Unknown)
- {
- /// Rejected open result. Does not carry an article record.
- public static StorageArticleOpenResult Rejected(string? reason = null) => new(false, reason);
-
- /// Accepted open result. The session sends META and DATA from .
- public static StorageArticleOpenResult Opened(
- ArticleRecord record,
- NntpArticleHeaderName selectedDateHeaderName) =>
- new(true, null, record, selectedDateHeaderName);
- }
-
- ///
- /// Default open boundary that always rejects OPEN requests.
- ///
- public sealed class RejectingStorageArticleOpenBoundary : IStorageArticleOpenBoundary
- {
- ///
- public StorageArticleOpenResult TryOpen(Guid requestId, in ArticleId articleId)
- {
- _ = requestId;
- _ = articleId;
- return StorageArticleOpenResult.Rejected("storage-not-implemented");
- }
- }
-
- ///
- /// Null-object open boundary that always rejects OPEN requests.
- ///
- public sealed class NullStorageArticleOpenBoundary : IStorageArticleOpenBoundary
- {
- /// Shared singleton instance.
- public static NullStorageArticleOpenBoundary Instance { get; } = new();
-
- ///
- public StorageArticleOpenResult TryOpen(Guid requestId, in ArticleId articleId)
- {
- _ = requestId;
- _ = articleId;
- return StorageArticleOpenResult.Rejected("storage-not-implemented");
- }
- }
-}
diff --git a/src/VectorNNTPD.StorageService/Listener/IStorageListenerTransport.cs b/src/VectorNNTPD.StorageService/Listener/IStorageListenerTransport.cs
new file mode 100644
index 00000000..a44a6e11
--- /dev/null
+++ b/src/VectorNNTPD.StorageService/Listener/IStorageListenerTransport.cs
@@ -0,0 +1,19 @@
+namespace VectorNNTPD.StorageService.Listener
+{
+ /// One established TLS transport owned by a StorageServer NNTP feed session.
+ public interface IStorageListenerTransport : IAsyncDisposable
+ {
+ /// Reads bytes. Zero means the peer closed.
+ ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken);
+
+ /// Writes bytes and returns how many were accepted.
+ ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken);
+
+ ///
+ /// Suspends or resumes the read no-progress timeout.
+ /// A TAKETHIS wait for queue capacity suspends it until that wait ends.
+ ///
+ /// to disarm the outstanding read timer.
+ void SetReadProgressSuspended(bool suspended) { }
+ }
+}
diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs
new file mode 100644
index 00000000..fb46ef76
--- /dev/null
+++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs
@@ -0,0 +1,844 @@
+using System.Buffers;
+using VectorNNTPD.Common.Articles;
+using VectorNNTPD.Common.Articles.Parsing;
+using VectorNNTPD.Common.Articles.Validation;
+using VectorNNTPD.Common.Core;
+using VectorNNTPD.StorageService.Configuration;
+using VectorNNTPD.StorageService.Ingress;
+
+namespace VectorNNTPD.StorageService.Listener
+{
+ ///
+ /// RFC 4644 STREAMING feed session over an established TLS transport.
+ /// Accepts CAPABILITIES, MODE STREAM, CHECK, TAKETHIS, and QUIT.
+ /// A successful TAKETHIS commits into before 239 is written.
+ ///
+ public sealed partial class StorageNntpFeedSession : IAsyncDisposable
+ {
+ private const int MaximumCommandOctets = 512;
+
+ ///
+ /// Initial destuffed-assembly rental. Grows on demand up to
+ /// . Avoids a 1 MiB pool rent for typical articles.
+ ///
+ private const int InitialAssemblyRentBytes = 64 * 1024;
+
+ private static readonly byte[] Greeting = "200 VectorNNTP StorageServer ready\r\n"u8.ToArray();
+ private static readonly byte[] CapabilitiesHeader = "101 Capability list follows\r\n"u8.ToArray();
+ private static readonly byte[] CapabilitiesBody = "VERSION 2\r\nSTREAMING\r\nIMPLEMENTATION VectorNNTP-StorageServer\r\n.\r\n"u8.ToArray();
+ private static readonly byte[] ModeStreamOk = "203 Streaming allowed\r\n"u8.ToArray();
+ private static readonly byte[] Closing = "205 Connection closing\r\n"u8.ToArray();
+ private static readonly byte[] SyntaxError = "501 Syntax error\r\n"u8.ToArray();
+ private static readonly byte[] UnknownCommand = "500 Unknown command\r\n"u8.ToArray();
+ private static readonly byte[] TemporaryFailure = "400 Temporary failure\r\n"u8.ToArray();
+
+ private static readonly byte[] CapabilitiesKeyword = "CAPABILITIES"u8.ToArray();
+ private static readonly byte[] ModeKeyword = "MODE"u8.ToArray();
+ private static readonly byte[] StreamKeyword = "STREAM"u8.ToArray();
+ private static readonly byte[] CheckKeyword = "CHECK"u8.ToArray();
+ private static readonly byte[] TakeThisKeyword = "TAKETHIS"u8.ToArray();
+ private static readonly byte[] QuitKeyword = "QUIT"u8.ToArray();
+
+ private readonly IStorageListenerTransport _transport;
+ private readonly StorageServerListenerRuntimeOptions _listener;
+ private readonly ILogger _logger;
+ private readonly IAcceptedArticleQueue? _acceptedArticles;
+ private readonly StoreAssemblyAdmission? _assemblyAdmission;
+ private readonly Func? _articlePresent;
+ private readonly NntpArticleParser _parser;
+ private byte[] _pending;
+ private readonly byte[] _readScratch;
+ private readonly byte[] _statusScratch;
+ private int _pendingCount;
+ private int _disposed;
+
+ /// Initializes a STREAMING feed session over an established transport.
+ public StorageNntpFeedSession(
+ IStorageListenerTransport transport,
+ StorageServerListenerRuntimeOptions listener,
+ ILogger logger,
+ string pathIdentity,
+ IAcceptedArticleQueue? acceptedArticles = null,
+ StoreAssemblyAdmission? assemblyAdmission = null,
+ Func? articlePresent = null)
+ {
+ ArgumentNullException.ThrowIfNull(transport);
+ ArgumentNullException.ThrowIfNull(listener);
+ ArgumentNullException.ThrowIfNull(logger);
+ ArgumentException.ThrowIfNullOrWhiteSpace(pathIdentity);
+ _transport = transport;
+ _listener = listener;
+ _logger = logger;
+ _acceptedArticles = acceptedArticles;
+ _assemblyAdmission = assemblyAdmission;
+ _articlePresent = articlePresent;
+ _parser = new NntpArticleParser(pathIdentity.Trim());
+ var capacity = Math.Max(listener.ParserAccumulationMaxBytes, MaximumCommandOctets);
+ _pending = new byte[MaximumCommandOctets];
+ _readScratch = new byte[Math.Min(capacity, 64 * 1024)];
+ _statusScratch = new byte[4 + 1 + NntpMessageIdValidation.MaxMessageIdLength + 2];
+ }
+
+ /// Sends the greeting, then reads commands until QUIT, peer close, or cancellation.
+ public async Task RunAsync(CancellationToken cancellationToken)
+ {
+ await WriteFullyAsync(Greeting, cancellationToken).ConfigureAwait(false);
+ while (!cancellationToken.IsCancellationRequested)
+ {
+ var command = await ReadCommandAsync(cancellationToken).ConfigureAwait(false);
+ if (command.Kind == CommandReadKind.Disconnected)
+ {
+ return;
+ }
+
+ if (command.Kind == CommandReadKind.Overlong)
+ {
+ await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false);
+ continue;
+ }
+
+ var line = command.Line.Span;
+ if (TryMatchKeyword(line, QuitKeyword, out var quitRest))
+ {
+ if (!quitRest.IsEmpty)
+ {
+ await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false);
+ continue;
+ }
+
+ await WriteFullyAsync(Closing, cancellationToken).ConfigureAwait(false);
+ return;
+ }
+
+ if (TryMatchKeyword(line, CapabilitiesKeyword, out var capsRest))
+ {
+ if (!capsRest.IsEmpty)
+ {
+ await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false);
+ continue;
+ }
+
+ await WriteFullyAsync(CapabilitiesHeader, cancellationToken).ConfigureAwait(false);
+ await WriteFullyAsync(CapabilitiesBody, cancellationToken).ConfigureAwait(false);
+ continue;
+ }
+
+ if (TryMatchModeStream(line, out var modeStreamSyntaxError))
+ {
+ await WriteFullyAsync(
+ modeStreamSyntaxError ? SyntaxError : ModeStreamOk,
+ cancellationToken)
+ .ConfigureAwait(false);
+ continue;
+ }
+
+ if (TryMatchKeyword(line, CheckKeyword, out var checkRest))
+ {
+ await HandleCheckAsync(SkipSpaces(checkRest), cancellationToken).ConfigureAwait(false);
+ continue;
+ }
+
+ if (TryMatchKeyword(line, TakeThisKeyword, out var takeRest))
+ {
+ // One heap copy of the message-id argument; spans cannot cross the TAKETHIS awaits.
+ var takeArg = SkipSpaces(takeRest).ToArray();
+ var keepOpen = await HandleTakeThisAsync(takeArg, cancellationToken).ConfigureAwait(false);
+ if (!keepOpen)
+ {
+ return;
+ }
+
+ continue;
+ }
+
+ await WriteFullyAsync(UnknownCommand, cancellationToken).ConfigureAwait(false);
+ }
+ }
+
+ ///
+ public ValueTask DisposeAsync()
+ {
+ if (Interlocked.Exchange(ref _disposed, 1) == 1)
+ {
+ return ValueTask.CompletedTask;
+ }
+
+ return ValueTask.CompletedTask;
+ }
+
+ private Task HandleCheckAsync(ReadOnlySpan argument, CancellationToken cancellationToken)
+ {
+ if (!TryParseMessageIdArgument(argument, out var messageId))
+ {
+ return WriteFullyAsync(SyntaxError, cancellationToken);
+ }
+
+ var alreadyHave = _articlePresent?.Invoke(ArticleId.FromMessageId(messageId)) == true;
+ return WriteStatusAsync(
+ alreadyHave ? (byte)'4' : (byte)'2',
+ (byte)'3',
+ (byte)'8',
+ messageId,
+ cancellationToken);
+ }
+
+ private async Task HandleTakeThisAsync(byte[] argument, CancellationToken cancellationToken)
+ {
+ if (!TryParseMessageIdArgument(argument, out var requestMessageId))
+ {
+ // Peer already started the multiline; drain it so the next command is not contaminated.
+ await DrainArticleAsync(cancellationToken).ConfigureAwait(false);
+ await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false);
+ return true;
+ }
+
+ // argument holds the message-id octets for the rest of this method (copied once in RunAsync).
+ var requestId = ArticleId.FromMessageId(requestMessageId);
+
+ StoreAssemblyAdmission? admission = null;
+ var gate = _assemblyAdmission;
+ if (gate is not null)
+ {
+ if (!gate.TryEnter())
+ {
+ await DrainArticleAsync(cancellationToken).ConfigureAwait(false);
+ await WriteStatusAsync(
+ (byte)'4',
+ (byte)'3',
+ (byte)'9',
+ argument,
+ cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+
+ admission = gate;
+ }
+
+ byte[]? rented = null;
+ QueueReservation? reservation = null;
+ try
+ {
+ var destuffed = await ReadDestuffedArticleAsync(cancellationToken).ConfigureAwait(false);
+ rented = destuffed.Array;
+ var payload = destuffed.Memory;
+
+ if (_articlePresent?.Invoke(requestId) == true)
+ {
+ await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+
+ var created = ArticleRecordFactory.TryCreate(
+ _parser,
+ payload,
+ ArticlePathMode.Normalize);
+ if (!created.IsAccepted)
+ {
+ await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+
+ var record = created.Record;
+ if (!record.ArtId.Equals(requestId))
+ {
+ record.ReleaseUnusedCanonicalBuffer();
+ await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+
+ var queue = _acceptedArticles;
+ if (queue is null || queue.IsCompleted)
+ {
+ record.ReleaseUnusedCanonicalBuffer();
+ await WriteFullyAsync(TemporaryFailure, cancellationToken).ConfigureAwait(false);
+ return false;
+ }
+
+ if (record.ArtSize > queue.MaxQueuedBytes)
+ {
+ record.ReleaseUnusedCanonicalBuffer();
+ await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+
+ if (!queue.TryReserve(record.ArtSize, out var reserved))
+ {
+ _transport.SetReadProgressSuspended(true);
+ try
+ {
+ reservation = await queue.ReserveAsync(record.ArtSize, cancellationToken).ConfigureAwait(false);
+ }
+ finally
+ {
+ _transport.SetReadProgressSuspended(false);
+ }
+
+ if (reservation is null)
+ {
+ record.ReleaseUnusedCanonicalBuffer();
+ await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+ }
+ else
+ {
+ reservation = reserved;
+ }
+
+ var queued = new AcceptedArticle(record.ArtId, record.ArtHash, record.ArtSize, record.ArtData);
+ bool committed;
+ try
+ {
+ committed = queue.TryCommit(reservation.Value, queued);
+ }
+ catch
+ {
+ queue.Release(reservation.Value);
+ reservation = null;
+ record.ReleaseUnusedCanonicalBuffer();
+ throw;
+ }
+
+ reservation = null;
+ if (!committed)
+ {
+ record.ReleaseUnusedCanonicalBuffer();
+ await WriteFullyAsync(TemporaryFailure, cancellationToken).ConfigureAwait(false);
+ return false;
+ }
+
+ // ArtData remains reachable via the queued Memory; Exact buffers are not pooled.
+ if (_logger.IsEnabled(LogLevel.Debug))
+ {
+ StorageNntpFeedSessionLogMessages.ArticleAccepted(
+ _logger,
+ record.ArtId.ToLowerHexString());
+ }
+
+ await WriteStatusAsync((byte)'2', (byte)'3', (byte)'9', argument, cancellationToken)
+ .ConfigureAwait(false);
+ return true;
+ }
+ catch (EndOfStreamException)
+ {
+ if (reservation is { } held)
+ {
+ _acceptedArticles?.Release(held);
+ }
+
+ throw;
+ }
+ catch (InvalidOperationException)
+ {
+ // Oversized article or unrecoverable framing residue: RFC 4644 temporary failure + close.
+ if (reservation is { } held)
+ {
+ _acceptedArticles?.Release(held);
+ }
+
+ await WriteFullyAsync(TemporaryFailure, cancellationToken).ConfigureAwait(false);
+ return false;
+ }
+ finally
+ {
+ if (rented is not null)
+ {
+ ArrayPool.Shared.Return(rented);
+ }
+
+ admission?.Exit();
+ }
+ }
+
+ private async Task DrainArticleAsync(CancellationToken cancellationToken)
+ {
+ try
+ {
+ var destuffed = await ReadDestuffedArticleAsync(cancellationToken).ConfigureAwait(false);
+ if (destuffed.Array is not null)
+ {
+ ArrayPool.Shared.Return(destuffed.Array);
+ }
+ }
+ catch (EndOfStreamException ex)
+ {
+ ExpectedException.Ignore(ex);
+ }
+ }
+
+ private Task<(byte[]? Array, ReadOnlyMemory Memory)> ReadDestuffedArticleAsync(
+ CancellationToken cancellationToken)
+ => ReadDestuffedCoreAsync(ArticleResourceLimits.MaxArticleBytes, cancellationToken);
+
+ private async Task<(byte[]? Array, ReadOnlyMemory Memory)> ReadDestuffedCoreAsync(
+ int maxBytes,
+ CancellationToken cancellationToken)
+ {
+ var rented = ArrayPool.Shared.Rent(Math.Min(maxBytes, InitialAssemblyRentBytes));
+ var written = 0;
+ var atLineStart = true;
+ // Prefer the session scratch buffer so typical articles do not rent a second read array.
+ byte[]? rentedInput = null;
+ byte[] input;
+ if (_pendingCount > _readScratch.Length)
+ {
+ rentedInput = ArrayPool.Shared.Rent(_pendingCount);
+ input = rentedInput;
+ }
+ else
+ {
+ input = _readScratch;
+ }
+
+ var inputCount = 0;
+ var inputOffset = 0;
+ try
+ {
+ // Seed from any leftover command-buffer bytes that follow the TAKETHIS line.
+ if (_pendingCount > 0)
+ {
+ _pending.AsSpan(0, _pendingCount).CopyTo(input);
+ inputCount = _pendingCount;
+ _pendingCount = 0;
+ }
+
+ while (true)
+ {
+ if (inputCount - inputOffset == 0)
+ {
+ inputOffset = 0;
+ inputCount = await _transport.ReadAsync(input.AsMemory(), cancellationToken).ConfigureAwait(false);
+ if (inputCount == 0)
+ {
+ throw new EndOfStreamException("NNTP TAKETHIS ended before multiline terminator.");
+ }
+ }
+
+ var span = input.AsSpan(inputOffset, inputCount - inputOffset);
+ var index = 0;
+ var needMore = false;
+ while (index < span.Length)
+ {
+ if (atLineStart && span[index] == (byte)'.')
+ {
+ if (index + 1 >= span.Length
+ || (span[index + 1] == (byte)'\r' && index + 2 >= span.Length))
+ {
+ needMore = true;
+ break;
+ }
+
+ var next = span[index + 1];
+ if (next == (byte)'\n')
+ {
+ inputOffset += index + 2;
+ PreserveLeftover(input, inputOffset, inputCount - inputOffset);
+ ReturnRentedInput(rentedInput);
+ return (rented, rented.AsMemory(0, written));
+ }
+
+ if (next == (byte)'\r' && span[index + 2] == (byte)'\n')
+ {
+ inputOffset += index + 3;
+ PreserveLeftover(input, inputOffset, inputCount - inputOffset);
+ ReturnRentedInput(rentedInput);
+ return (rented, rented.AsMemory(0, written));
+ }
+
+ index += 1;
+ atLineStart = false;
+ continue;
+ }
+
+ var start = index;
+ do
+ {
+ var current = span[index++];
+ atLineStart = current is (byte)'\r' or (byte)'\n';
+ }
+ while (index < span.Length && !(atLineStart && span[index] == (byte)'.'));
+
+ var slice = span[start..index];
+ if (written > maxBytes - slice.Length)
+ {
+ throw new InvalidOperationException("NNTP TAKETHIS article exceeds maximum size.");
+ }
+
+ if (written + slice.Length > rented.Length)
+ {
+ var grown = ArrayPool.Shared.Rent(
+ Math.Min(maxBytes, Math.Max(rented.Length * 2, written + slice.Length)));
+ rented.AsSpan(0, written).CopyTo(grown);
+ ArrayPool.Shared.Return(rented);
+ rented = grown;
+ }
+
+ slice.CopyTo(rented.AsSpan(written));
+ written += slice.Length;
+ }
+
+ inputOffset += index;
+ if (needMore)
+ {
+ var leftover = inputCount - inputOffset;
+ if (leftover > 0 && inputOffset > 0)
+ {
+ Buffer.BlockCopy(input, inputOffset, input, 0, leftover);
+ }
+
+ inputOffset = 0;
+ inputCount = leftover;
+ var read = await _transport
+ .ReadAsync(input.AsMemory(inputCount, input.Length - inputCount), cancellationToken)
+ .ConfigureAwait(false);
+ if (read == 0)
+ {
+ throw new EndOfStreamException("NNTP TAKETHIS ended before multiline terminator.");
+ }
+
+ inputCount += read;
+ }
+ }
+ }
+ catch
+ {
+ ArrayPool.Shared.Return(rented);
+ ReturnRentedInput(rentedInput);
+ throw;
+ }
+ }
+
+ private static void ReturnRentedInput(byte[]? rentedInput)
+ {
+ if (rentedInput is not null)
+ {
+ ArrayPool.Shared.Return(rentedInput);
+ }
+ }
+
+ private void PreserveLeftover(byte[] input, int offset, int count)
+ {
+ if (count <= 0)
+ {
+ return;
+ }
+
+ // Pipelined TAKETHIS may leave the next command plus article bytes after the terminator.
+ // Grow beyond the 512-octet command-line limit so residue is not discarded.
+ if (count > _pending.Length)
+ {
+ var grown = GC.AllocateUninitializedArray(Math.Max(count, _pending.Length * 2));
+ _pending.AsSpan(0, _pendingCount).CopyTo(grown);
+ _pending = grown;
+ }
+
+ input.AsSpan(offset, count).CopyTo(_pending);
+ _pendingCount = count;
+ }
+
+ private async Task ReadCommandAsync(CancellationToken cancellationToken)
+ {
+ while (true)
+ {
+ var span = _pending.AsSpan(0, _pendingCount);
+ var crlf = IndexOfCrlf(span);
+ if (crlf >= 0)
+ {
+ // RFC 3977: command line including CRLF must not exceed 512 octets.
+ if (crlf + 2 > MaximumCommandOctets)
+ {
+ var consumedOverlong = crlf + 2;
+ if (consumedOverlong < _pendingCount)
+ {
+ Buffer.BlockCopy(_pending, consumedOverlong, _pending, 0, _pendingCount - consumedOverlong);
+ }
+
+ _pendingCount -= consumedOverlong;
+ return new CommandRead(CommandReadKind.Overlong, ReadOnlyMemory.Empty);
+ }
+
+ var consumed = crlf + 2;
+ ReadOnlyMemory line;
+ if (consumed < _pendingCount)
+ {
+ // Residue follows the CRLF (pipelined article bytes). Copy the command line
+ // before BlockCopy shifts that residue over offset 0.
+ line = _pending.AsSpan(0, crlf).ToArray();
+ Buffer.BlockCopy(_pending, consumed, _pending, 0, _pendingCount - consumed);
+ _pendingCount -= consumed;
+ }
+ else
+ {
+ // No residue: caller finishes using Line before the next read or PreserveLeftover.
+ line = _pending.AsMemory(0, crlf);
+ _pendingCount = 0;
+ }
+
+ return new CommandRead(CommandReadKind.Complete, line);
+ }
+
+ // No CRLF yet: overlong only when the first MaximumCommandOctets lack a terminator.
+ if (_pendingCount >= MaximumCommandOctets)
+ {
+ // Keep bytes after the first MaximumCommandOctets only once DrainUntilCrlf finishes.
+ _pendingCount = 0;
+ await DrainUntilCrlfAsync(cancellationToken).ConfigureAwait(false);
+ return new CommandRead(CommandReadKind.Overlong, ReadOnlyMemory.Empty);
+ }
+
+ EnsurePendingCapacity(_pendingCount + 1);
+ var read = await _transport
+ .ReadAsync(_pending.AsMemory(_pendingCount, _pending.Length - _pendingCount), cancellationToken)
+ .ConfigureAwait(false);
+ if (read == 0)
+ {
+ return new CommandRead(CommandReadKind.Disconnected, ReadOnlyMemory.Empty);
+ }
+
+ _pendingCount += read;
+ }
+ }
+
+ private void EnsurePendingCapacity(int minimumLength)
+ {
+ if (minimumLength <= _pending.Length)
+ {
+ return;
+ }
+
+ var grown = GC.AllocateUninitializedArray(Math.Max(minimumLength, _pending.Length * 2));
+ _pending.AsSpan(0, _pendingCount).CopyTo(grown);
+ _pending = grown;
+ }
+
+ private async Task DrainUntilCrlfAsync(CancellationToken cancellationToken)
+ {
+ var seenCr = false;
+ while (true)
+ {
+ var read = await _transport.ReadAsync(_readScratch, cancellationToken).ConfigureAwait(false);
+ if (read == 0)
+ {
+ return;
+ }
+
+ for (var i = 0; i < read; i++)
+ {
+ var b = _readScratch[i];
+ if (seenCr && b == (byte)'\n')
+ {
+ var remaining = read - i - 1;
+ if (remaining > 0)
+ {
+ EnsurePendingCapacity(remaining);
+ _readScratch.AsSpan(i + 1, remaining).CopyTo(_pending);
+ _pendingCount = remaining;
+ }
+
+ return;
+ }
+
+ seenCr = b == (byte)'\r';
+ }
+ }
+ }
+
+ private Task WriteStatusAsync(
+ byte hundreds,
+ byte tens,
+ byte ones,
+ byte[] messageIdArgument,
+ CancellationToken cancellationToken)
+ {
+ if (!TryParseMessageIdArgument(messageIdArgument, out var messageId))
+ {
+ return WriteFullyAsync(SyntaxError, cancellationToken);
+ }
+
+ return WriteStatusAsync(hundreds, tens, ones, messageId, cancellationToken);
+ }
+
+ private Task WriteStatusAsync(
+ byte hundreds,
+ byte tens,
+ byte ones,
+ ReadOnlySpan messageId,
+ CancellationToken cancellationToken)
+ {
+ var length = 4 + messageId.Length + 2;
+ if ((uint)length > (uint)_statusScratch.Length)
+ {
+ return WriteFullyAsync(SyntaxError, cancellationToken);
+ }
+
+ _statusScratch[0] = hundreds;
+ _statusScratch[1] = tens;
+ _statusScratch[2] = ones;
+ _statusScratch[3] = (byte)' ';
+ messageId.CopyTo(_statusScratch.AsSpan(4));
+ _statusScratch[4 + messageId.Length] = (byte)'\r';
+ _statusScratch[5 + messageId.Length] = (byte)'\n';
+ return WriteFullyAsync(_statusScratch.AsMemory(0, length), cancellationToken);
+ }
+
+ private async Task WriteFullyAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken)
+ {
+ var remaining = buffer;
+ while (!remaining.IsEmpty)
+ {
+ var written = await _transport.WriteAsync(remaining, cancellationToken).ConfigureAwait(false);
+ if (written <= 0)
+ {
+ throw new IOException("Storage NNTP transport write returned no progress.");
+ }
+
+ remaining = remaining[written..];
+ }
+ }
+
+ private static bool TryParseMessageIdArgument(ReadOnlySpan argument, out ReadOnlySpan messageId)
+ {
+ argument = SkipSpaces(argument);
+ if (argument.IsEmpty)
+ {
+ messageId = default;
+ return false;
+ }
+
+ var end = 0;
+ while (end < argument.Length && argument[end] is not ((byte)' ' or (byte)'\t'))
+ {
+ end++;
+ }
+
+ if (end < argument.Length)
+ {
+ var rest = SkipSpaces(argument[end..]);
+ if (!rest.IsEmpty)
+ {
+ messageId = default;
+ return false;
+ }
+ }
+
+ messageId = argument[..end];
+ return NntpMessageIdValidation.IsValidMessageId(messageId);
+ }
+
+ private static bool TryMatchModeStream(ReadOnlySpan line, out bool syntaxError)
+ {
+ syntaxError = false;
+ if (!TryMatchKeyword(line, ModeKeyword, out var modeRest))
+ {
+ return false;
+ }
+
+ if (!TryMatchKeyword(SkipSpaces(modeRest), StreamKeyword, out var streamRest))
+ {
+ return false;
+ }
+
+ syntaxError = !streamRest.IsEmpty;
+ return true;
+ }
+
+ private static bool TryMatchKeyword(
+ ReadOnlySpan line,
+ ReadOnlySpan keyword,
+ out ReadOnlySpan rest)
+ {
+ if (line.Length < keyword.Length)
+ {
+ rest = default;
+ return false;
+ }
+
+ for (var i = 0; i < keyword.Length; i++)
+ {
+ if (ToAsciiUpper(line[i]) != keyword[i])
+ {
+ rest = default;
+ return false;
+ }
+ }
+
+ if (line.Length == keyword.Length)
+ {
+ rest = ReadOnlySpan.Empty;
+ return true;
+ }
+
+ var next = line[keyword.Length];
+ if (next is not ((byte)' ' or (byte)'\t'))
+ {
+ rest = default;
+ return false;
+ }
+
+ rest = line[(keyword.Length + 1)..];
+ return true;
+ }
+
+ private static ReadOnlySpan SkipSpaces(ReadOnlySpan value)
+ {
+ var i = 0;
+ while (i < value.Length && value[i] is (byte)' ' or (byte)'\t')
+ {
+ i++;
+ }
+
+ return value[i..];
+ }
+
+ private static int IndexOfCrlf(ReadOnlySpan span)
+ {
+ for (var i = 0; i < span.Length - 1; i++)
+ {
+ if (span[i] == (byte)'\r' && span[i + 1] == (byte)'\n')
+ {
+ return i;
+ }
+ }
+
+ return -1;
+ }
+
+ private static byte ToAsciiUpper(byte value) =>
+ value is >= (byte)'a' and <= (byte)'z' ? (byte)(value - 32) : value;
+
+ private enum CommandReadKind
+ {
+ Complete = 0,
+ Overlong = 1,
+ Disconnected = 2,
+ }
+
+ private readonly struct CommandRead
+ {
+ public CommandRead(CommandReadKind kind, ReadOnlyMemory line)
+ {
+ Kind = kind;
+ Line = line;
+ }
+
+ public CommandReadKind Kind { get; }
+ public ReadOnlyMemory Line { get; }
+ }
+ }
+
+ /// Source-generated StorageServer NNTP feed session logs.
+ internal static partial class StorageNntpFeedSessionLogMessages
+ {
+ [LoggerMessage(
+ EventId = 5450,
+ Level = LogLevel.Debug,
+ Message = "Storage NNTP TAKETHIS accepted artId={ArtId}")]
+ public static partial void ArticleAccepted(ILogger logger, string ArtId);
+ }
+}
diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpLogMessages.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerLogMessages.cs
similarity index 76%
rename from src/VectorNNTPD.StorageService/Listener/StorageVatpLogMessages.cs
rename to src/VectorNNTPD.StorageService/Listener/StorageNntpListenerLogMessages.cs
index 20d5a71d..b8dbce0c 100644
--- a/src/VectorNNTPD.StorageService/Listener/StorageVatpLogMessages.cs
+++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerLogMessages.cs
@@ -1,86 +1,86 @@
namespace VectorNNTPD.StorageService.Listener
{
- /// Source-generated StorageServer VATP listener logs.
- internal static partial class StorageVatpListenerLogMessages
+ /// Source-generated StorageServer NNTP listener logs.
+ internal static partial class StorageNntpListenerLogMessages
{
[LoggerMessage(
EventId = 5400,
Level = LogLevel.Information,
- Message = "Storage VATP Listener starting bindPortTls={BindPortTls} endpoints={EndpointCount}")]
+ Message = "Storage NNTP Listener starting bindPortTls={BindPortTls} endpoints={EndpointCount}")]
public static partial void Starting(ILogger logger, int BindPortTls, int EndpointCount);
[LoggerMessage(
EventId = 5401,
Level = LogLevel.Information,
- Message = "Storage VATP Listener bound endpoint={Endpoint} family={AddressFamily}")]
+ Message = "Storage NNTP Listener bound endpoint={Endpoint} family={AddressFamily}")]
public static partial void EndpointBound(ILogger logger, string Endpoint, string AddressFamily);
[LoggerMessage(
EventId = 5402,
Level = LogLevel.Information,
- Message = "Storage VATP Listener running sockets={SocketCount} bindPortTls={BindPortTls}")]
+ Message = "Storage NNTP Listener running sockets={SocketCount} bindPortTls={BindPortTls}")]
public static partial void Running(ILogger logger, int SocketCount, int BindPortTls);
[LoggerMessage(
EventId = 5403,
Level = LogLevel.Information,
- Message = "Storage VATP Listener retiring")]
+ Message = "Storage NNTP Listener retiring")]
public static partial void Retiring(ILogger logger);
[LoggerMessage(
EventId = 5404,
Level = LogLevel.Information,
- Message = "Storage VATP Listener stopped")]
+ Message = "Storage NNTP Listener stopped")]
public static partial void Stopped(ILogger logger);
[LoggerMessage(
EventId = 5405,
Level = LogLevel.Error,
- Message = "Storage VATP Listener failed to start: {Reason}")]
+ Message = "Storage NNTP Listener failed to start: {Reason}")]
public static partial void StartFailed(ILogger logger, string Reason);
[LoggerMessage(
EventId = 5406,
Level = LogLevel.Warning,
- Message = "Storage VATP Listener TLS handshake failed remote={Remote}")]
+ Message = "Storage NNTP Listener TLS handshake failed remote={Remote}")]
public static partial void HandshakeFailed(ILogger logger, string Remote);
[LoggerMessage(
EventId = 5407,
Level = LogLevel.Warning,
- Message = "Storage VATP Listener connection timed out stage={Stage} remote={Remote}")]
+ Message = "Storage NNTP Listener connection timed out stage={Stage} remote={Remote}")]
public static partial void ConnectionTimedOut(ILogger logger, string Stage, string Remote);
[LoggerMessage(
EventId = 5408,
Level = LogLevel.Debug,
- Message = "Storage VATP Listener accepted remote={Remote} active={Active}")]
+ Message = "Storage NNTP Listener accepted remote={Remote} active={Active}")]
public static partial void Accepted(ILogger logger, string Remote, int Active);
[LoggerMessage(
EventId = 5409,
Level = LogLevel.Warning,
- Message = "Storage VATP Listener rejected connection at capacity remote={Remote} max={Max}")]
+ Message = "Storage NNTP Listener rejected connection at capacity remote={Remote} max={Max}")]
public static partial void CapacityRejected(ILogger logger, string Remote, int Max);
[LoggerMessage(
EventId = 5410,
Level = LogLevel.Warning,
- Message = "Storage VATP Listener skipped unsupported wildcard family family={AddressFamily}: {Reason}")]
+ Message = "Storage NNTP Listener skipped unsupported wildcard family family={AddressFamily}: {Reason}")]
public static partial void WildcardFamilySkipped(ILogger logger, string AddressFamily, string Reason);
[LoggerMessage(
EventId = 5411,
Level = LogLevel.Warning,
- Message = "Storage VATP Listener connection failed remote={Remote} error={ErrorType}")]
+ Message = "Storage NNTP Listener connection failed remote={Remote} error={ErrorType}")]
public static partial void ConnectionFailed(ILogger logger, string Remote, string ErrorType);
/// Routine peer reset or transport tear-down after accept; not an operational failure.
[LoggerMessage(
EventId = 5412,
Level = LogLevel.Debug,
- Message = "Storage VATP Listener connection peer disconnect remote={Remote}")]
+ Message = "Storage NNTP Listener connection peer disconnect remote={Remote}")]
public static partial void ConnectionPeerDisconnected(ILogger logger, string Remote);
///
@@ -90,59 +90,59 @@ internal static partial class StorageVatpListenerLogMessages
[LoggerMessage(
EventId = 5413,
Level = LogLevel.Error,
- Message = "Storage VATP Listener connection drain observed unexpected task failure")]
+ Message = "Storage NNTP Listener connection drain observed unexpected task failure")]
public static partial void ConnectionDrainFailed(ILogger logger, Exception exception);
}
- /// Source-generated StorageServer VATP session logs.
- internal static partial class StorageVatpSessionLogMessages
+ /// Source-generated StorageServer NNTP session logs.
+ internal static partial class StorageNntpFeedSessionLogMessages
{
[LoggerMessage(
EventId = 5500,
Level = LogLevel.Debug,
- Message = "Storage VATP client HELLO accepted maxFramePayload={MaxFramePayload}")]
+ Message = "Storage NNTP client HELLO accepted maxFramePayload={MaxFramePayload}")]
public static partial void ClientHelloAccepted(ILogger logger, uint MaxFramePayload);
[LoggerMessage(
EventId = 5501,
Level = LogLevel.Debug,
- Message = "Storage VATP OPEN rejected streamId={StreamId} requestId={RequestId}")]
+ Message = "Storage NNTP OPEN rejected streamId={StreamId} requestId={RequestId}")]
public static partial void OpenRejected(ILogger logger, uint StreamId, Guid RequestId);
[LoggerMessage(
EventId = 5502,
Level = LogLevel.Debug,
- Message = "Storage VATP session closed")]
+ Message = "Storage NNTP session closed")]
public static partial void ConnectionClosed(ILogger logger);
[LoggerMessage(
EventId = 5503,
Level = LogLevel.Debug,
- Message = "Storage VATP OPEN accepted streamId={StreamId} requestId={RequestId}")]
+ Message = "Storage NNTP OPEN accepted streamId={StreamId} requestId={RequestId}")]
public static partial void OpenAccepted(ILogger logger, uint StreamId, Guid RequestId);
[LoggerMessage(
EventId = 5504,
Level = LogLevel.Debug,
- Message = "Storage VATP transfer completed streamId={StreamId}")]
+ Message = "Storage NNTP transfer completed streamId={StreamId}")]
public static partial void TransferCompleted(ILogger logger, uint StreamId);
[LoggerMessage(
EventId = 5505,
Level = LogLevel.Debug,
- Message = "Storage VATP transfer cancelled streamId={StreamId}")]
+ Message = "Storage NNTP transfer cancelled streamId={StreamId}")]
public static partial void TransferCancelled(ILogger logger, uint StreamId);
[LoggerMessage(
EventId = 5506,
Level = LogLevel.Trace,
- Message = "Storage VATP STORE completed artId={ArtId} outcome={Outcome} attempt=1")]
+ Message = "Storage NNTP STORE completed artId={ArtId} outcome={Outcome} attempt=1")]
public static partial void StoreCompleted(ILogger logger, string ArtId, string Outcome);
[LoggerMessage(
EventId = 5507,
Level = LogLevel.Warning,
- Message = "Storage VATP STORE failed artId={ArtId} failure={Failure} attempt=1")]
+ Message = "Storage NNTP STORE failed artId={ArtId} failure={Failure} attempt=1")]
public static partial void StoreFailed(ILogger logger, string ArtId, string Failure);
/// Logs that STORE body credit is withheld until queue capacity is reserved.
@@ -152,7 +152,7 @@ internal static partial class StorageVatpSessionLogMessages
[LoggerMessage(
EventId = 5508,
Level = LogLevel.Debug,
- Message = "Storage VATP STORE waiting for queue capacity streamId={StreamId} bytes={Bytes}")]
+ Message = "Storage NNTP STORE waiting for queue capacity streamId={StreamId} bytes={Bytes}")]
public static partial void StoreAdmissionWaiting(ILogger logger, uint StreamId, int Bytes);
}
}
diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpListenerService.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs
similarity index 81%
rename from src/VectorNNTPD.StorageService/Listener/StorageVatpListenerService.cs
rename to src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs
index 65542733..1e9d98ba 100644
--- a/src/VectorNNTPD.StorageService/Listener/StorageVatpListenerService.cs
+++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs
@@ -7,29 +7,27 @@
using VectorNNTPD.Common.Acme;
using VectorNNTPD.Common.Core;
using VectorNNTPD.Common.Networking.Certificates;
-using VectorNNTPD.Common.Transport.ArticleTransfer;
using VectorNNTPD.StorageService.Configuration;
+using VectorNNTPD.Common.Articles;
using VectorNNTPD.StorageService.Ingress;
using VectorNNTPD.Common.Networking.Listeners;
namespace VectorNNTPD.StorageService.Listener
{
-
///
- /// Process-wide VATP TLS listener: binds configured endpoints, authenticates TLS, and serves
- /// minimal VATP sessions. Does not own article storage.
+ /// Process-wide NNTP TLS listener: binds configured endpoints, authenticates TLS, and serves
+ /// RFC 4644 STREAMING feed sessions (CHECK/TAKETHIS). Does not own article storage.
///
- public sealed class StorageVatpListenerService : IApplicationService, IAsyncDisposable
+ public sealed class StorageNntpListenerService : IApplicationService, IAsyncDisposable
{
private readonly StorageServerRuntimeOptionsSource? _runtimeSource;
private StorageServerRuntimeOptions? _runtime;
private readonly ITlsCertificateContextProvider _certificates;
- private readonly IStorageArticleOpenBoundary _openBoundary;
private readonly IAcmeCertificateReadiness _readiness;
- private readonly ILogger _logger;
+ private readonly ILogger _logger;
private readonly IAcceptedArticleQueue? _acceptedArticles;
- private readonly StoreAssemblyAdmission? _storeAdmission;
- private readonly ArticleTransferLimits? _transferLimits;
+ private readonly StoreAssemblyAdmission? _assemblyAdmission;
+ private readonly Func? _articlePresent;
///
/// Publishes DNS after at least one socket is bound. Null skips publication.
@@ -42,7 +40,7 @@ public sealed class StorageVatpListenerService : IApplicationService, IAsyncDisp
private readonly CancellationTokenSource _runCts = new();
private Task? _acceptTask;
- private StorageVatpListenerState _state = StorageVatpListenerState.Created;
+ private StorageNntpListenerState _state = StorageNntpListenerState.Created;
private int _started;
private int _disposed;
private int _activeConnections;
@@ -54,41 +52,39 @@ public sealed class StorageVatpListenerService : IApplicationService, IAsyncDisp
internal static int LastAcceptedReceiveBuffer { get; private set; }
/// Initializes the Listener service for isolated tests with a pre-ready certificate gate.
- public StorageVatpListenerService(
+ public StorageNntpListenerService(
StorageServerRuntimeOptions runtime,
ITlsCertificateContextProvider certificates,
- IStorageArticleOpenBoundary openBoundary,
- ILogger logger,
- ArticleTransferLimits? transferLimits = null)
- : this(runtime, certificates, openBoundary, CreateReadyGate(), logger, transferLimits: transferLimits)
+ ILogger logger,
+ IAcceptedArticleQueue? acceptedArticles = null,
+ StoreAssemblyAdmission? assemblyAdmission = null,
+ Func? articlePresent = null)
+ : this(runtime, certificates, CreateReadyGate(), logger, acceptedArticles, assemblyAdmission, articlePresent)
{
}
/// Initializes the Listener service.
- public StorageVatpListenerService(
+ public StorageNntpListenerService(
StorageServerRuntimeOptions runtime,
ITlsCertificateContextProvider certificates,
- IStorageArticleOpenBoundary openBoundary,
IAcmeCertificateReadiness readiness,
- ILogger logger,
+ ILogger logger,
IAcceptedArticleQueue? acceptedArticles = null,
- StoreAssemblyAdmission? storeAdmission = null,
- ArticleTransferLimits? transferLimits = null,
+ StoreAssemblyAdmission? assemblyAdmission = null,
+ Func? articlePresent = null,
Func? publishAfterBind = null)
{
ArgumentNullException.ThrowIfNull(runtime);
ArgumentNullException.ThrowIfNull(certificates);
- ArgumentNullException.ThrowIfNull(openBoundary);
ArgumentNullException.ThrowIfNull(readiness);
ArgumentNullException.ThrowIfNull(logger);
_runtime = runtime;
_certificates = certificates;
- _openBoundary = openBoundary;
_readiness = readiness;
_logger = logger;
_acceptedArticles = acceptedArticles;
- _storeAdmission = storeAdmission;
- _transferLimits = transferLimits;
+ _assemblyAdmission = assemblyAdmission;
+ _articlePresent = articlePresent;
_publishAfterBind = publishAfterBind;
}
@@ -96,39 +92,30 @@ public StorageVatpListenerService(
/// Initializes the listener for the host. Runtime options are read when runs,
/// after nntpsharedconfig has been published.
///
- public StorageVatpListenerService(
+ public StorageNntpListenerService(
StorageServerRuntimeOptionsSource runtimeSource,
ITlsCertificateContextProvider certificates,
- IStorageArticleOpenBoundary openBoundary,
IAcmeCertificateReadiness readiness,
- ILogger logger,
+ ILogger logger,
IAcceptedArticleQueue? acceptedArticles,
- StoreAssemblyAdmission? storeAdmission,
- ArticleTransferLimits? transferLimits = null,
+ StoreAssemblyAdmission? assemblyAdmission,
+ Func? articlePresent = null,
Func? publishAfterBind = null)
{
ArgumentNullException.ThrowIfNull(runtimeSource);
ArgumentNullException.ThrowIfNull(certificates);
- ArgumentNullException.ThrowIfNull(openBoundary);
ArgumentNullException.ThrowIfNull(readiness);
ArgumentNullException.ThrowIfNull(logger);
_runtimeSource = runtimeSource;
_certificates = certificates;
- _openBoundary = openBoundary;
_readiness = readiness;
_logger = logger;
_acceptedArticles = acceptedArticles;
- _storeAdmission = storeAdmission;
- _transferLimits = transferLimits;
+ _assemblyAdmission = assemblyAdmission;
+ _articlePresent = articlePresent;
_publishAfterBind = publishAfterBind;
}
- ///
- /// Gets the per-stream transfer limits for sessions this listener creates.
- /// Null at construction uses , including the 1 MiB initial window.
- ///
- internal ArticleTransferLimits TransferLimits => _transferLimits ?? ArticleTransferLimits.Default;
-
private StorageServerRuntimeOptions Runtime =>
_runtime ?? throw new InvalidOperationException("StorageServer runtime options have not been published.");
@@ -140,13 +127,13 @@ private static IAcmeCertificateReadiness CreateReadyGate()
}
///
- public string Name => "StorageVatpListener";
+ public string Name => "StorageNntpListener";
///
public Task? Execution => _acceptTask;
/// Gets the local lifecycle state.
- public StorageVatpListenerState State
+ public StorageNntpListenerState State
{
get
{
@@ -182,7 +169,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
lock (_gate)
{
- _state = StorageVatpListenerState.Starting;
+ _state = StorageNntpListenerState.Starting;
}
try
@@ -193,26 +180,26 @@ public async Task StartAsync(CancellationToken cancellationToken)
if (!_readiness.IsReady)
{
throw new InvalidOperationException(
- "Storage VATP Listener cannot start because the ACME certificate is not ready. StorageServer is TLS-only and the listener must wait for a usable certificate.");
+ "Storage NNTP Listener cannot start because the ACME certificate is not ready. StorageServer is TLS-only and the listener must wait for a usable certificate.");
}
if (!_certificates.IsAvailable)
{
throw new InvalidOperationException(
- $"Storage VATP Listener cannot start because no TLS certificate is available from ACME state '{Runtime.CertificateDirectory}'.");
+ $"Storage NNTP Listener cannot start because no TLS certificate is available from ACME state '{Runtime.CertificateDirectory}'.");
}
var bindings = ListenEndpointPlanner.Plan(Runtime.BindAddressTokens, Runtime.BindPortTls);
if (bindings.Count == 0)
{
- throw new InvalidOperationException("Storage VATP Listener has no bind endpoints.");
+ throw new InvalidOperationException("Storage NNTP Listener has no bind endpoints.");
}
- StorageVatpListenerLogMessages.Starting(_logger, Runtime.BindPortTls, bindings.Count);
+ StorageNntpListenerLogMessages.Starting(_logger, Runtime.BindPortTls, bindings.Count);
BindEndpoints(bindings);
if (_listenSockets.Count == 0)
{
- throw new InvalidOperationException("Storage VATP Listener failed to bind any endpoint.");
+ throw new InvalidOperationException("Storage NNTP Listener failed to bind any endpoint.");
}
if (_publishAfterBind is not null)
@@ -222,19 +209,19 @@ public async Task StartAsync(CancellationToken cancellationToken)
lock (_gate)
{
- _state = StorageVatpListenerState.Running;
+ _state = StorageNntpListenerState.Running;
}
- StorageVatpListenerLogMessages.Running(_logger, _listenSockets.Count, Runtime.BindPortTls);
+ StorageNntpListenerLogMessages.Running(_logger, _listenSockets.Count, Runtime.BindPortTls);
_acceptTask = AcceptAllAsync(_runCts.Token);
}
catch (Exception ex)
{
- StorageVatpListenerLogMessages.StartFailed(_logger, ex.Message);
+ StorageNntpListenerLogMessages.StartFailed(_logger, ex.Message);
await DisposeBoundResourcesAsync().ConfigureAwait(false);
lock (_gate)
{
- _state = StorageVatpListenerState.Stopped;
+ _state = StorageNntpListenerState.Stopped;
}
Interlocked.Exchange(ref _started, 0);
@@ -258,10 +245,10 @@ public async ValueTask DisposeAsync()
lock (_gate)
{
- _state = StorageVatpListenerState.Retiring;
+ _state = StorageNntpListenerState.Retiring;
}
- StorageVatpListenerLogMessages.Retiring(_logger);
+ StorageNntpListenerLogMessages.Retiring(_logger);
await _runCts.CancelAsync().ConfigureAwait(false);
CloseListenSockets();
@@ -284,10 +271,10 @@ public async ValueTask DisposeAsync()
_runCts.Dispose();
lock (_gate)
{
- _state = StorageVatpListenerState.Stopped;
+ _state = StorageNntpListenerState.Stopped;
}
- StorageVatpListenerLogMessages.Stopped(_logger);
+ StorageNntpListenerLogMessages.Stopped(_logger);
}
private void BindEndpoints(IReadOnlyList bindings)
@@ -302,7 +289,7 @@ private void BindEndpoints(IReadOnlyList bindings)
_listenSockets.Add(socket);
}
- StorageVatpListenerLogMessages.EndpointBound(
+ StorageNntpListenerLogMessages.EndpointBound(
_logger,
binding.EndPoint.ToString() ?? string.Empty,
binding.Address.AddressFamily.ToString());
@@ -311,7 +298,7 @@ private void BindEndpoints(IReadOnlyList bindings)
IsImplicitWildcard(binding.EndPoint)
&& ex.SocketErrorCode is SocketError.AddressFamilyNotSupported or SocketError.ProtocolNotSupported or SocketError.AddressNotAvailable)
{
- StorageVatpListenerLogMessages.WildcardFamilySkipped(
+ StorageNntpListenerLogMessages.WildcardFamilySkipped(
_logger,
binding.Address.AddressFamily.ToString(),
ex.SocketErrorCode.ToString());
@@ -395,16 +382,16 @@ private async Task AcceptLoopAsync(Socket listenSocket, CancellationToken cancel
var remote = accepted.RemoteEndPoint?.ToString() ?? "";
if (!TryAdmit())
{
- StorageVatpListenerLogMessages.CapacityRejected(_logger, remote, Runtime.Listener.MaxActiveConnections);
+ StorageNntpListenerLogMessages.CapacityRejected(_logger, remote, Runtime.Listener.MaxActiveConnections);
accepted.Dispose();
continue;
}
- StorageVatpListenerLogMessages.Accepted(_logger, remote, ActiveConnections);
+ StorageNntpListenerLogMessages.Accepted(_logger, remote, ActiveConnections);
var connection = ProcessAsync(accepted, remote, cancellationToken);
_connections[connection] = 0;
_ = connection.ContinueWith(
- static (task, state) => ((StorageVatpListenerService)state!)._connections.TryRemove(task, out _),
+ static (task, state) => ((StorageNntpListenerService)state!)._connections.TryRemove(task, out _),
this,
TaskScheduler.Default);
}
@@ -440,19 +427,19 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke
}
handshakeCompleted = true;
- await using var transport = new StreamStorageVatpTransport(
+ await using var transport = new StreamStorageListenerTransport(
ssl,
Runtime.Listener.IoProgressTimeout,
leaveInnerStreamOpen: true);
- await using var vatpSession = new StorageVatpSession(
+ await using var session = new StorageNntpFeedSession(
transport,
- _openBoundary,
Runtime.Listener,
_logger,
- limits: _transferLimits,
+ Runtime.Fqdn,
acceptedArticles: _acceptedArticles,
- storeAdmission: _storeAdmission);
- await vatpSession.RunAsync(cancellationToken).ConfigureAwait(false);
+ assemblyAdmission: _assemblyAdmission,
+ articlePresent: _articlePresent);
+ await session.RunAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested)
{
@@ -460,11 +447,11 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke
}
catch (AuthenticationException)
{
- StorageVatpListenerLogMessages.HandshakeFailed(_logger, remote);
+ StorageNntpListenerLogMessages.HandshakeFailed(_logger, remote);
}
catch (TimeoutException ex)
{
- StorageVatpListenerLogMessages.ConnectionTimedOut(
+ StorageNntpListenerLogMessages.ConnectionTimedOut(
_logger,
handshakeCompleted ? "io-progress" : ex.Message,
remote);
@@ -472,11 +459,11 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke
catch (IOException)
{
// Routine peer reset / transport tear-down after accept; not an operational failure.
- StorageVatpListenerLogMessages.ConnectionPeerDisconnected(_logger, remote);
+ StorageNntpListenerLogMessages.ConnectionPeerDisconnected(_logger, remote);
}
catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex))
{
- StorageVatpListenerLogMessages.ConnectionFailed(_logger, remote, ex.GetType().Name);
+ StorageNntpListenerLogMessages.ConnectionFailed(_logger, remote, ex.GetType().Name);
}
finally
{
@@ -568,13 +555,13 @@ private void LogUnexpectedDrainFailures(Task[] tasks, Exception awaitException)
}
logged = true;
- StorageVatpListenerLogMessages.ConnectionDrainFailed(_logger, inner);
+ StorageNntpListenerLogMessages.ConnectionDrainFailed(_logger, inner);
}
}
if (!logged)
{
- StorageVatpListenerLogMessages.ConnectionDrainFailed(_logger, awaitException);
+ StorageNntpListenerLogMessages.ConnectionDrainFailed(_logger, awaitException);
}
}
@@ -585,8 +572,8 @@ private Task DisposeBoundResourcesAsync()
}
}
- /// Local lifecycle states for the StorageServer VATP listener.
- public enum StorageVatpListenerState
+ /// Local lifecycle states for the StorageServer NNTP listener.
+ public enum StorageNntpListenerState
{
/// Created but not started.
Created = 0,
diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.Store.cs b/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.Store.cs
deleted file mode 100644
index 8eb7ddb4..00000000
--- a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.Store.cs
+++ /dev/null
@@ -1,642 +0,0 @@
-using System.Buffers;
-using System.Diagnostics;
-using VectorNNTPD.Common.Articles;
-using VectorNNTPD.Common.Transport.ArticleTransfer;
-using VectorNNTPD.StorageService.Ingress;
-
-namespace VectorNNTPD.StorageService.Listener
-{
-
- public sealed partial class StorageVatpSession
- {
- private async Task HandleStoreAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (!_clientHelloComplete)
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken)
- .ConfigureAwait(false);
- return false;
- }
-
- if (!VatpStorePayload.TryDecode(frame.Payload, out var articleId, out var decodeError))
- {
- await WriteFailAsync(streamId, decodeError, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (streamId == VatpProtocol.ConnectionStreamId
- || _streams.ContainsKey(streamId)
- || _storeStreams.ContainsKey(streamId)
- || _streams.Count + _storeStreams.Count >= _limits.MaxStreamsPerConnection)
- {
- await WriteFailAsync(
- streamId,
- streamId == VatpProtocol.ConnectionStreamId
- ? VatpErrorCode.InvalidStreamId
- : VatpErrorCode.StreamTableError,
- cancellationToken)
- .ConfigureAwait(false);
- return true;
- }
-
- // Capture the gate that TryEnter acquired. Do not re-read _storeAdmission on release.
- StoreAssemblyAdmission? acquiredAdmission = null;
- var admission = _storeAdmission;
- if (admission is not null)
- {
- if (!admission.TryEnter())
- {
- await WriteFailAsync(streamId, VatpErrorCode.StreamTableError, cancellationToken)
- .ConfigureAwait(false);
- return true;
- }
-
- acquiredAdmission = admission;
- }
-
- var receive = new ArticleTransferReceiveStream(
- streamId,
- StoreReceiveCorrelation,
- articleId,
- _limits);
- if (!_storeStreams.TryAdd(streamId, new StoreStream(receive, acquiredAdmission)))
- {
- acquiredAdmission?.Exit();
-
- await WriteFailAsync(streamId, VatpErrorCode.StreamTableError, cancellationToken)
- .ConfigureAwait(false);
- return true;
- }
-
- return true;
- }
-
- private async Task HandleStoreMetaAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (!_storeStreams.TryGetValue(streamId, out var store))
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameType, cancellationToken).ConfigureAwait(false);
- return false;
- }
-
- if (store.MetaSeen || store.BodyAuthorized || store.AdmissionTask is not null)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- Span metaBytes = stackalloc byte[VatpProtocol.MetaPayloadLength];
- if (frame.Payload.Length != VatpProtocol.MetaPayloadLength)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.InvalidMeta, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- frame.Payload.CopyTo(metaBytes);
- if (!VatpMetaCodec.TryDecode(metaBytes, out var meta, out var decodeError))
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, decodeError, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (meta.ArtSize < 1 || meta.ArtSize > ArticleResourceLimits.MaxArticleBytes)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.ArticleTooLarge, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var queue = _acceptedArticles;
- if (queue is null)
- {
- return await AcceptMetaWithoutReservationAsync(streamId, store, frame.Payload, meta, cancellationToken)
- .ConfigureAwait(false);
- }
-
- if (meta.ArtSize > queue.MaxQueuedBytes || queue.IsCompleted)
- {
- RemoveStore(streamId);
- if (meta.ArtSize > queue.MaxQueuedBytes && !queue.IsCompleted)
- {
- await WriteEncodedAsync(
- VatpFrameEncoder.EncodeResult(streamId, VatpStoreOutcome.RejectedCapacity),
- cancellationToken)
- .ConfigureAwait(false);
- return true;
- }
-
- await WriteFailAsync(streamId, VatpErrorCode.Cancelled, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (queue.TryReserve(meta.ArtSize, out var reserved))
- {
- return await AuthorizeReservedBodyAsync(streamId, store, frame.Payload, meta, reserved, cancellationToken)
- .ConfigureAwait(false);
- }
-
- var metaCopy = new byte[VatpProtocol.MetaPayloadLength];
- frame.Payload.CopyTo(metaCopy);
- lock (_storeGate)
- {
- if (store.MetaSeen || store.AdmissionTask is not null)
- {
- RemoveStore(streamId);
- }
- else
- {
- store.PendingMeta = metaCopy;
- store.AdmissionCts = CancellationTokenSource.CreateLinkedTokenSource(_runCancellation);
- // Log before starting the waiter. AdmissionWaiters can become visible as soon as
- // ReserveAsync blocks; observers that wait on waiters must already see this log.
- StorageVatpSessionLogMessages.StoreAdmissionWaiting(_logger, streamId, meta.ArtSize);
- store.AdmissionTask = AdmitWhenCapacityAsync(streamId, store, meta.ArtSize, store.AdmissionCts.Token);
- SyncAdmissionReadTimeout();
- }
- }
-
- if (store.AdmissionTask is null)
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- return true;
- }
-
- private async Task AdmitWhenCapacityAsync(
- uint streamId,
- StoreStream store,
- int bytes,
- CancellationToken cancellationToken)
- {
- var queue = _acceptedArticles;
- if (queue is null)
- {
- return;
- }
-
- QueueReservation? reservation = null;
- try
- {
- reservation = await queue.ReserveAsync(bytes, cancellationToken).ConfigureAwait(false);
- byte[]? metaCopy;
- lock (_storeGate)
- {
- if (store.Closed || !_storeStreams.ContainsKey(streamId) || store.PendingMeta is null)
- {
- metaCopy = null;
- }
- else if (reservation is null)
- {
- metaCopy = null;
- store.AdmissionFailed = true;
- }
- else
- {
- metaCopy = store.PendingMeta;
- store.ReservationHold.Bind(queue, reservation.Value);
- reservation = null;
- }
- }
-
- if (metaCopy is null)
- {
- if (reservation is { } abandoned)
- {
- queue.Release(abandoned);
- }
-
- if (store.AdmissionFailed && !store.Closed)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.Cancelled, CancellationToken.None).ConfigureAwait(false);
- }
-
- return;
- }
-
- if (!VatpMetaCodec.TryDecode(metaCopy, out var meta, out var decodeError))
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, decodeError, CancellationToken.None).ConfigureAwait(false);
- return;
- }
-
- await AuthorizeReservedBodyAsync(
- streamId,
- store,
- new ReadOnlySequence(metaCopy),
- meta,
- store.ReservationHold.Reservation,
- CancellationToken.None)
- .ConfigureAwait(false);
- }
- catch (OperationCanceledException)
- {
- if (reservation is { } abandoned)
- {
- queue.Release(abandoned);
- }
- }
- catch (Exception ex) when (ex is IOException or ObjectDisposedException)
- {
- if (reservation is { } abandoned)
- {
- queue.Release(abandoned);
- }
- }
- finally
- {
- lock (_storeGate)
- {
- SyncAdmissionReadTimeout();
- }
- }
- }
-
- ///
- /// Suspends the read no-progress timeout while a STORE is waiting for queue capacity.
- /// Resumes it when no such wait remains, including after WINDOW is granted.
- /// Caller holds .
- ///
- private void SyncAdmissionReadTimeout()
- {
- var pending = false;
- foreach (var store in _storeStreams.Values)
- {
- if (!store.Closed && !store.BodyAuthorized && store.AdmissionTask is { IsCompleted: false })
- {
- pending = true;
- break;
- }
- }
-
- _transport.SetReadProgressSuspended(pending);
- }
-
- private async Task AcceptMetaWithoutReservationAsync(
- uint streamId,
- StoreStream store,
- ReadOnlySequence payload,
- ArticleCanonicalTransferMeta meta,
- CancellationToken cancellationToken)
- {
- var apply = store.Receive.TryAcceptMeta(payload);
- if (!apply.Success)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, store.Receive.Failure, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- store.MetaArtHash = meta.ArtHash;
- store.MetaArtSize = meta.ArtSize;
- store.MetaSeen = true;
- store.BodyAuthorized = true;
- return true;
- }
-
- private async Task AuthorizeReservedBodyAsync(
- uint streamId,
- StoreStream store,
- ReadOnlySequence payload,
- ArticleCanonicalTransferMeta meta,
- QueueReservation reservation,
- CancellationToken cancellationToken)
- {
- // True when AdmitWhenCapacityAsync already bound this reservation before this call.
- // Combined with StoreReservationHold.TryRelease (exactly-once), prevents double Release
- // when RemoveStore races with this failure path.
- var reservationAlreadyBound = store.ReservationHold.IsBound;
- VatpErrorCode failure;
- lock (_storeGate)
- {
- if (store.Closed || store.BodyAuthorized)
- {
- failure = VatpErrorCode.InvalidStateTransition;
- }
- else
- {
- var apply = store.Receive.TryAcceptMeta(payload);
- if (!apply.Success)
- {
- failure = store.Receive.Failure;
- }
- else
- {
- store.ReservationHold.Bind(_acceptedArticles, reservation);
- store.MetaArtHash = meta.ArtHash;
- store.MetaArtSize = meta.ArtSize;
- store.MetaSeen = true;
- store.BodyAuthorized = true;
- Interlocked.Increment(ref StoreBodiesAllocated);
- failure = VatpErrorCode.None;
- }
- }
- }
-
- if (failure != VatpErrorCode.None)
- {
- if (!store.ReservationHold.TryRelease() && !reservationAlreadyBound)
- {
- // Unbound TryReserve/ReserveAsync slot still owned by this call.
- _acceptedArticles?.Release(reservation);
- }
-
- RemoveStore(streamId);
- await WriteFailAsync(streamId, failure, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var grant = _limits.InitialStreamWindowBytes;
- if (grant > 0)
- {
- await WriteEncodedAsync(
- VatpFrameEncoder.EncodeWindow(streamId, (uint)grant),
- cancellationToken)
- .ConfigureAwait(false);
- }
-
- return true;
- }
-
- private async Task HandleStoreDataAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (!_storeStreams.TryGetValue(streamId, out var store))
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameType, cancellationToken).ConfigureAwait(false);
- return false;
- }
-
- ArticleTransferApplyResult apply;
- var unauthorized = false;
- var dataStarted = Stopwatch.GetTimestamp();
- lock (_storeGate)
- {
- if (!store.BodyAuthorized || store.Closed)
- {
- unauthorized = true;
- apply = ArticleTransferApplyResult.Fail(VatpErrorCode.FlowControlViolation);
- }
- else
- {
- apply = store.Receive.TryAcceptData(frame.Payload, frame.Header.HasFin);
- }
- }
-
- if (unauthorized)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.FlowControlViolation, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- NoteDataAccept(
- Stopwatch.GetTimestamp() - dataStarted,
- apply.Success ? frame.Payload.Length : 0);
- if (!apply.Success)
- {
- var error = store.Receive.Failure;
- RemoveStore(streamId);
- await WriteFailAsync(streamId, error, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var payloadLength = checked((uint)frame.Payload.Length);
- if (payloadLength > 0 && store.Receive.TryAcceptWindow(payloadLength).Success)
- {
- var windowStarted = Stopwatch.GetTimestamp();
- await WriteEncodedAsync(VatpFrameEncoder.EncodeWindow(streamId, payloadLength), cancellationToken)
- .ConfigureAwait(false);
- NoteWindowWrite(Stopwatch.GetTimestamp() - windowStarted);
- }
-
- if (store.Receive.IsTerminal)
- {
- var error = store.Receive.Failure == VatpErrorCode.None
- ? VatpErrorCode.IncompleteTransfer
- : store.Receive.Failure;
- RemoveStore(streamId);
- await WriteFailAsync(streamId, error, cancellationToken).ConfigureAwait(false);
- }
-
- return true;
- }
-
- private async Task HandleStoreEndAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (!_storeStreams.TryGetValue(streamId, out var store))
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameType, cancellationToken).ConfigureAwait(false);
- return false;
- }
-
- var endStarted = Stopwatch.GetTimestamp();
- ArticleTransferApplyResult apply;
- var tookRecord = false;
- ArticleRecord record = default;
- lock (_storeGate)
- {
- apply = store.Receive.TryAcceptEnd();
- if (apply.Success)
- {
- tookRecord = store.Receive.TryTakeRecord(out record);
- }
- }
-
- NoteEndValidate(Stopwatch.GetTimestamp() - endStarted);
- if (!tookRecord)
- {
- var error = store.Receive.Failure == VatpErrorCode.None
- ? VatpErrorCode.IncompleteTransfer
- : store.Receive.Failure;
- RemoveStore(streamId);
- await WriteFailAsync(streamId, error, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var mismatch = Mismatch(store, record);
- if (mismatch != VatpErrorCode.None)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, mismatch, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (_acceptedArticles is null)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.OpenRejected, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var queued = new AcceptedArticle(record.ArtId, record.ArtHash, record.ArtSize, record.ArtData);
- var committed = false;
- // Take hold ownership before TryCommit so concurrent RemoveStore cannot Release
- // a reservation the queue has already consumed (or is consuming).
- if (store.ReservationHold.TryTakeForCommit(out var commitQueue, out var commitReservation))
- {
- try
- {
- committed = commitQueue!.TryCommit(commitReservation, queued);
- }
- catch
- {
- // TryCommit throws before mutating the reservation table; return the slot.
- commitQueue!.Release(commitReservation);
- store.ReservationHold.MarkCommitOutcome(false);
- RemoveStore(streamId);
- throw;
- }
-
- store.ReservationHold.MarkCommitOutcome(committed);
- }
-
- if (!committed)
- {
- RemoveStore(streamId);
- await WriteFailAsync(streamId, VatpErrorCode.IncompleteTransfer, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var resultStarted = Stopwatch.GetTimestamp();
- store.AcceptStarted = true;
- RemoveStore(streamId);
- StorageVatpSessionLogMessages.StoreCompleted(
- _logger,
- record.ArtId.ToLowerHexString(),
- "Accepted");
- await WriteEncodedAsync(
- VatpFrameEncoder.EncodeResult(streamId, VatpStoreOutcome.Accepted),
- cancellationToken)
- .ConfigureAwait(false);
- NoteEnqueueToResult(Stopwatch.GetTimestamp() - resultStarted);
- return true;
- }
-
- private static VatpErrorCode Mismatch(StoreStream store, in ArticleRecord record)
- {
- if (!store.MetaSeen)
- {
- return VatpErrorCode.IncompleteTransfer;
- }
-
- if (!record.ArtId.Equals(store.Receive.ExpectedArtId))
- {
- return VatpErrorCode.ArticleIdMismatch;
- }
-
- if (record.ArtHash != store.MetaArtHash)
- {
- return VatpErrorCode.ArtHashMismatch;
- }
-
- if (record.ArtSize != store.MetaArtSize)
- {
- return VatpErrorCode.ArtSizeMismatch;
- }
-
- return VatpErrorCode.None;
- }
-
- private void RemoveStore(uint streamId)
- {
- if (!_storeStreams.TryRemove(streamId, out var store))
- {
- return;
- }
-
- store.Closed = true;
- // Exit the admission instance that TryEnter acquired — never re-read _storeAdmission.
- store.ReleaseAcquiredAdmission();
-
- try
- {
- store.AdmissionCts?.Cancel();
- }
- catch (ObjectDisposedException ex)
- {
- ExpectedException.Ignore(ex);
- }
-
- // Serialize with Bind (always under _storeGate) so TryRelease cannot miss a
- // mid-Bind publication. Monitor is reentrant for RemoveStore under the gate.
- lock (_storeGate)
- {
- if (!store.ReservationHold.Committed)
- {
- store.ReservationHold.TryRelease();
- }
- }
- }
-
- private void ReleaseStoreSlots()
- {
- if (_storeStreams.Count == 0)
- {
- return;
- }
-
- var ids = new uint[_storeStreams.Count];
- _storeStreams.Keys.CopyTo(ids, 0);
- foreach (var streamId in ids)
- {
- RemoveStore(streamId);
- }
- }
-
- private sealed class StoreStream
- {
- private StoreAssemblyAdmission? _acquiredAdmission;
-
- public StoreStream(ArticleTransferReceiveStream receive, StoreAssemblyAdmission? acquiredAdmission)
- {
- Receive = receive;
- _acquiredAdmission = acquiredAdmission;
- }
-
- public ArticleTransferReceiveStream Receive { get; }
-
- /// Exactly-once hold for the queue reservation bound to this stream.
- public StoreReservationHold ReservationHold { get; } = new();
-
- /// Gets whether an assembly admission slot is still held for this stream.
- public bool SlotHeld => Volatile.Read(ref _acquiredAdmission) is not null;
-
- public bool MetaSeen { get; set; }
-
- public ulong MetaArtHash { get; set; }
-
- public int MetaArtSize { get; set; }
-
- public bool AcceptStarted { get; set; }
-
- public bool BodyAuthorized { get; set; }
-
- public bool Closed { get; set; }
-
- public bool AdmissionFailed { get; set; }
-
- public byte[]? PendingMeta { get; set; }
-
- public CancellationTokenSource? AdmissionCts { get; set; }
-
- public Task? AdmissionTask { get; set; }
-
- /// Exits the assembly admission slot acquired for this stream, if any.
- public void ReleaseAcquiredAdmission()
- {
- // Exactly-once: concurrent RemoveStore / failure paths must not double-Exit.
- Interlocked.Exchange(ref _acquiredAdmission, null)?.Exit();
- }
- }
- }
-}
diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.cs b/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.cs
deleted file mode 100644
index 58127425..00000000
--- a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.cs
+++ /dev/null
@@ -1,1245 +0,0 @@
-using System.Buffers;
-using System.Collections.Concurrent;
-using System.Diagnostics;
-using VectorNNTPD.Common.Articles;
-using VectorNNTPD.Common.Transport.ArticleTransfer;
-using VectorNNTPD.StorageService.Configuration;
-using VectorNNTPD.StorageService.Ingress;
-
-namespace VectorNNTPD.StorageService.Listener
-{
-
- ///
- /// VATP server session: client HELLO, then OPEN served from
- /// as META, credit-paced DATA/FIN, and END.
- /// STORE is a separate receive stream. After META matches the inbound bytes, the original
- /// article is enqueued. Path is not rewritten here. Retrieval does not rewrite Path.
- ///
- public sealed partial class StorageVatpSession : IAsyncDisposable
- {
- /// Local receive-stream correlation. Not a wire field and not a storage key.
- private static readonly Guid StoreReceiveCorrelation = new("5f5f5f5f-5f5f-5f5f-5f5f-5f5f5f5f5f5f");
-
- private static long _enqueueToResultTicks;
- private static long _enqueueToResultCount;
- private static long _windowWriteTicks;
- private static long _windowWriteCount;
- private static long _readWaitTicks;
- private static long _readCount;
- private static long _readBytes;
- private static long _readLt4K;
- private static long _readLt16K;
- private static long _readLt32K;
- private static long _readLt64K;
- private static long _readGe64K;
- private static long _readDurLt20Us;
- private static long _readDurLt50Us;
- private static long _readDurLt100Us;
- private static long _readDurLt500Us;
- private static long _readDurGe500Us;
- private static long _incompleteParses;
- private static long _framesParsed;
- private static long _processTicks;
- private static long _writeGateTicks;
- private static long _dataAcceptTicks;
- private static long _dataAcceptCount;
- private static long _dataCopyBytes;
- private static long _endValidateTicks;
- private static long _endValidateCount;
- private static long _readExact16K;
- private static long _framesThisRead0;
- private static long _framesThisRead1;
- private static long _framesThisReadMany;
- private static long _readsToFrame1;
- private static long _readsToFrame2;
- private static long _readsToFrame3;
- private static long _readsToFrame4;
- private static long _readsToFrame5Plus;
- private static long _partialAtRead0;
- private static long _partialAtReadHeader;
- private static long _partialAtReadLt16K;
- private static long _partialAtReadLt64K;
- private static long _partialAtReadGe64K;
- private static long _parseTicks;
- private static long _compactCount;
- private static long _compactBytes;
- private static long _compactTicks;
- private static int _clientWriteActive;
- private static long _clientWriteEndedAt;
- private static int _serverIoPhase;
- private static int _clientWriteServerPhase;
- private static long _readDuringWriteTicks;
- private static long _readDuringWriteCount;
- private static long _readAfterWriteTicks;
- private static long _readAfterWriteCount;
- private static long _readGapLt20Us;
- private static long _readGapLt100Us;
- private static long _readGapLt1Ms;
- private static long _readGapGe1Ms;
- private static long _clientWriteDuringReadTicks;
- private static long _clientWriteDuringReadCount;
- private static long _clientWriteDuringServerWriteTicks;
- private static long _clientWriteDuringServerWriteCount;
- private static long _clientWriteDuringOtherTicks;
- private static long _clientWriteDuringOtherCount;
- private static long _readyBehindPositive;
- private static long _readyBehindSamples;
- private static long _readyBehindSum;
- private static long _sendGapLt20Us;
- private static long _sendGapLt100Us;
- private static long _sendGapLt1Ms;
- private static long _sendGapGe1Ms;
- private static long _sessionCpu100Ns;
-
- /// Clears session path timing counters. Does not change acceptance.
- internal static void ResetEnqueueToResultTiming()
- {
- Interlocked.Exchange(ref _enqueueToResultTicks, 0);
- Interlocked.Exchange(ref _enqueueToResultCount, 0);
- Interlocked.Exchange(ref _windowWriteTicks, 0);
- Interlocked.Exchange(ref _windowWriteCount, 0);
- Interlocked.Exchange(ref _readWaitTicks, 0);
- Interlocked.Exchange(ref _readCount, 0);
- Interlocked.Exchange(ref _readBytes, 0);
- Interlocked.Exchange(ref _readLt4K, 0);
- Interlocked.Exchange(ref _readLt16K, 0);
- Interlocked.Exchange(ref _readLt32K, 0);
- Interlocked.Exchange(ref _readLt64K, 0);
- Interlocked.Exchange(ref _readGe64K, 0);
- Interlocked.Exchange(ref _readDurLt20Us, 0);
- Interlocked.Exchange(ref _readDurLt50Us, 0);
- Interlocked.Exchange(ref _readDurLt100Us, 0);
- Interlocked.Exchange(ref _readDurLt500Us, 0);
- Interlocked.Exchange(ref _readDurGe500Us, 0);
- Interlocked.Exchange(ref _incompleteParses, 0);
- Interlocked.Exchange(ref _framesParsed, 0);
- Interlocked.Exchange(ref _processTicks, 0);
- Interlocked.Exchange(ref _writeGateTicks, 0);
- Interlocked.Exchange(ref _dataAcceptTicks, 0);
- Interlocked.Exchange(ref _dataAcceptCount, 0);
- Interlocked.Exchange(ref _dataCopyBytes, 0);
- Interlocked.Exchange(ref _endValidateTicks, 0);
- Interlocked.Exchange(ref _endValidateCount, 0);
- Interlocked.Exchange(ref _readExact16K, 0);
- Interlocked.Exchange(ref _framesThisRead0, 0);
- Interlocked.Exchange(ref _framesThisRead1, 0);
- Interlocked.Exchange(ref _framesThisReadMany, 0);
- Interlocked.Exchange(ref _readsToFrame1, 0);
- Interlocked.Exchange(ref _readsToFrame2, 0);
- Interlocked.Exchange(ref _readsToFrame3, 0);
- Interlocked.Exchange(ref _readsToFrame4, 0);
- Interlocked.Exchange(ref _readsToFrame5Plus, 0);
- Interlocked.Exchange(ref _partialAtRead0, 0);
- Interlocked.Exchange(ref _partialAtReadHeader, 0);
- Interlocked.Exchange(ref _partialAtReadLt16K, 0);
- Interlocked.Exchange(ref _partialAtReadLt64K, 0);
- Interlocked.Exchange(ref _partialAtReadGe64K, 0);
- Interlocked.Exchange(ref _parseTicks, 0);
- Interlocked.Exchange(ref _compactCount, 0);
- Interlocked.Exchange(ref _compactBytes, 0);
- Interlocked.Exchange(ref _compactTicks, 0);
- Volatile.Write(ref _clientWriteActive, 0);
- Volatile.Write(ref _clientWriteEndedAt, 0);
- Volatile.Write(ref _serverIoPhase, 0);
- Volatile.Write(ref _clientWriteServerPhase, 0);
- Interlocked.Exchange(ref _readDuringWriteTicks, 0);
- Interlocked.Exchange(ref _readDuringWriteCount, 0);
- Interlocked.Exchange(ref _readAfterWriteTicks, 0);
- Interlocked.Exchange(ref _readAfterWriteCount, 0);
- Interlocked.Exchange(ref _readGapLt20Us, 0);
- Interlocked.Exchange(ref _readGapLt100Us, 0);
- Interlocked.Exchange(ref _readGapLt1Ms, 0);
- Interlocked.Exchange(ref _readGapGe1Ms, 0);
- Interlocked.Exchange(ref _clientWriteDuringReadTicks, 0);
- Interlocked.Exchange(ref _clientWriteDuringReadCount, 0);
- Interlocked.Exchange(ref _clientWriteDuringServerWriteTicks, 0);
- Interlocked.Exchange(ref _clientWriteDuringServerWriteCount, 0);
- Interlocked.Exchange(ref _clientWriteDuringOtherTicks, 0);
- Interlocked.Exchange(ref _clientWriteDuringOtherCount, 0);
- Interlocked.Exchange(ref _readyBehindPositive, 0);
- Interlocked.Exchange(ref _readyBehindSamples, 0);
- Interlocked.Exchange(ref _readyBehindSum, 0);
- Interlocked.Exchange(ref _sendGapLt20Us, 0);
- Interlocked.Exchange(ref _sendGapLt100Us, 0);
- Interlocked.Exchange(ref _sendGapLt1Ms, 0);
- Interlocked.Exchange(ref _sendGapGe1Ms, 0);
- Interlocked.Exchange(ref _sessionCpu100Ns, 0);
- StreamStorageVatpTransport.ResetIoSplit();
- }
-
- /// Gets how long successful RESULT writes took after TryEnqueue returned true.
- internal static (long Count, double Seconds) ReadEnqueueToResultTiming()
- {
- var count = Volatile.Read(ref _enqueueToResultCount);
- var ticks = Volatile.Read(ref _enqueueToResultTicks);
- return (count, ticks / (double)Stopwatch.Frequency);
- }
-
- /// Gets time spent copying STORE DATA, writing WINDOW credit, waiting to read, and validating END.
- internal static (
- long DataCount,
- double DataSeconds,
- long WindowCount,
- double WindowSeconds,
- double ReadWaitSeconds,
- long EndCount,
- double EndSeconds) ReadReceivePathTiming()
- {
- return (
- Volatile.Read(ref _dataAcceptCount),
- Volatile.Read(ref _dataAcceptTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _windowWriteCount),
- Volatile.Read(ref _windowWriteTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _readWaitTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _endValidateCount),
- Volatile.Read(ref _endValidateTicks) / (double)Stopwatch.Frequency);
- }
-
- ///
- /// Gets read sizes and the time between reads. Process time includes WINDOW and RESULT writes
- /// and does not overlap read-wait time.
- ///
- internal static ReceiveObservation ReadReceiveObservation()
- {
- return new ReceiveObservation(
- Volatile.Read(ref _readCount),
- Volatile.Read(ref _readBytes),
- Volatile.Read(ref _readLt4K),
- Volatile.Read(ref _readLt16K),
- Volatile.Read(ref _readLt32K),
- Volatile.Read(ref _readLt64K),
- Volatile.Read(ref _readGe64K),
- Volatile.Read(ref _readDurLt20Us),
- Volatile.Read(ref _readDurLt50Us),
- Volatile.Read(ref _readDurLt100Us),
- Volatile.Read(ref _readDurLt500Us),
- Volatile.Read(ref _readDurGe500Us),
- Volatile.Read(ref _incompleteParses),
- Volatile.Read(ref _framesParsed),
- Volatile.Read(ref _processTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _writeGateTicks) / (double)Stopwatch.Frequency);
- }
-
- ///
- /// Gets frame-assembly counters. Parse and compact times sit inside process time and do not overlap read-wait.
- /// Timeout setup and stream-read times sit inside read-wait and do not overlap each other.
- ///
- internal static AssemblyObservation ReadAssemblyObservation()
- {
- var (timeoutSeconds, streamReadSeconds) = StreamStorageVatpTransport.ReadIoSplit();
- return new AssemblyObservation(
- Volatile.Read(ref _readExact16K),
- Volatile.Read(ref _framesThisRead0),
- Volatile.Read(ref _framesThisRead1),
- Volatile.Read(ref _framesThisReadMany),
- Volatile.Read(ref _readsToFrame1),
- Volatile.Read(ref _readsToFrame2),
- Volatile.Read(ref _readsToFrame3),
- Volatile.Read(ref _readsToFrame4),
- Volatile.Read(ref _readsToFrame5Plus),
- Volatile.Read(ref _partialAtRead0),
- Volatile.Read(ref _partialAtReadHeader),
- Volatile.Read(ref _partialAtReadLt16K),
- Volatile.Read(ref _partialAtReadLt64K),
- Volatile.Read(ref _partialAtReadGe64K),
- Volatile.Read(ref _parseTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _compactCount),
- Volatile.Read(ref _compactBytes),
- Volatile.Read(ref _compactTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _dataCopyBytes),
- timeoutSeconds,
- streamReadSeconds);
- }
-
- /// One level's frame-assembly counters. Times are elapsed and do not overlap.
- internal readonly record struct AssemblyObservation(
- long ReadExact16K,
- long FramesThisRead0,
- long FramesThisRead1,
- long FramesThisReadMany,
- long ReadsToFrame1,
- long ReadsToFrame2,
- long ReadsToFrame3,
- long ReadsToFrame4,
- long ReadsToFrame5Plus,
- long PartialAtRead0,
- long PartialAtReadHeader,
- long PartialAtReadLt16K,
- long PartialAtReadLt64K,
- long PartialAtReadGe64K,
- double ParseSeconds,
- long CompactCount,
- long CompactBytes,
- double CompactSeconds,
- long DataCopyBytes,
- double TimeoutSetupSeconds,
- double StreamReadSeconds);
-
- ///
- /// Correlates client writes with server reads on .
- /// Both sides run in this process, so the clock is the same. Intervals do not overlap within one side.
- ///
- internal static PaceObservation ReadPaceObservation()
- {
- return new PaceObservation(
- Volatile.Read(ref _readDuringWriteTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _readDuringWriteCount),
- Volatile.Read(ref _readAfterWriteTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _readAfterWriteCount),
- Volatile.Read(ref _readGapLt20Us),
- Volatile.Read(ref _readGapLt100Us),
- Volatile.Read(ref _readGapLt1Ms),
- Volatile.Read(ref _readGapGe1Ms),
- Volatile.Read(ref _clientWriteDuringReadTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _clientWriteDuringReadCount),
- Volatile.Read(ref _clientWriteDuringServerWriteTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _clientWriteDuringServerWriteCount),
- Volatile.Read(ref _clientWriteDuringOtherTicks) / (double)Stopwatch.Frequency,
- Volatile.Read(ref _clientWriteDuringOtherCount),
- Volatile.Read(ref _readyBehindPositive),
- Volatile.Read(ref _readyBehindSamples),
- Volatile.Read(ref _readyBehindSum),
- Volatile.Read(ref _sendGapLt20Us),
- Volatile.Read(ref _sendGapLt100Us),
- Volatile.Read(ref _sendGapLt1Ms),
- Volatile.Read(ref _sendGapGe1Ms),
- Volatile.Read(ref _sessionCpu100Ns) / 10_000_000d);
- }
-
- ///
- /// Records that the benchmark client is about to enter SslStream.WriteAsync.
- /// is other STORE slots that already have credit and a frame.
- /// is true when the send loop blocked since the previous write.
- ///
- internal static void NoteClientWriteStart(int readyBehind, bool afterLoopWait)
- {
- var now = Stopwatch.GetTimestamp();
- var previous = Volatile.Read(ref _clientWriteEndedAt);
- if (previous != 0 && !afterLoopWait)
- {
- NoteSendGap(now - previous);
- }
-
- if (readyBehind > 0)
- {
- Interlocked.Increment(ref _readyBehindPositive);
- }
-
- Interlocked.Add(ref _readyBehindSum, readyBehind);
- Interlocked.Increment(ref _readyBehindSamples);
- Volatile.Write(ref _clientWriteServerPhase, Volatile.Read(ref _serverIoPhase));
- Volatile.Write(ref _clientWriteActive, 1);
- }
-
- /// Records that the benchmark client's TLS write returned. is that write only.
- internal static void NoteClientWriteEnd(long elapsedTicks)
- {
- var phase = Volatile.Read(ref _clientWriteServerPhase);
- Volatile.Write(ref _clientWriteActive, 0);
- Volatile.Write(ref _clientWriteEndedAt, Stopwatch.GetTimestamp());
- if (phase == 1)
- {
- Interlocked.Add(ref _clientWriteDuringReadTicks, elapsedTicks);
- Interlocked.Increment(ref _clientWriteDuringReadCount);
- }
- else if (phase == 2)
- {
- Interlocked.Add(ref _clientWriteDuringServerWriteTicks, elapsedTicks);
- Interlocked.Increment(ref _clientWriteDuringServerWriteCount);
- }
- else
- {
- Interlocked.Add(ref _clientWriteDuringOtherTicks, elapsedTicks);
- Interlocked.Increment(ref _clientWriteDuringOtherCount);
- }
- }
-
- ///
- /// One level of client/server overlap. Read-during-write and read-after-write partition server read-wait.
- /// Client-write buckets partition client TLS-write time by the server phase at write start.
- ///
- internal readonly record struct PaceObservation(
- double ReadDuringClientWriteSeconds,
- long ReadDuringClientWriteCount,
- double ReadAfterClientWriteSeconds,
- long ReadAfterClientWriteCount,
- long ReadGapLt20Us,
- long ReadGapLt100Us,
- long ReadGapLt1Ms,
- long ReadGapGe1Ms,
- double ClientWriteDuringServerReadSeconds,
- long ClientWriteDuringServerReadCount,
- double ClientWriteDuringServerWriteSeconds,
- long ClientWriteDuringServerWriteCount,
- double ClientWriteDuringOtherSeconds,
- long ClientWriteDuringOtherCount,
- long ReadyBehindPositive,
- long ReadyBehindSamples,
- long ReadyBehindSum,
- long SendGapLt20Us,
- long SendGapLt100Us,
- long SendGapLt1Ms,
- long SendGapGe1Ms,
- double SessionCpuSeconds);
-
- /// One level's receive-size counters. Times are not CPU time.
- internal readonly record struct ReceiveObservation(
- long Reads,
- long ReadBytes,
- long ReadLt4K,
- long ReadLt16K,
- long ReadLt32K,
- long ReadLt64K,
- long ReadGe64K,
- long ReadDurLt20Us,
- long ReadDurLt50Us,
- long ReadDurLt100Us,
- long ReadDurLt500Us,
- long ReadDurGe500Us,
- long IncompleteParses,
- long Frames,
- double ProcessSeconds,
- double WriteGateSeconds);
-
- /// Counts one completed transport read by size. Does not change the bytes.
- private static void NoteRead(int bytes)
- {
- Interlocked.Increment(ref _readCount);
- Interlocked.Add(ref _readBytes, bytes);
- if (bytes < 4 * 1024)
- {
- Interlocked.Increment(ref _readLt4K);
- }
- else if (bytes < 16 * 1024)
- {
- Interlocked.Increment(ref _readLt16K);
- }
- else if (bytes < 32 * 1024)
- {
- Interlocked.Increment(ref _readLt32K);
- if (bytes == 16 * 1024)
- {
- Interlocked.Increment(ref _readExact16K);
- }
- }
- else if (bytes < 64 * 1024)
- {
- Interlocked.Increment(ref _readLt64K);
- }
- else
- {
- Interlocked.Increment(ref _readGe64K);
- }
- }
-
- /// Counts how long one read call took. This is elapsed time, not CPU time.
- private static void NoteReadDuration(long ticks)
- {
- var microseconds = ticks * 1_000_000d / Stopwatch.Frequency;
- if (microseconds < 20)
- {
- Interlocked.Increment(ref _readDurLt20Us);
- }
- else if (microseconds < 50)
- {
- Interlocked.Increment(ref _readDurLt50Us);
- }
- else if (microseconds < 100)
- {
- Interlocked.Increment(ref _readDurLt100Us);
- }
- else if (microseconds < 500)
- {
- Interlocked.Increment(ref _readDurLt500Us);
- }
- else
- {
- Interlocked.Increment(ref _readDurGe500Us);
- }
- }
-
- /// Counts bytes already retained when a transport read starts. Does not move those bytes.
- private static void NotePartialAtRead(int buffered)
- {
- if (buffered == 0)
- {
- Interlocked.Increment(ref _partialAtRead0);
- }
- else if (buffered < VatpProtocol.HeaderLengthBytes)
- {
- Interlocked.Increment(ref _partialAtReadHeader);
- }
- else if (buffered < 16 * 1024)
- {
- Interlocked.Increment(ref _partialAtReadLt16K);
- }
- else if (buffered < 64 * 1024)
- {
- Interlocked.Increment(ref _partialAtReadLt64K);
- }
- else
- {
- Interlocked.Increment(ref _partialAtReadGe64K);
- }
- }
-
- /// Counts complete frames decoded from one transport read.
- private static void NoteFramesThisRead(int frames)
- {
- if (frames == 0)
- {
- Interlocked.Increment(ref _framesThisRead0);
- }
- else if (frames == 1)
- {
- Interlocked.Increment(ref _framesThisRead1);
- }
- else
- {
- Interlocked.Increment(ref _framesThisReadMany);
- }
- }
-
- /// Counts transport reads from the previous complete frame through this one.
- private static void NoteReadsToFrame(int reads)
- {
- if (reads <= 1)
- {
- Interlocked.Increment(ref _readsToFrame1);
- }
- else if (reads == 2)
- {
- Interlocked.Increment(ref _readsToFrame2);
- }
- else if (reads == 3)
- {
- Interlocked.Increment(ref _readsToFrame3);
- }
- else if (reads == 4)
- {
- Interlocked.Increment(ref _readsToFrame4);
- }
- else
- {
- Interlocked.Increment(ref _readsToFrame5Plus);
- }
- }
-
- /// Process-wide read-gap buckets. The buckets are mutually exclusive.
- private static void NoteReadGap(long ticks)
- {
- var microseconds = ticks * 1_000_000d / Stopwatch.Frequency;
- if (microseconds < 20)
- {
- Interlocked.Increment(ref _readGapLt20Us);
- }
- else if (microseconds < 100)
- {
- Interlocked.Increment(ref _readGapLt100Us);
- }
- else if (microseconds < 1_000)
- {
- Interlocked.Increment(ref _readGapLt1Ms);
- }
- else
- {
- Interlocked.Increment(ref _readGapGe1Ms);
- }
- }
-
- /// Process-wide send-gap buckets used by the bench client write markers.
- private static void NoteSendGap(long ticks)
- {
- var microseconds = ticks * 1_000_000d / Stopwatch.Frequency;
- if (microseconds < 20)
- {
- Interlocked.Increment(ref _sendGapLt20Us);
- }
- else if (microseconds < 100)
- {
- Interlocked.Increment(ref _sendGapLt100Us);
- }
- else if (microseconds < 1_000)
- {
- Interlocked.Increment(ref _sendGapLt1Ms);
- }
- else
- {
- Interlocked.Increment(ref _sendGapGe1Ms);
- }
- }
-
- private static void AddProcessTicks(long ticks) => Interlocked.Add(ref _processTicks, ticks);
-
- private static void AddReadWaitTicks(long ticks) => Interlocked.Add(ref _readWaitTicks, ticks);
-
- private static void AddParseTicks(long ticks) => Interlocked.Add(ref _parseTicks, ticks);
-
- private static void AddWriteGateTicks(long ticks) => Interlocked.Add(ref _writeGateTicks, ticks);
-
- private static void AddSessionCpu(long cpu100Ns) => Interlocked.Add(ref _sessionCpu100Ns, cpu100Ns);
-
- private static void NoteIncompleteParse() => Interlocked.Increment(ref _incompleteParses);
-
- private static void NoteFrameParsed() => Interlocked.Increment(ref _framesParsed);
-
- private static void NoteCompact(long ticks, int bytes)
- {
- Interlocked.Add(ref _compactTicks, ticks);
- Interlocked.Increment(ref _compactCount);
- Interlocked.Add(ref _compactBytes, bytes);
- }
-
- private static void NoteReadOverlap(bool clientWriting, long readTicks)
- {
- if (clientWriting)
- {
- Interlocked.Add(ref _readDuringWriteTicks, readTicks);
- Interlocked.Increment(ref _readDuringWriteCount);
- }
- else
- {
- Interlocked.Add(ref _readAfterWriteTicks, readTicks);
- Interlocked.Increment(ref _readAfterWriteCount);
- }
- }
-
- private static void SetServerIoPhase(int phase) => Volatile.Write(ref _serverIoPhase, phase);
-
- private static bool IsClientWriteActive() => Volatile.Read(ref _clientWriteActive) != 0;
-
- private static long ReadClientWriteEndedAt() => Volatile.Read(ref _clientWriteEndedAt);
-
- private static void NoteDataAccept(long ticks, long copiedBytes)
- {
- Interlocked.Add(ref _dataAcceptTicks, ticks);
- Interlocked.Increment(ref _dataAcceptCount);
- if (copiedBytes > 0)
- {
- Interlocked.Add(ref _dataCopyBytes, copiedBytes);
- }
- }
-
- private static void NoteWindowWrite(long ticks)
- {
- Interlocked.Add(ref _windowWriteTicks, ticks);
- Interlocked.Increment(ref _windowWriteCount);
- }
-
- private static void NoteEndValidate(long ticks)
- {
- Interlocked.Add(ref _endValidateTicks, ticks);
- Interlocked.Increment(ref _endValidateCount);
- }
-
- private static void NoteEnqueueToResult(long ticks)
- {
- Interlocked.Add(ref _enqueueToResultTicks, ticks);
- Interlocked.Increment(ref _enqueueToResultCount);
- }
-
- private readonly IStorageVatpTransport _transport;
- private readonly IStorageArticleOpenBoundary _openBoundary;
- private readonly StorageServerListenerRuntimeOptions _listener;
- private readonly ArticleTransferLimits _limits;
- private readonly ILogger _logger;
- private readonly IAcceptedArticleQueue? _acceptedArticles;
- private readonly StoreAssemblyAdmission? _storeAdmission;
- private readonly Dictionary _streams = [];
- private readonly ConcurrentDictionary _storeStreams = [];
- private readonly ArticleTransferReadyRing _readyRing = new();
- private readonly SemaphoreSlim _writeGate = new(1, 1);
- private readonly object _storeGate = new();
- private readonly byte[] _smallWrite = new byte[512];
- private CancellationToken _runCancellation;
-
- /// Article buffers allocated by this session after a STORE reservation was granted.
- internal long StoreBodiesAllocated;
- private Exception? _fatalSessionException;
- private uint _maxFramePayload = VatpProtocol.DefaultMaxFramePayload;
- private bool _clientHelloComplete;
- private bool _serverHelloSent;
- private int _disposed;
-
- /// Initializes a VATP session over an established transport.
- public StorageVatpSession(
- IStorageVatpTransport transport,
- IStorageArticleOpenBoundary openBoundary,
- StorageServerListenerRuntimeOptions listener,
- ILogger logger,
- ArticleTransferLimits? limits = null,
- IAcceptedArticleQueue? acceptedArticles = null,
- StoreAssemblyAdmission? storeAdmission = null)
- {
- ArgumentNullException.ThrowIfNull(transport);
- ArgumentNullException.ThrowIfNull(openBoundary);
- ArgumentNullException.ThrowIfNull(listener);
- ArgumentNullException.ThrowIfNull(logger);
- _transport = transport;
- _openBoundary = openBoundary;
- _listener = listener;
- _limits = limits ?? ArticleTransferLimits.Default;
- _logger = logger;
- _acceptedArticles = acceptedArticles;
- _storeAdmission = storeAdmission;
- }
-
- /// Runs until peer close, cancellation, or fatal protocol error.
- public async Task RunAsync(CancellationToken cancellationToken)
- {
- _runCancellation = cancellationToken;
- var capacity = _listener.ParserAccumulationMaxBytes;
- var parseBuffer = ArrayPool.Shared.Rent(capacity);
- var usable = Math.Min(parseBuffer.Length, capacity);
- var buffered = 0;
- var cpuStart = ThreadCpuTimes.Capture();
- try
- {
- // 0 means "no prior successful read yet"; after the first read this holds a timestamp.
- var processMark = 0L;
- var readsSinceFrame = 0;
- while (!cancellationToken.IsCancellationRequested)
- {
- if (processMark != 0)
- {
- AddProcessTicks(Stopwatch.GetTimestamp() - processMark);
- }
-
- if (buffered >= usable)
- {
- await WriteFailAsync(
- VatpProtocol.ConnectionStreamId,
- VatpErrorCode.FrameTooLarge,
- cancellationToken)
- .ConfigureAwait(false);
- return;
- }
-
- int bytesRead;
- var clientWriting = IsClientWriteActive();
- if (!clientWriting)
- {
- var ended = ReadClientWriteEndedAt();
- if (ended != 0)
- {
- NoteReadGap(Stopwatch.GetTimestamp() - ended);
- }
- }
-
- try
- {
- NotePartialAtRead(buffered);
- SetServerIoPhase(1);
- var readStarted = Stopwatch.GetTimestamp();
- bytesRead = await _transport
- .ReadAsync(parseBuffer.AsMemory(buffered, usable - buffered), cancellationToken)
- .ConfigureAwait(false);
- var readTicks = Stopwatch.GetTimestamp() - readStarted;
- SetServerIoPhase(0);
- AddReadWaitTicks(readTicks);
- NoteReadDuration(readTicks);
- NoteReadOverlap(clientWriting, readTicks);
-
- processMark = Stopwatch.GetTimestamp();
- }
- catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
- {
- return;
- }
- catch (TimeoutException)
- {
- if (_fatalSessionException is not null)
- {
- throw _fatalSessionException;
- }
-
- return;
- }
-
- if (bytesRead == 0)
- {
- if (_fatalSessionException is not null)
- {
- throw _fatalSessionException;
- }
-
- return;
- }
-
- buffered += bytesRead;
- NoteRead(bytesRead);
- readsSinceFrame++;
-
- var consumedTotal = 0;
- var framesThisRead = 0;
- while (true)
- {
- var remaining = new ReadOnlySequence(parseBuffer, consumedTotal, buffered - consumedTotal);
- var parseStarted = Stopwatch.GetTimestamp();
- var parsed = VatpFrameParser.ParseOneFrame(remaining, _maxFramePayload);
- AddParseTicks(Stopwatch.GetTimestamp() - parseStarted);
- if (parsed.Status == VatpFrameParseStatus.Incomplete)
- {
- NoteIncompleteParse();
- break;
- }
-
- if (parsed.Status == VatpFrameParseStatus.Invalid)
- {
- await WriteFailAsync(
- VatpProtocol.ConnectionStreamId,
- parsed.Error,
- cancellationToken)
- .ConfigureAwait(false);
- return;
- }
-
- consumedTotal += checked((int)parsed.ConsumedBytes);
- framesThisRead++;
- NoteFrameParsed();
- if (parsed.Frame is not { } frame)
- {
- await WriteFailAsync(
- VatpProtocol.ConnectionStreamId,
- VatpErrorCode.InvalidFrameType,
- cancellationToken)
- .ConfigureAwait(false);
- return;
- }
-
- var continueSession = await HandleFrameAsync(frame, cancellationToken).ConfigureAwait(false);
- if (!continueSession)
- {
- return;
- }
-
- if (_fatalSessionException is not null)
- {
- throw _fatalSessionException;
- }
- }
-
- if (consumedTotal > 0)
- {
- var remainingBytes = buffered - consumedTotal;
- if (remainingBytes > 0)
- {
- var compactStarted = Stopwatch.GetTimestamp();
- Buffer.BlockCopy(parseBuffer, consumedTotal, parseBuffer, 0, remainingBytes);
- NoteCompact(Stopwatch.GetTimestamp() - compactStarted, remainingBytes);
- }
-
- buffered = remainingBytes;
- }
-
- NoteFramesThisRead(framesThisRead);
- if (framesThisRead > 0)
- {
- NoteReadsToFrame(readsSinceFrame);
- readsSinceFrame = 0;
- }
- }
- }
- finally
- {
- ReleaseStoreSlots();
- _streams.Clear();
- _storeStreams.Clear();
- _readyRing.Clear();
- ArrayPool.Shared.Return(parseBuffer);
- var cpuEnd = ThreadCpuTimes.Capture();
- AddSessionCpu((cpuEnd.Kernel - cpuStart.Kernel) + (cpuEnd.User - cpuStart.User));
- StorageVatpSessionLogMessages.ConnectionClosed(_logger);
- }
- }
-
- ///
- public async ValueTask DisposeAsync()
- {
- if (Interlocked.Exchange(ref _disposed, 1) == 1)
- {
- return;
- }
-
- await _transport.DisposeAsync().ConfigureAwait(false);
- _writeGate.Dispose();
- }
-
- private async Task HandleFrameAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- switch (frame.Header.Type)
- {
- case VatpFrameType.Hello:
- return await HandleHelloAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.Open:
- return await HandleOpenAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.Store:
- return await HandleStoreAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.Meta:
- return await HandleStoreMetaAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.Data:
- return await HandleStoreDataAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.End:
- return await HandleStoreEndAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.Window:
- return await HandleWindowAsync(frame, cancellationToken).ConfigureAwait(false);
- case VatpFrameType.Cancel:
- return await HandleCancelAsync(frame, cancellationToken).ConfigureAwait(false);
- default:
- await WriteFailAsync(
- frame.Header.StreamId == VatpProtocol.ConnectionStreamId
- ? VatpProtocol.ConnectionStreamId
- : frame.Header.StreamId,
- VatpErrorCode.InvalidFrameType,
- cancellationToken)
- .ConfigureAwait(false);
- return false;
- }
- }
-
- private async Task HandleHelloAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- if (frame.Header.StreamId != VatpProtocol.ConnectionStreamId)
- {
- await WriteFailAsync(VatpProtocol.ConnectionStreamId, VatpErrorCode.InvalidStreamId, cancellationToken)
- .ConfigureAwait(false);
- return false;
- }
-
- if (_clientHelloComplete)
- {
- await WriteFailAsync(VatpProtocol.ConnectionStreamId, VatpErrorCode.InvalidHello, cancellationToken)
- .ConfigureAwait(false);
- return false;
- }
-
- if (!VatpHello.TryDecode(frame.Payload, out var hello, out var error))
- {
- await WriteFailAsync(VatpProtocol.ConnectionStreamId, error, cancellationToken).ConfigureAwait(false);
- return false;
- }
-
- _maxFramePayload = Math.Min(hello.MaxFramePayload, VatpProtocol.DefaultMaxFramePayload);
- _clientHelloComplete = true;
- StorageVatpSessionLogMessages.ClientHelloAccepted(_logger, _maxFramePayload);
-
- if (!_serverHelloSent)
- {
- _serverHelloSent = true;
- var encoded = VatpFrameEncoder.EncodeHello(_maxFramePayload);
- await WriteEncodedAsync(encoded, cancellationToken).ConfigureAwait(false);
- }
-
- return true;
- }
-
- private async Task HandleOpenAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (!_clientHelloComplete)
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken).ConfigureAwait(false);
- return false;
- }
-
- if (!VatpOpenPayload.TryDecode(frame.Payload, out var open, out var decodeError))
- {
- await WriteFailAsync(streamId, decodeError, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (streamId == VatpProtocol.ConnectionStreamId
- || _streams.ContainsKey(streamId)
- || _storeStreams.ContainsKey(streamId)
- || _streams.Count + _storeStreams.Count >= _limits.MaxStreamsPerConnection)
- {
- await WriteFailAsync(streamId, streamId == VatpProtocol.ConnectionStreamId
- ? VatpErrorCode.InvalidStreamId
- : VatpErrorCode.StreamTableError, cancellationToken)
- .ConfigureAwait(false);
- return true;
- }
-
- cancellationToken.ThrowIfCancellationRequested();
- var result = _openBoundary.TryOpen(open.RequestId, open.ArticleId);
- cancellationToken.ThrowIfCancellationRequested();
- if (!result.Accepted || result.Record.ParseStatus != ArticleParseStatus.CanonicalV1)
- {
- StorageVatpSessionLogMessages.OpenRejected(_logger, streamId, open.RequestId);
- await WriteFailAsync(streamId, VatpErrorCode.OpenRejected, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- var record = result.Record;
- var meta = ArticleCanonicalTransferMeta.FromRecord(in record, result.SelectedDateHeaderName);
- var metaBytes = VatpMetaCodec.Encode(in meta);
- var stream = new SendStream(
- streamId,
- record,
- new ArticleTransferWindow(_limits.InitialStreamWindowBytes, _limits.MaxStreamCreditBytes));
- _streams.Add(streamId, stream);
- StorageVatpSessionLogMessages.OpenAccepted(_logger, streamId, open.RequestId);
- await WriteEncodedAsync(VatpFrameEncoder.EncodeMeta(streamId, metaBytes), cancellationToken).ConfigureAwait(false);
- stream.Phase = SendPhase.SendingData;
- if (stream.SentBytes < record.ArtSize && stream.Window.HasCredit)
- {
- _readyRing.Enqueue(streamId);
- }
-
- await PumpDataAsync(cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- private async Task HandleWindowAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (frame.Payload.Length != VatpProtocol.WindowPayloadLength)
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameLength, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- Span windowBytes = stackalloc byte[VatpProtocol.WindowPayloadLength];
- if (frame.Payload.IsSingleSegment)
- {
- frame.Payload.FirstSpan.CopyTo(windowBytes);
- }
- else
- {
- frame.Payload.CopyTo(windowBytes);
- }
-
- if (!VatpControlPayload.TryDecodeWindow(windowBytes, out var addCredit))
- {
- await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameLength, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (addCredit == 0)
- {
- return true;
- }
-
- if (!_streams.TryGetValue(streamId, out var stream) || stream.Phase != SendPhase.SendingData)
- {
- await WriteFailAsync(streamId, VatpErrorCode.UnknownStream, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- _ = stream.Window.Add(addCredit);
- if (stream.SentBytes < stream.Record.ArtSize)
- {
- _readyRing.Enqueue(streamId);
- }
-
- await PumpDataAsync(cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- private async Task HandleCancelAsync(VatpParsedFrame frame, CancellationToken cancellationToken)
- {
- var streamId = frame.Header.StreamId;
- if (_storeStreams.TryGetValue(streamId, out var store))
- {
- if (store.AcceptStarted)
- {
- return true;
- }
-
- _ = store.Receive.TryCancel();
- RemoveStore(streamId);
- StorageVatpSessionLogMessages.TransferCancelled(_logger, streamId);
- await WriteFailAsync(streamId, VatpErrorCode.Cancelled, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- if (!_streams.Remove(streamId, out var stream))
- {
- await WriteFailAsync(streamId, VatpErrorCode.UnknownStream, cancellationToken).ConfigureAwait(false);
- return true;
- }
-
- _readyRing.Remove(streamId);
- stream.Phase = SendPhase.Cancelled;
- StorageVatpSessionLogMessages.TransferCancelled(_logger, streamId);
- return true;
- }
-
- private async Task PumpDataAsync(CancellationToken cancellationToken)
- {
- while (TryTakeDataFrame(out var encoded, out var completedStreamId))
- {
- await WriteEncodedAsync(encoded, cancellationToken).ConfigureAwait(false);
- if (completedStreamId is { } streamId)
- {
- await WriteEncodedAsync(VatpFrameEncoder.EncodeEnd(streamId), cancellationToken).ConfigureAwait(false);
- }
- }
- }
-
- private bool TryTakeDataFrame(out VatpFrameEncoder.EncodedFrame encoded, out uint? completedStreamId)
- {
- encoded = default;
- completedStreamId = null;
- var attempts = _readyRing.Count;
- while (attempts-- > 0 && _readyRing.TryTakeNext(out var streamId))
- {
- if (!_streams.TryGetValue(streamId, out var stream) || stream.Phase != SendPhase.SendingData)
- {
- continue;
- }
-
- var remaining = stream.Record.ArtSize - stream.SentBytes;
- if (remaining <= 0)
- {
- CompleteStream(stream);
- continue;
- }
-
- if (!stream.Window.HasCredit)
- {
- continue;
- }
-
- var length = ArticleTransferReadyRing.ComputeDataPayloadLength(
- remaining,
- stream.Window.Credit,
- _maxFramePayload);
- if (length <= 0 || !stream.Window.TryConsume(length))
- {
- continue;
- }
-
- var fin = length == remaining;
- var payload = stream.Record.ArtData.Slice(stream.SentBytes, length);
- stream.SentBytes += length;
- encoded = VatpFrameEncoder.EncodeData(streamId, payload, fin);
- if (stream.SentBytes < stream.Record.ArtSize)
- {
- _readyRing.Enqueue(streamId);
- }
- else
- {
- CompleteStream(stream);
- completedStreamId = streamId;
- }
-
- return true;
- }
-
- return false;
- }
-
- private void CompleteStream(SendStream stream)
- {
- _readyRing.Remove(stream.StreamId);
- stream.Phase = SendPhase.Completed;
- _ = _streams.Remove(stream.StreamId);
- StorageVatpSessionLogMessages.TransferCompleted(_logger, stream.StreamId);
- }
-
- private async Task WriteFailAsync(uint streamId, VatpErrorCode error, CancellationToken cancellationToken)
- {
- var encoded = VatpFrameEncoder.EncodeFail(streamId, error);
- await WriteEncodedAsync(encoded, cancellationToken).ConfigureAwait(false);
- }
-
- private async Task WriteEncodedAsync(VatpFrameEncoder.EncodedFrame encoded, CancellationToken cancellationToken)
- {
- SetServerIoPhase(2);
- try
- {
- var gateStarted = Stopwatch.GetTimestamp();
- await _writeGate.WaitAsync(cancellationToken).ConfigureAwait(false);
- AddWriteGateTicks(Stopwatch.GetTimestamp() - gateStarted);
- try
- {
- var headerLength = encoded.Header.Length;
- var payloadLength = encoded.Payload.Length;
- if (headerLength + payloadLength <= _smallWrite.Length)
- {
- if (headerLength > 0)
- {
- encoded.Header.Span.CopyTo(_smallWrite);
- }
-
- if (payloadLength > 0)
- {
- encoded.Payload.Span.CopyTo(_smallWrite.AsSpan(headerLength));
- }
-
- await WriteFullyAsync(_smallWrite.AsMemory(0, headerLength + payloadLength), cancellationToken)
- .ConfigureAwait(false);
- return;
- }
-
- if (!encoded.Header.IsEmpty)
- {
- await WriteFullyAsync(encoded.Header, cancellationToken).ConfigureAwait(false);
- }
-
- if (!encoded.Payload.IsEmpty)
- {
- await WriteFullyAsync(encoded.Payload, cancellationToken).ConfigureAwait(false);
- }
- }
- finally
- {
- _writeGate.Release();
- }
- }
- finally
- {
- SetServerIoPhase(0);
- }
- }
-
- private async Task WriteFullyAsync(ReadOnlyMemory payload, CancellationToken cancellationToken)
- {
- var written = 0;
- while (written < payload.Length)
- {
- var accepted = await _transport.WriteAsync(payload[written..], cancellationToken).ConfigureAwait(false);
- if (accepted <= 0)
- {
- throw new IOException("Transport returned zero accepted bytes.");
- }
-
- written += accepted;
- }
- }
-
- private void TrySetFatalSessionException(Exception exception)
- {
- _ = Interlocked.CompareExchange(ref _fatalSessionException, exception, null);
- }
-
- private enum SendPhase
- {
- SendingData,
- Completed,
- Cancelled,
- }
-
- private sealed class SendStream
- {
- public SendStream(uint streamId, ArticleRecord record, ArticleTransferWindow window)
- {
- StreamId = streamId;
- Record = record;
- Window = window;
- }
-
- public uint StreamId { get; }
-
- public ArticleRecord Record { get; }
-
- /// Mutable credit. Must stay a field so updates this stream.
- public ArticleTransferWindow Window;
-
- public int SentBytes { get; set; }
-
- public SendPhase Phase { get; set; }
- }
- }
-}
diff --git a/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs b/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs
index dbf0265f..75fa81da 100644
--- a/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs
+++ b/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs
@@ -2,8 +2,8 @@ namespace VectorNNTPD.StorageService.Listener
{
///
- /// Process-wide cap on in-flight STORE article assemblies, acquired before the receive
- /// stream allocates ArtData.
+ /// Process-wide cap on in-flight TAKETHIS article assemblies, acquired before the article
+ /// payload is buffered and admitted to the accepted-article queue.
///
public sealed class StoreAssemblyAdmission
{
diff --git a/src/VectorNNTPD.StorageService/Listener/StoreReservationHold.cs b/src/VectorNNTPD.StorageService/Listener/StoreReservationHold.cs
deleted file mode 100644
index 68680823..00000000
--- a/src/VectorNNTPD.StorageService/Listener/StoreReservationHold.cs
+++ /dev/null
@@ -1,110 +0,0 @@
-using VectorNNTPD.StorageService.Ingress;
-
-namespace VectorNNTPD.StorageService.Listener
-{
-
- ///
- /// Exactly-once ownership of a queue reservation bound to a STORE stream.
- ///
- ///
- /// Bind, release, and commit-take share an atomic ownership flag so concurrent
- /// RemoveStore, authorization failure, and TryCommit paths cannot both
- /// release or both consume the same slot. The hold must be taken before
- /// so teardown cannot observe a still-bound
- /// reservation after the queue has already consumed it.
- ///
- internal sealed class StoreReservationHold
- {
- private IAcceptedArticleQueue? _queue;
- private QueueReservation _reservation;
- private int _bound;
-
- /// Gets whether a reservation is currently bound to this hold.
- public bool IsBound => Volatile.Read(ref _bound) != 0;
-
- /// Gets the bound reservation. Meaningful only while is true.
- public QueueReservation Reservation => _reservation;
-
- /// Gets the queue that owns the bound reservation.
- public IAcceptedArticleQueue? Queue => _queue;
-
- ///
- /// Records a reserved queue slot and the queue that must release it.
- ///
- ///
- /// Callers must serialize this method with and
- /// (the session _storeGate) so a concurrent
- /// teardown cannot observe as false while
- /// and are only partially published.
- ///
- public void Bind(IAcceptedArticleQueue? queue, QueueReservation reservation)
- {
- _queue = queue;
- _reservation = reservation;
- // Publish ownership last; paired take/release uses Interlocked.Exchange.
- Volatile.Write(ref _bound, queue is not null ? 1 : 0);
- }
-
- ///
- /// Releases the bound reservation if this caller wins ownership.
- ///
- ///
- /// when this call performed the release;
- /// when there was no bound reservation or another caller already took it.
- ///
- public bool TryRelease()
- {
- if (!TryTake(out var queue, out var reservation))
- {
- return false;
- }
-
- queue?.Release(reservation);
- return true;
- }
-
- ///
- /// Takes exclusive ownership for a commit attempt without releasing the reservation.
- ///
- ///
- /// when this caller now owns and
- /// and must either
- /// or them;
- /// when another path already released or took the reservation.
- ///
- public bool TryTakeForCommit(out IAcceptedArticleQueue? queue, out QueueReservation reservation)
- {
- return TryTake(out queue, out reservation);
- }
-
- ///
- /// Records whether a taken reservation was committed into the queue.
- /// Does not call .
- ///
- ///
- /// when returned true.
- ///
- public void MarkCommitOutcome(bool committed)
- {
- Committed = committed;
- }
-
- /// Gets whether the reservation was committed into the queue.
- public bool Committed { get; private set; }
-
- private bool TryTake(out IAcceptedArticleQueue? queue, out QueueReservation reservation)
- {
- if (Interlocked.Exchange(ref _bound, 0) == 0)
- {
- queue = null;
- reservation = default;
- return false;
- }
-
- queue = _queue;
- reservation = _reservation;
- _queue = null;
- return true;
- }
- }
-}
diff --git a/src/VectorNNTPD.StorageService/Listener/IStorageVatpTransport.cs b/src/VectorNNTPD.StorageService/Listener/StreamStorageListenerTransport.cs
similarity index 86%
rename from src/VectorNNTPD.StorageService/Listener/IStorageVatpTransport.cs
rename to src/VectorNNTPD.StorageService/Listener/StreamStorageListenerTransport.cs
index 7228c870..93b016f8 100644
--- a/src/VectorNNTPD.StorageService/Listener/IStorageVatpTransport.cs
+++ b/src/VectorNNTPD.StorageService/Listener/StreamStorageListenerTransport.cs
@@ -2,40 +2,13 @@
namespace VectorNNTPD.StorageService.Listener
{
-
- /// One established connected transport owned by a StorageServer VATP session.
- public interface IStorageVatpTransport : IAsyncDisposable
- {
- /// Reads bytes. Zero means the peer closed.
- ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken);
-
- /// Writes bytes and returns how many were accepted.
- ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken);
-
- ///
- /// Suspends or resumes the read no-progress timeout.
- /// STORE admission suspends it while the client must stay silent, then resumes it
- /// when that wait ends so a later stall still times out. The default does nothing.
- ///
- /// to disarm the outstanding read timer.
- void SetReadProgressSuspended(bool suspended) { }
- }
-
///
/// Stream adapter that applies the configured I/O no-progress timeout to each read or write.
/// Successive reads reuse one timeout source, and successive writes reuse another.
- /// One read may overlap one write. STORE admission does that when it grants WINDOW while the
- /// session is blocked waiting for the next client frame. Two overlapping reads, or two
- /// overlapping writes, are not supported. A read and a write keep separate timeout sources,
- /// so a WINDOW write that passes cannot cancel the read.
+ /// One read may overlap one write. Queue admission may suspend the read timer while waiting
+ /// for capacity. Two overlapping reads, or two overlapping writes, are not supported.
///
- ///
- /// Timeout CTS and dispose state are per transport instance. I/O-split and TLS thread-cost
- /// counters are process-wide aggregates for bench observation (same model as
- /// receive metrics): every live transport contributes through
- /// updates, and clears the process totals.
- ///
- public sealed class StreamStorageVatpTransport : IStorageVatpTransport
+ public sealed class StreamStorageListenerTransport : IStorageListenerTransport
{
private readonly Stream _stream;
private readonly TimeSpan _ioProgressTimeout;
@@ -178,7 +151,7 @@ private static void NoteWriteTlsThreadCost(int threadId, long cpuKernel, long cp
}
/// Initializes a stream-backed transport.
- public StreamStorageVatpTransport(Stream stream, TimeSpan ioProgressTimeout, bool leaveInnerStreamOpen = false)
+ public StreamStorageListenerTransport(Stream stream, TimeSpan ioProgressTimeout, bool leaveInnerStreamOpen = false)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(ioProgressTimeout, TimeSpan.Zero);
@@ -216,7 +189,7 @@ public async ValueTask ReadAsync(Memory buffer, CancellationToken can
}
catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
{
- throw new TimeoutException($"Storage VATP transport read exceeded no-progress timeout of {_ioProgressTimeout}.");
+ throw new TimeoutException($"Storage NNTP transport read exceeded no-progress timeout of {_ioProgressTimeout}.");
}
}
@@ -245,7 +218,7 @@ public async ValueTask WriteAsync(ReadOnlyMemory buffer, Cancellation
}
catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
{
- throw new TimeoutException($"Storage VATP transport write exceeded no-progress timeout of {_ioProgressTimeout}.");
+ throw new TimeoutException($"Storage NNTP transport write exceeded no-progress timeout of {_ioProgressTimeout}.");
}
}
diff --git a/src/VectorNNTPD.StorageService/Listener/VatpStoreOutcome.cs b/src/VectorNNTPD.StorageService/Listener/VatpStoreOutcome.cs
deleted file mode 100644
index 972fcab3..00000000
--- a/src/VectorNNTPD.StorageService/Listener/VatpStoreOutcome.cs
+++ /dev/null
@@ -1,20 +0,0 @@
-namespace VectorNNTPD.StorageService.Listener
-{
-
- /// VATP RESULT outcome byte written after an article is queued.
- public static class VatpStoreOutcome
- {
- ///
- /// The Path-rewritten article was accepted into the in-memory queue.
- /// This is the historical ArticleAcceptOutcome.Accepted value (1).
- ///
- public const byte Accepted = 1;
-
- ///
- /// The article cannot fit in the queue budget, so the body is not authorized.
- /// This is the historical ArticleAcceptOutcome.RejectedCapacity value (6).
- /// A full queue does not use this outcome. The server withholds WINDOW until a reservation fits.
- ///
- public const byte RejectedCapacity = 6;
- }
-}
diff --git a/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs b/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs
index 1be41785..f46d5cc0 100644
--- a/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs
+++ b/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs
@@ -18,11 +18,11 @@ namespace VectorNNTPD.StorageService.Storage.ArticleIndex
/// Startup creates DbDir when it is missing and fails if that directory cannot be used.
/// It does not substitute a spool directory. Membership is loaded from the open databases,
/// and only then does return. Later application services, including the
- /// accepted-article worker and the VATP listener, start after that.
+ /// accepted-article worker and the NNTP feed listener, start after that.
/// Startup does not walk article files or compare them with index rows.
///
///
- /// VATP RESULT is still sent when an article is queued, before the spool write and before this index.
+ /// TAKETHIS 239 is still sent when an article is queued, before the spool write and before this index.
/// A committed index row is durable across process and power loss because each writer uses WAL and
/// synchronous=FULL. An access update that is only queued is not durable until its batch commits.
/// Eviction queries skip a row whose queued access time is newer than the committed time.
diff --git a/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs b/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs
index deea82f5..41f752a9 100644
--- a/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs
+++ b/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs
@@ -7,7 +7,7 @@ namespace VectorNNTPD.StorageService.Storage.SegmentPools
{
///
- /// Hard startup gate. Completes, or throws, before the accepted-article worker, VATP listener,
+ /// Hard startup gate. Completes, or throws, before the accepted-article worker, NNTP feed listener,
/// and RabbitMQ advertisement publisher are started. Thresholds are not enforced here.
///
internal sealed class SegmentPoolValidationService : IApplicationService
diff --git a/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs b/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs
index 0aab9f19..4ba97c4a 100644
--- a/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs
+++ b/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs
@@ -13,7 +13,7 @@ namespace VectorNNTPD.StorageService.Storage.SpoolPersistence
///
/// Rewrites Path, publishes the rewritten bytes to a selected spool, and then indexes the article.
- /// A successful VATP enqueue has already happened. This processor does not change that.
+ /// A successful feed enqueue has already happened. This processor does not change that.
/// The index insert runs only after a stored or duplicate publish and waits for the SQLite commit.
///
internal sealed class SpoolArticlePersistenceProcessor : IAcceptedArticleProcessor
diff --git a/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs b/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs
index 8620ad34..f7a113de 100644
--- a/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs
+++ b/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs
@@ -22,7 +22,7 @@ internal static class SpoolArticlePath
/// Relative path {n0}/{n1}/{n2}/{64 lowercase hex}.
/// The three directory nibbles are the first three characters of the canonical lowercase hex digest.
///
- /// Canonical VATP article identity.
+ /// Canonical article identity.
/// A relative path with no parent segments.
internal static string RelativePath(ArticleId articleId)
{
@@ -38,7 +38,7 @@ internal static string RelativePath(ArticleId articleId)
/// The normalized root prefix is cached for the life of the process.
///
/// Absolute spool directory.
- /// Canonical VATP article identity.
+ /// Canonical article identity.
/// The absolute article path, using the platform directory separator.
internal static string CombineArticle(string spoolRoot, ArticleId articleId)
{
diff --git a/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs b/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs
index 27d0fa74..036231af 100644
--- a/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs
+++ b/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs
@@ -14,7 +14,7 @@ namespace VectorNNTPD.StorageService.Storage
/// Uses the shared connection; does not own TCP lifecycle.
/// Heartbeat publication must not block clean shutdown.
/// The periodic advertisement is the readiness signal. It starts only after this service starts,
- /// which is after the VATP listener is accepting. Stop publishes one transient Draining
+ /// which is after the NNTP feed listener is accepting. Stop publishes one transient Draining
/// announcement after the loop has stopped and before the channel is disposed. A failed Draining
/// publication is logged and does not block shutdown past .
///
diff --git a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs
index 92434ccb..ae039433 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs
@@ -41,7 +41,7 @@ public async Task Bind_failure_before_publication_leaves_existing_records()
await Assert.ThrowsAsync(() => listener.StartAsync(CancellationToken.None));
Assert.Equal(0, published);
- Assert.Equal(StorageVatpListenerState.Stopped, listener.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, listener.State);
await harness.Service.StopAsync(CancellationToken.None);
Assert.Equal(before, Ids(harness.Client));
@@ -73,7 +73,7 @@ public async Task Failure_after_registration_completes_removes_the_owning_instan
{
var harness = CreateHarness(HealthyAddresses);
var publisher = new FakeApplicationService(
- "StorageVatpListener",
+ "StorageNntpListener",
onStart: cancellationToken => harness.Service.PublishAfterListenerBoundAsync(cancellationToken));
var later = new FakeApplicationService(
"later",
@@ -96,7 +96,7 @@ public async Task Graceful_shutdown_of_the_owning_instance_removes_its_records()
{
var harness = CreateHarness(HealthyAddresses);
var listener = new FakeApplicationService(
- "StorageVatpListener",
+ "StorageNntpListener",
onStart: cancellationToken => harness.Service.PublishAfterListenerBoundAsync(cancellationToken));
var manager = new ApplicationServiceManager(
[new CloudflareDnsReconciliationApplicationService(harness.Service), listener],
@@ -139,7 +139,7 @@ public async Task Second_instance_bind_failure_leaves_the_healthy_instance_recor
await intruder.Service.StopAsync(CancellationToken.None);
Assert.Equal(0, published);
- Assert.Equal(StorageVatpListenerState.Running, ownerListener.State);
+ Assert.Equal(StorageNntpListenerState.Running, ownerListener.State);
Assert.Equal(owned, Ids(client));
await ownerListener.StopAsync(CancellationToken.None);
@@ -293,7 +293,7 @@ private static CloudflareDnsRecord Record(string id, string type, string fqdn, s
Proxied = false,
};
- private static StorageVatpListenerService CreateListener(int port, Func publish)
+ private static StorageNntpListenerService CreateListener(int port, Func publish)
{
var identity = StorageServerTestOptions.CreateValid();
identity.BindAddress = ["127.0.0.1"];
@@ -302,12 +302,11 @@ private static StorageVatpListenerService CreateListener(int port, Func.Instance,
+ NullLogger.Instance,
publishAfterBind: publish);
}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs
index b2cfc19d..4d72aae9 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs
@@ -35,7 +35,7 @@ public sealed class StorageServerFleetLifecycleOrderTests
public async Task AdvertisementPublisher_DoesNotStartBeforeVatpListener()
{
await using var session = await FleetSession.StartHeldAtListenerAsync();
- Assert.NotEqual(StorageVatpListenerState.Running, session.Listener.State);
+ Assert.NotEqual(StorageNntpListenerState.Running, session.Listener.State);
Assert.Equal(0, session.Publisher.PublishedCount);
Assert.Null(session.Publisher.Execution);
Assert.DoesNotContain(
@@ -47,7 +47,7 @@ public async Task AdvertisementPublisher_DoesNotStartBeforeVatpListener()
public async Task AdvertisementPublisher_StartsAfterVatpListener()
{
await using var session = await FleetSession.StartAsync();
- Assert.Equal(StorageVatpListenerState.Running, session.Listener.State);
+ Assert.Equal(StorageNntpListenerState.Running, session.Listener.State);
await WaitForAsync(() => session.Publisher.PublishedCount >= 1, TimeSpan.FromSeconds(5));
var publications = session.Connection.PublishChannels.SelectMany(static channel => channel.Publications).ToArray();
Assert.Contains(publications, static publication => IsAdvertisement(publication));
@@ -61,7 +61,7 @@ public async Task AdvertisementPublisher_StopsBeforeVatpListener()
await WaitForAsync(() => session.Publisher.PublishedCount >= 1, TimeSpan.FromSeconds(5));
await session.StopAsync();
var publisherEnd = session.StopEvents.IndexOf("end:StorageServerAdvertisementPublisher");
- var listenerBegin = session.StopEvents.IndexOf("begin:StorageVatpListener");
+ var listenerBegin = session.StopEvents.IndexOf("begin:StorageNntpListener");
Assert.True(publisherEnd >= 0);
Assert.True(listenerBegin > publisherEnd);
Assert.Equal(session.PublishedCountWhenListenerStopBegan, session.Publisher.PublishedCount);
@@ -80,7 +80,7 @@ public async Task StartupFailure_DoesNotAdvertise()
Assert.NotNull(failure);
Assert.Equal(0, session.Publisher.PublishedCount);
Assert.Null(session.Publisher.Execution);
- Assert.NotEqual(StorageVatpListenerState.Running, session.Listener.State);
+ Assert.NotEqual(StorageNntpListenerState.Running, session.Listener.State);
Assert.DoesNotContain(
session.Connection.PublishChannels.SelectMany(static channel => channel.Publications),
static publication => IsDraining(publication));
@@ -140,7 +140,7 @@ private sealed class FleetSession : IAsyncDisposable
private FleetSession(
IHost host,
- StorageVatpListenerService listener,
+ StorageNntpListenerService listener,
StorageServerAdvertisementPublisherService publisher,
RecordingStorageServerRabbitMqConnectionFactory factory,
bool held)
@@ -156,7 +156,7 @@ private FleetSession(
public ApplicationServiceManager Manager { get; private set; } = null!;
- public StorageVatpListenerService Listener { get; }
+ public StorageNntpListenerService Listener { get; }
public StorageServerAdvertisementPublisherService Publisher { get; }
@@ -236,16 +236,16 @@ private static Task CreateAsync(int port, bool holdListener, bool
var host = CreateHost(port, factory);
var ordered = host.Services.GetServices().ToArray();
AssertProductionOrder(ordered);
- var listener = host.Services.GetRequiredService();
+ var listener = host.Services.GetRequiredService();
var publisher = host.Services.GetRequiredService();
var session = new FleetSession(host, listener, publisher, factory, holdListener);
var services = ordered.Select(service => service switch
{
- StorageVatpListenerService when holdListener => (IApplicationService)new HoldListener(
+ StorageNntpListenerService when holdListener => (IApplicationService)new HoldListener(
listener,
session.ListenerEntered,
session._releaseListener),
- StorageVatpListenerService when traceStops => new StopTrace(
+ StorageNntpListenerService when traceStops => new StopTrace(
service,
session.StopEvents,
() =>
@@ -268,7 +268,7 @@ private static Task CreateAsync(int port, bool holdListener, bool
private static void AssertProductionOrder(IApplicationService[] ordered)
{
- var listenerIndex = Array.FindIndex(ordered, static service => service is StorageVatpListenerService);
+ var listenerIndex = Array.FindIndex(ordered, static service => service is StorageNntpListenerService);
var publisherIndex = Array.FindIndex(
ordered,
static service => service is StorageServerAdvertisementPublisherService);
@@ -328,12 +328,12 @@ private static void ReplaceAcme(IServiceCollection services)
private sealed class HoldListener : IApplicationService
{
- private readonly StorageVatpListenerService _inner;
+ private readonly StorageNntpListenerService _inner;
private readonly TaskCompletionSource _entered;
private readonly TaskCompletionSource _release;
public HoldListener(
- StorageVatpListenerService inner,
+ StorageNntpListenerService inner,
TaskCompletionSource entered,
TaskCompletionSource release)
{
diff --git a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs
index 4fdb887d..624b003b 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs
@@ -68,10 +68,10 @@ public void AddStorageServerHosting_registers_expected_service_graph()
Assert.IsType(application[7]);
Assert.IsType(application[8]);
Assert.Same(host.Services.GetRequiredService(), application[9]);
- Assert.Same(host.Services.GetRequiredService(), application[10]);
+ Assert.Same(host.Services.GetRequiredService(), application[10]);
Assert.IsType(application[11]);
Assert.Equal(1, application.Count(static service => service is AcceptedArticleWorker));
- Assert.IsType(host.Services.GetRequiredService());
+ Assert.IsType(host.Services.GetRequiredService());
Assert.IsType(host.Services.GetRequiredService());
Assert.IsType(host.Services.GetRequiredService());
Assert.NotNull(host.Services.GetRequiredService());
@@ -145,8 +145,8 @@ public async Task Pool_validation_failure_stops_the_host_before_the_listener_and
Assert.NotEqual(ApplicationState.Running, lifecycle.State);
Assert.Equal(ApplicationState.Stopped, lifecycle.State);
- var listener = host.Services.GetRequiredService();
- Assert.Equal(StorageVatpListenerState.Created, listener.State);
+ var listener = host.Services.GetRequiredService();
+ Assert.Equal(StorageNntpListenerState.Created, listener.State);
Assert.Equal(0, host.Services.GetRequiredService().PublishedCount);
var application = host.Services.GetServices().ToArray();
@@ -165,7 +165,7 @@ public async Task Shard_initialization_failure_stops_the_host_before_the_listene
var lifecycle = host.Services.GetRequiredService();
Assert.Equal(ApplicationState.Stopped, lifecycle.State);
- Assert.Equal(StorageVatpListenerState.Created, host.Services.GetRequiredService().State);
+ Assert.Equal(StorageNntpListenerState.Created, host.Services.GetRequiredService().State);
Assert.Equal(0, host.Services.GetRequiredService().PublishedCount);
var application = host.Services.GetServices().ToArray();
Assert.Null(application.OfType().Single().Execution);
@@ -194,10 +194,10 @@ public async Task Host_starts_application_services_in_order_when_acme_and_rabbit
Assert.IsType(application[7]);
Assert.IsType(application[8]);
Assert.IsType(application[9]);
- var listener = host.Services.GetRequiredService();
+ var listener = host.Services.GetRequiredService();
Assert.Same(listener, application[10]);
Assert.IsType(application[11]);
- Assert.Equal(StorageVatpListenerState.Running, listener.State);
+ Assert.Equal(StorageNntpListenerState.Running, listener.State);
Assert.True(host.Services.GetRequiredService().IsReady);
Assert.True(host.Services.GetRequiredService().IsReady);
Assert.NotNull(host.Services.GetRequiredService());
diff --git a/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs b/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs
index 9bf53995..38deb48a 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs
@@ -41,11 +41,11 @@ public async Task ApplicationServiceManager_starts_in_registration_order_and_rol
return Task.CompletedTask;
});
var listener = new FakeApplicationService(
- "StorageVatpListener",
+ "StorageNntpListener",
onStart: _ => throw new InvalidOperationException("listener-fail"),
onStop: _ =>
{
- stopped.Add("StorageVatpListener");
+ stopped.Add("StorageNntpListener");
return Task.CompletedTask;
});
@@ -68,7 +68,7 @@ public async Task Graceful_shutdown_stops_in_reverse_order()
{
new FakeApplicationService("CloudflareDns", onStop: _ => { stopped.Add("CloudflareDns"); return Task.CompletedTask; }),
new FakeApplicationService("AcmeCertificate", onStop: _ => { stopped.Add("AcmeCertificate"); return Task.CompletedTask; }),
- new FakeApplicationService("StorageVatpListener", onStop: _ => { stopped.Add("StorageVatpListener"); return Task.CompletedTask; }),
+ new FakeApplicationService("StorageNntpListener", onStop: _ => { stopped.Add("StorageNntpListener"); return Task.CompletedTask; }),
};
var options = StorageServerTestOptions.CreateValid();
var manager = new ApplicationServiceManager(services, options, NullLogger.Instance);
@@ -78,7 +78,7 @@ public async Task Graceful_shutdown_stops_in_reverse_order()
Assert.Equal(ApplicationState.Running, lifecycle.State);
await lifecycle.StopAsync(CancellationToken.None);
Assert.Equal(ApplicationState.Stopped, lifecycle.State);
- Assert.Equal(["StorageVatpListener", "AcmeCertificate", "CloudflareDns"], stopped);
+ Assert.Equal(["StorageNntpListener", "AcmeCertificate", "CloudflareDns"], stopped);
}
[Fact]
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs
index 0fb2e783..657c71f6 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs
@@ -1,5 +1,4 @@
using System.Diagnostics;
-using System.Net.Security;
using System.Text;
using Microsoft.Extensions.Logging.Abstractions;
using VectorNNTPD.Common.Articles;
@@ -9,9 +8,8 @@
namespace VectorNNTPD.StorageService.Tests.Listener
{
-
///
- /// Measures against the StorageServer ingress listener.
+ /// Measures against the StorageServer NNTP listener.
/// Runs only when VECTORNNTPD_STORAGE_PLACEMENT_BENCH=1.
///
public sealed class ArticlePlacementClientBench
@@ -28,6 +26,7 @@ public async Task One_connection_compares_one_and_two_outstanding_stores()
_output.WriteLine("Set VECTORNNTPD_STORAGE_PLACEMENT_BENCH=1 to measure ArticlePlacementClient.");
return;
}
+
var seconds = 8;
var configured = Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_PLACEMENT_SECONDS");
if (int.TryParse(configured, out var parsed) && parsed is >= 1 and <= 120)
@@ -36,14 +35,14 @@ public async Task One_connection_compares_one_and_two_outstanding_stores()
}
var record = CreateRecord();
- _output.WriteLine($"artBytes={record.ArtSize} bodyBytes={StorageVatpThroughputBench.BodyBytes} window=1048576");
+ _output.WriteLine($"artBytes={record.ArtSize} bodyBytes={StorageNntpThroughputBench.BodyBytes}");
_output.WriteLine(await MeasureAsync(record, outstanding: 1, TimeSpan.FromSeconds(seconds)));
_output.WriteLine(await MeasureAsync(record, outstanding: 2, TimeSpan.FromSeconds(seconds)));
}
private async Task MeasureAsync(ArticleRecord record, int outstanding, TimeSpan duration)
{
- await using var host = await StorageVatpThroughputBench.StartPlacementIngressHostAsync();
+ await using var host = await StorageNntpThroughputBench.StartPlacementIngressHostAsync();
await using var client = new ArticlePlacementClient(
NullLogger.Instance,
outstanding)
@@ -51,36 +50,16 @@ private async Task MeasureAsync(ArticleRecord record, int outstanding, T
TestTcpConnectHost = "127.0.0.1",
ServerCertificateValidationCallback = static (_, _, _, _) => true,
};
- var target = new StorageServerFleetEntry(
- 1,
- "cache01.usenet.ninja",
- 10_000,
- 0,
- 10_000,
- DateTimeOffset.UtcNow,
- host.Port);
- var warmup = await client.PlaceAsync(record, target, CancellationToken.None);
+ var warmup = await client.PlaceAsync(record, host.Target, CancellationToken.None);
if (warmup.Kind != ArticlePlacementKind.Accepted || client.ConnectCount != 1)
{
throw new InvalidOperationException($"Warmup failed kind={warmup.Kind} connects={client.ConnectCount} failure={warmup.Failure}");
}
+ _ = await host.Queue.DequeueAsync(CancellationToken.None);
+
var accepted = 0;
- var duplicate = 0;
- var rejected = 0;
var failed = 0;
- var ambiguous = 0;
- var cancelled = 0;
- var payload = 0L;
- var inflight = 0;
- var peak = 0;
- var queueMax = 0L;
- var allocBefore = GC.GetTotalAllocatedBytes(precise: true);
- var gc0 = GC.CollectionCount(0);
- var gc1 = GC.CollectionCount(1);
- var gc2 = GC.CollectionCount(2);
- using var process = Process.GetCurrentProcess();
- var cpu = process.TotalProcessorTime;
var clock = Stopwatch.StartNew();
var workers = new Task[outstanding];
for (var worker = 0; worker < outstanding; worker++)
@@ -89,123 +68,42 @@ private async Task MeasureAsync(ArticleRecord record, int outstanding, T
{
while (clock.Elapsed < duration)
{
- var now = Interlocked.Increment(ref inflight);
- int seen;
- do
+ var result = await client.PlaceAsync(record, host.Target, CancellationToken.None);
+ if (result.Kind == ArticlePlacementKind.Accepted)
{
- seen = Volatile.Read(ref peak);
- if (now <= seen)
- {
- break;
- }
- }
- while (Interlocked.CompareExchange(ref peak, now, seen) != seen);
-
- try
- {
- var result = await client.PlaceAsync(record, target, CancellationToken.None).ConfigureAwait(false);
- var queued = host.Queue.QueuedBytes;
- long previousQueue;
- do
- {
- previousQueue = Volatile.Read(ref queueMax);
- if (queued <= previousQueue)
- {
- break;
- }
- }
- while (Interlocked.CompareExchange(ref queueMax, queued, previousQueue) != previousQueue);
-
- if (result.Kind == ArticlePlacementKind.Accepted)
- {
- Interlocked.Increment(ref accepted);
- Interlocked.Add(ref payload, record.ArtSize);
- }
- else if (result.Kind == ArticlePlacementKind.Duplicate)
- {
- Interlocked.Increment(ref duplicate);
- Interlocked.Add(ref payload, record.ArtSize);
- }
- else if (result.Kind == ArticlePlacementKind.AcknowledgementNotObserved)
- {
- Interlocked.Increment(ref ambiguous);
- }
- else if (result.Kind == ArticlePlacementKind.Cancelled)
- {
- Interlocked.Increment(ref cancelled);
- }
- else if (result.Succeeded)
- {
- Interlocked.Increment(ref accepted);
- }
- else if (result.Kind is ArticlePlacementKind.RejectedInvalid or ArticlePlacementKind.RejectedPressure or ArticlePlacementKind.RejectedCapacity or ArticlePlacementKind.Conflict)
- {
- Interlocked.Increment(ref rejected);
- }
- else
- {
- Interlocked.Increment(ref failed);
- }
+ Interlocked.Increment(ref accepted);
+ _ = await host.Queue.DequeueAsync(CancellationToken.None);
}
- finally
+ else
{
- Interlocked.Decrement(ref inflight);
+ Interlocked.Increment(ref failed);
}
}
});
}
await Task.WhenAll(workers);
- clock.Stop();
- var counters = client.Snapshot();
- var seconds = clock.Elapsed.TotalSeconds;
- var artGbps = payload * 8 / seconds / 1_000_000_000d;
- var bodyGbps = (accepted + duplicate) * (long)StorageVatpThroughputBench.BodyBytes * 8 / seconds / 1_000_000_000d;
- var articlesPerSecond = (accepted + duplicate) / seconds;
- var alloc = GC.GetTotalAllocatedBytes(precise: true) - allocBefore;
- var cpuMs = (process.TotalProcessorTime - cpu).TotalMilliseconds;
- var resultUs = counters.ResultCount == 0
- ? 0
- : counters.ResultTicks * 1_000_000d / Stopwatch.Frequency / counters.ResultCount;
- var resultMaxUs = counters.ResultMaxTicks * 1_000_000d / Stopwatch.Frequency;
- var slotMs = counters.SlotWaitTicks * 1000d / Stopwatch.Frequency;
- var creditMs = counters.CreditWaitTicks * 1000d / Stopwatch.Frequency;
- return
- $"outstanding={outstanding} seconds={seconds:F3} artGbps={artGbps:F3} bodyGbps={bodyGbps:F3} articlesPerSec={articlesPerSecond:F1} " +
- $"connects={client.ConnectCount} callerPeak={Volatile.Read(ref peak)} connectionPeak={counters.PeakActive} " +
- $"accepted={Volatile.Read(ref accepted)} duplicate={Volatile.Read(ref duplicate)} rejected={Volatile.Read(ref rejected)} " +
- $"failed={Volatile.Read(ref failed)} ambiguous={Volatile.Read(ref ambiguous)} cancelled={Volatile.Read(ref cancelled)} " +
- $"resultAvgUs={resultUs:F1} resultMaxUs={resultMaxUs:F1} slotWaitMs={slotMs:F1} creditWaitMs={creditMs:F1} " +
- $"queueMaxBytes={Volatile.Read(ref queueMax)} allocBytes={alloc} gc={GC.CollectionCount(0) - gc0}/{GC.CollectionCount(1) - gc1}/{GC.CollectionCount(2) - gc2} cpuMs={cpuMs:F0}";
+ var snapshot = client.Snapshot();
+ return $"outstanding={outstanding} accepted={accepted} failed={failed} " +
+ $"peak={snapshot.PeakActive} responseWaitMs={snapshot.CreditWaitTicks * 1000.0 / Stopwatch.Frequency:F1}";
}
private static ArticleRecord CreateRecord()
{
- var body = new byte[StorageVatpThroughputBench.BodyBytes];
- for (var offset = 0; offset < body.Length; offset += 80)
- {
- body.AsSpan(offset, 78).Fill((byte)'x');
- body[offset + 78] = (byte)'\r';
- body[offset + 79] = (byte)'\n';
- }
-
- var header = Encoding.ASCII.GetBytes(
+ var text =
"Path: peer.example\r\n" +
"Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" +
- "Message-ID: \r\n" +
+ "Message-ID: \r\n" +
"Newsgroups: alt.test\r\n" +
"From: user@example.test\r\n" +
"Subject: placement-bench\r\n" +
- "\r\n");
- var article = new byte[header.Length + body.Length];
- header.CopyTo(article, 0);
- body.CopyTo(article, header.Length);
- var created = ArticleRecordFactory.TryCreate(new NntpArticleParser("nntpd01.usenet.ninja"), article);
- if (!created.IsAccepted)
- {
- throw new InvalidOperationException("Placement benchmark article was not accepted.");
- }
-
+ "\r\n" +
+ new string('y', StorageNntpThroughputBench.BodyBytes) + "\r\n";
+ var created = ArticleRecordFactory.TryCreate(
+ new NntpArticleParser("nntpd01.usenet.ninja"),
+ Encoding.ASCII.GetBytes(text),
+ ArticlePathMode.Normalize);
+ Assert.True(created.IsAccepted);
return created.Record;
}
}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpFeedSessionTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpFeedSessionTests.cs
new file mode 100644
index 00000000..d1608e88
--- /dev/null
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpFeedSessionTests.cs
@@ -0,0 +1,400 @@
+using System.Text;
+using System.Threading.Channels;
+using Microsoft.Extensions.Logging.Abstractions;
+using VectorNNTPD.Common.Articles;
+using VectorNNTPD.Common.Articles.Parsing;
+using VectorNNTPD.Common.Core;
+using VectorNNTPD.StorageService.Configuration;
+using VectorNNTPD.StorageService.Ingress;
+using VectorNNTPD.StorageService.Listener;
+
+namespace VectorNNTPD.StorageService.Tests.Listener
+{
+ public sealed class StorageNntpFeedSessionTests
+ {
+ private const string CacheFqdn = "cache01.usenet.ninja";
+ private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10);
+
+ [Fact]
+ public async Task TakeThis_enqueues_article_and_returns_239()
+ {
+ var wire = BuildWireArticle("", "body-line\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+
+ var status = await harness.TakeThisAsync(wire.MessageId, wire.Stuffed).WaitAsync(Safety);
+ Assert.StartsWith("239 ", status, StringComparison.Ordinal);
+ Assert.Contains(wire.MessageId, status, StringComparison.Ordinal);
+
+ var delivery = await queue.DequeueAsync(CancellationToken.None).AsTask().WaitAsync(Safety);
+ Assert.True(delivery.HasArticle);
+ Assert.Equal(wire.ArtId, delivery.Article.ArtId);
+ Assert.True(delivery.Article.ArtData.Span.SequenceEqual(wire.Canonical.Span));
+ Assert.Equal(0, CountHop(Encoding.ASCII.GetString(delivery.Article.ArtData.Span), CacheFqdn));
+ }
+
+ [Fact]
+ public async Task TakeThis_239_is_sent_before_persistence_finishes()
+ {
+ var wire = BuildWireArticle("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var processor = new BlockingProcessor(release.Task);
+ var worker = new AcceptedArticleWorker(
+ queue,
+ processor,
+ NullLogger.Instance,
+ new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 },
+ new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(),
+ new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(),
+ TimeProvider.System);
+ await worker.StartAsync(CancellationToken.None);
+ try
+ {
+ await using var harness = await Harness.StartAsync(queue);
+ var status = await harness.TakeThisAsync(wire.MessageId, wire.Stuffed).WaitAsync(TimeSpan.FromSeconds(2));
+ Assert.StartsWith("239 ", status, StringComparison.Ordinal);
+ Assert.Equal(0, processor.Finished);
+ }
+ finally
+ {
+ release.TrySetResult();
+ await worker.StopAsync(CancellationToken.None);
+ }
+ }
+
+ [Fact]
+ public async Task TakeThis_message_id_mismatch_returns_439_and_does_not_enqueue()
+ {
+ var wire = BuildWireArticle("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+
+ var status = await harness.TakeThisAsync("", wire.Stuffed).WaitAsync(Safety);
+ Assert.StartsWith("439 ", status, StringComparison.Ordinal);
+ Assert.False(queue.TryDequeue(out _));
+ }
+
+ [Fact]
+ public async Task Disconnect_during_article_does_not_enqueue()
+ {
+ var wire = BuildWireArticle("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+
+ await harness.WriteClientAsync(Encoding.ASCII.GetBytes($"TAKETHIS {wire.MessageId}\r\n"));
+ await harness.WriteClientAsync(wire.Stuffed[..Math.Min(32, wire.Stuffed.Length)]);
+ await harness.DisposeTransportAsync();
+
+ await Task.Delay(50);
+ Assert.False(queue.TryDequeue(out _));
+ }
+
+ [Fact]
+ public async Task Concurrent_sessions_enqueue_without_cross_contamination()
+ {
+ var left = BuildWireArticle("", "left-body\r\n");
+ var right = BuildWireArticle("", "right-body\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var a = await Harness.StartAsync(queue);
+ await using var b = await Harness.StartAsync(queue);
+
+ var leftTask = a.TakeThisAsync(left.MessageId, left.Stuffed);
+ var rightTask = b.TakeThisAsync(right.MessageId, right.Stuffed);
+ var leftStatus = await leftTask.WaitAsync(Safety);
+ var rightStatus = await rightTask.WaitAsync(Safety);
+ Assert.StartsWith("239 ", leftStatus, StringComparison.Ordinal);
+ Assert.StartsWith("239 ", rightStatus, StringComparison.Ordinal);
+
+ var first = await queue.DequeueAsync(CancellationToken.None).AsTask().WaitAsync(Safety);
+ var second = await queue.DequeueAsync(CancellationToken.None).AsTask().WaitAsync(Safety);
+ Assert.True(first.HasArticle && second.HasArticle);
+ var ids = new HashSet { first.Article.ArtId, second.Article.ArtId };
+ Assert.Contains(left.ArtId, ids);
+ Assert.Contains(right.ArtId, ids);
+ }
+
+ [Fact]
+ public async Task Check_then_mode_stream_then_capabilities_are_accepted()
+ {
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.StartsWith("101 ", await harness.CommandAsync("CAPABILITIES\r\n").WaitAsync(Safety), StringComparison.Ordinal);
+ Assert.StartsWith("203 ", await harness.CommandAsync("MODE STREAM\r\n").WaitAsync(Safety), StringComparison.Ordinal);
+ Assert.StartsWith("238 ", await harness.CommandAsync("CHECK \r\n").WaitAsync(Safety), StringComparison.Ordinal);
+ }
+
+ private static int CountHop(string article, string hop)
+ {
+ var marker = hop + "!";
+ var count = 0;
+ var index = 0;
+ while ((index = article.IndexOf(marker, index, StringComparison.Ordinal)) >= 0)
+ {
+ count++;
+ index += marker.Length;
+ }
+
+ return count;
+ }
+
+ private static BuiltWire BuildWireArticle(string messageId, string body, string ingressFqdn = "nntpd01.usenet.ninja")
+ {
+ var text =
+ $"Path: {ingressFqdn}!not-for-mail\r\n" +
+ $"From: demo@example.com\r\n" +
+ $"Newsgroups: misc.test\r\n" +
+ $"Subject: test\r\n" +
+ $"Date: Fri, 23 Oct 2026 12:00:00 +0000\r\n" +
+ $"Message-ID: {messageId}\r\n" +
+ "\r\n" +
+ body;
+ var destuffed = Encoding.ASCII.GetBytes(text);
+ var parser = new NntpArticleParser(CacheFqdn);
+ var created = ArticleRecordFactory.TryCreate(parser, destuffed, ArticlePathMode.Normalize);
+ Assert.True(created.IsAccepted);
+ var record = created.Record;
+ // Wire form for TAKETHIS: destuffed article with body lines dot-stuffed for transfer.
+ var stuffed = StuffForWire(destuffed);
+ return new BuiltWire(messageId, record.ArtId, record.ArtData, stuffed);
+ }
+
+ private static byte[] StuffForWire(ReadOnlySpan destuffed)
+ {
+ // Destuffed input already has CRLF lines; double leading dots in body for NNTP transfer.
+ var headerEnd = destuffed.IndexOf("\r\n\r\n"u8);
+ Assert.True(headerEnd >= 0);
+ var bodyStart = headerEnd + 4;
+ var output = new List(destuffed.Length + 16);
+ output.AddRange(destuffed[..bodyStart].ToArray());
+ var body = destuffed[bodyStart..];
+ var offset = 0;
+ while (offset < body.Length)
+ {
+ var lineEnd = body[offset..].IndexOf("\r\n"u8);
+ ReadOnlySpan line;
+ int advance;
+ if (lineEnd < 0)
+ {
+ line = body[offset..];
+ advance = line.Length;
+ }
+ else
+ {
+ line = body.Slice(offset, lineEnd);
+ advance = lineEnd + 2;
+ }
+
+ if (line.Length > 0 && line[0] == (byte)'.')
+ {
+ output.Add((byte)'.');
+ }
+
+ output.AddRange(line.ToArray());
+ if (lineEnd >= 0)
+ {
+ output.Add((byte)'\r');
+ output.Add((byte)'\n');
+ }
+
+ offset += advance;
+ }
+
+ output.Add((byte)'.');
+ output.Add((byte)'\r');
+ output.Add((byte)'\n');
+ return output.ToArray();
+ }
+
+ private readonly record struct BuiltWire(
+ string MessageId,
+ ArticleId ArtId,
+ ReadOnlyMemory Canonical,
+ byte[] Stuffed);
+
+ private sealed class BlockingProcessor : IAcceptedArticleProcessor
+ {
+ private readonly Task _release;
+ public int Finished;
+
+ public BlockingProcessor(Task release) => _release = release;
+
+ public void EnsureReady()
+ {
+ }
+
+ public async ValueTask ProcessAsync(
+ AcceptedArticle article,
+ CancellationToken cancellationToken)
+ {
+ await _release.WaitAsync(cancellationToken).ConfigureAwait(false);
+ Finished++;
+ return AcceptedArticleProcessResult.Completed;
+ }
+ }
+
+ private sealed class Harness : IAsyncDisposable
+ {
+ private readonly CancellationTokenSource _cts = new();
+ private readonly Task _run;
+ private readonly PipeTransport _transport;
+
+ private Harness(PipeTransport transport, StorageNntpFeedSession session)
+ {
+ _transport = transport;
+ _run = session.RunAsync(_cts.Token);
+ }
+
+ public static async Task StartAsync(IAcceptedArticleQueue queue)
+ {
+ var transport = new PipeTransport();
+ var session = new StorageNntpFeedSession(
+ transport,
+ new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8),
+ NullLogger.Instance,
+ CacheFqdn,
+ acceptedArticles: queue,
+ assemblyAdmission: new StoreAssemblyAdmission(8));
+ var harness = new Harness(transport, session);
+ var greeting = await harness.ReadLineAsync().WaitAsync(Safety);
+ Assert.StartsWith("200 ", greeting, StringComparison.Ordinal);
+ return harness;
+ }
+
+ public Task WriteClientAsync(byte[] bytes) => _transport.ClientWriteAsync(bytes);
+
+ public async Task DisposeTransportAsync()
+ {
+ await _transport.DisposeAsync();
+ }
+
+ public async Task CommandAsync(string command)
+ {
+ await _transport.ClientWriteAsync(Encoding.ASCII.GetBytes(command));
+ return await ReadLineAsync();
+ }
+
+ public async Task TakeThisAsync(string messageId, byte[] stuffedArticleIncludingTerminator)
+ {
+ await _transport.ClientWriteAsync(Encoding.ASCII.GetBytes($"TAKETHIS {messageId}\r\n"));
+ await _transport.ClientWriteAsync(stuffedArticleIncludingTerminator);
+ return await ReadLineAsync();
+ }
+
+ public async Task ReadLineAsync()
+ {
+ using var timeout = new CancellationTokenSource(Safety);
+ var buffer = new List();
+ while (true)
+ {
+ var chunk = await _transport.ReadServerAsync(timeout.Token);
+ buffer.AddRange(chunk);
+ for (var i = 0; i < buffer.Count - 1; i++)
+ {
+ if (buffer[i] == (byte)'\r' && buffer[i + 1] == (byte)'\n')
+ {
+ var line = Encoding.ASCII.GetString(buffer.GetRange(0, i).ToArray());
+ buffer.RemoveRange(0, i + 2);
+ // Capabilities multiline: keep reading until terminator line if 101.
+ if (line.StartsWith("101 ", StringComparison.Ordinal))
+ {
+ while (true)
+ {
+ while (buffer.Count < 2)
+ {
+ buffer.AddRange(await _transport.ReadServerAsync(timeout.Token));
+ }
+
+ var end = -1;
+ for (var j = 0; j < buffer.Count - 1; j++)
+ {
+ if (buffer[j] == (byte)'\r' && buffer[j + 1] == (byte)'\n')
+ {
+ end = j;
+ break;
+ }
+ }
+
+ if (end < 0)
+ {
+ buffer.AddRange(await _transport.ReadServerAsync(timeout.Token));
+ continue;
+ }
+
+ var next = Encoding.ASCII.GetString(buffer.GetRange(0, end).ToArray());
+ buffer.RemoveRange(0, end + 2);
+ if (next == ".")
+ {
+ return line;
+ }
+ }
+ }
+
+ return line;
+ }
+ }
+ }
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ await _cts.CancelAsync();
+ try
+ {
+ await _run;
+ }
+ catch (OperationCanceledException ex)
+ {
+ ExpectedException.Ignore(ex);
+ }
+ catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex))
+ {
+ ExpectedException.Ignore(ex);
+ }
+
+ _cts.Dispose();
+ await _transport.DisposeAsync();
+ }
+ }
+
+ private sealed class PipeTransport : IStorageListenerTransport
+ {
+ private readonly Channel _toServer = Channel.CreateUnbounded();
+ private readonly Channel _toClient = Channel.CreateUnbounded();
+ private byte[] _pending = [];
+ private int _pendingOffset;
+
+ public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken)
+ {
+ if (_pendingOffset >= _pending.Length)
+ {
+ _pending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
+ _pendingOffset = 0;
+ }
+
+ var count = Math.Min(buffer.Length, _pending.Length - _pendingOffset);
+ _pending.AsSpan(_pendingOffset, count).CopyTo(buffer.Span);
+ _pendingOffset += count;
+ return count;
+ }
+
+ public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken)
+ {
+ await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false);
+ return buffer.Length;
+ }
+
+ public Task ClientWriteAsync(byte[] frame) => _toServer.Writer.WriteAsync(frame).AsTask();
+
+ public Task ReadServerAsync(CancellationToken cancellationToken) =>
+ _toClient.Reader.ReadAsync(cancellationToken).AsTask();
+
+ public ValueTask DisposeAsync()
+ {
+ _toServer.Writer.TryComplete();
+ _toClient.Writer.TryComplete();
+ return ValueTask.CompletedTask;
+ }
+ }
+ }
+}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpListenerServiceTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpListenerServiceTests.cs
similarity index 72%
rename from tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpListenerServiceTests.cs
rename to tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpListenerServiceTests.cs
index 3b807825..1b4b5b65 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpListenerServiceTests.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpListenerServiceTests.cs
@@ -8,8 +8,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using VectorNNTPD.Common.Acme;
-using VectorNNTPD.Common.Networking.Certificates;
-using VectorNNTPD.Common.Transport.ArticleTransfer;
using VectorNNTPD.StorageService.Configuration;
using VectorNNTPD.StorageService.Listener;
using VectorNNTPD.StorageService.Tests.Fixtures;
@@ -17,8 +15,7 @@
namespace VectorNNTPD.StorageService.Tests.Listener
{
-
- public sealed class StorageVatpListenerServiceTests
+ public sealed class StorageNntpListenerServiceTests
{
private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10);
@@ -27,16 +24,15 @@ public async Task Listener_refuses_start_without_acme_readiness()
{
var runtime = CreateRuntime(StorageServerTestOptions.GetFreeTcpPort());
var readiness = new AcmeCertificateReadiness();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
readiness,
- NullLogger.Instance);
+ NullLogger.Instance);
var ex = await Assert.ThrowsAsync(() => service.StartAsync(CancellationToken.None));
Assert.Contains("ACME certificate is not ready", ex.Message, StringComparison.Ordinal);
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
Assert.Empty(service.LocalEndPoints);
}
@@ -46,16 +42,15 @@ public async Task Listener_refuses_start_without_certificate()
var runtime = CreateRuntime(StorageServerTestOptions.GetFreeTcpPort());
var readiness = new AcmeCertificateReadiness();
readiness.MarkReady();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
TestListenerCertificates.CreateUnavailableProvider(),
- NullStorageArticleOpenBoundary.Instance,
readiness,
- NullLogger.Instance);
+ NullLogger.Instance);
var ex = await Assert.ThrowsAsync(() => service.StartAsync(CancellationToken.None));
Assert.Contains("no TLS certificate", ex.Message, StringComparison.Ordinal);
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
}
[Fact]
@@ -65,15 +60,14 @@ public async Task Listener_starts_when_ready_and_shuts_down_cleanly()
var runtime = CreateRuntime(port);
var readiness = new AcmeCertificateReadiness();
readiness.MarkReady();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
readiness,
- NullLogger.Instance);
+ NullLogger.Instance);
await service.StartAsync(CancellationToken.None);
- Assert.Equal(StorageVatpListenerState.Running, service.State);
+ Assert.Equal(StorageNntpListenerState.Running, service.State);
Assert.All(service.LocalEndPoints, endpoint =>
{
var ip = Assert.IsType(endpoint);
@@ -81,9 +75,9 @@ public async Task Listener_starts_when_ready_and_shuts_down_cleanly()
});
await service.StopAsync(CancellationToken.None);
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
await service.DisposeAsync();
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
}
[Fact]
@@ -98,18 +92,17 @@ public async Task Bind_failure_disposes_the_partial_socket_and_leaves_the_listen
var runtime = CreateRuntime(port);
var readiness = new AcmeCertificateReadiness();
readiness.MarkReady();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
readiness,
- NullLogger.Instance);
+ NullLogger.Instance);
var ex = await Assert.ThrowsAnyAsync(() => service.StartAsync(CancellationToken.None));
Assert.True(
ex is SocketException or InvalidOperationException,
"Expected bind failure or no-endpoints failure, got " + ex.GetType().Name);
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
Assert.Empty(service.LocalEndPoints);
}
@@ -121,12 +114,11 @@ public async Task Existing_connection_keeps_certificate_lease_after_rotation()
var certificates = TestListenerCertificates.CreatePublishedProvider("cache-original.test");
var readiness = new AcmeCertificateReadiness();
readiness.MarkReady();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
certificates,
- NullStorageArticleOpenBoundary.Instance,
readiness,
- NullLogger.Instance);
+ NullLogger.Instance);
await service.StartAsync(CancellationToken.None);
@@ -165,47 +157,6 @@ await ssl2.AuthenticateAsClientAsync(
await service.StopAsync(CancellationToken.None);
}
- [Fact]
- public async Task Listener_uses_the_production_initial_window_when_no_limits_are_supplied()
- {
- await using var service = new StorageVatpListenerService(
- CreateRuntime(StorageServerTestOptions.GetFreeTcpPort()),
- TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
- NullLogger.Instance);
- Assert.Equal(1024 * 1024, service.TransferLimits.InitialStreamWindowBytes);
- Assert.Equal(64L * 1024 * 1024, service.TransferLimits.MaxStreamCreditBytes);
- }
-
- [Fact]
- public async Task Listener_keeps_an_explicit_initial_window_for_its_sessions()
- {
- var limits = new ArticleTransferLimits { InitialStreamWindowBytes = 2 * 1024 * 1024 };
- await using var service = new StorageVatpListenerService(
- CreateRuntime(StorageServerTestOptions.GetFreeTcpPort()),
- TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
- NullLogger.Instance,
- transferLimits: limits);
- Assert.Same(limits, service.TransferLimits);
- Assert.Equal(2 * 1024 * 1024, service.TransferLimits.InitialStreamWindowBytes);
- Assert.Equal(ArticleTransferLimits.Default.MaxStreamCreditBytes, service.TransferLimits.MaxStreamCreditBytes);
- }
-
- [Fact]
- public void Bench_window_accepts_only_the_five_sizes()
- {
- Assert.Equal(1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow(null));
- Assert.Equal(256 * 1024, StorageVatpThroughputBench.ParseBenchWindow("262144"));
- Assert.Equal(512 * 1024, StorageVatpThroughputBench.ParseBenchWindow("524288"));
- Assert.Equal(1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow("1048576"));
- Assert.Equal(2 * 1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow("2097152"));
- Assert.Equal(4 * 1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow("4194304"));
- var rejected = Assert.Throws(() => StorageVatpThroughputBench.ParseBenchWindow("1048575"));
- Assert.Contains("262144", rejected.Message, StringComparison.Ordinal);
- Assert.Throws(() => StorageVatpThroughputBench.ParseBenchWindow("67108864"));
- }
-
[Fact]
public async Task Peer_disconnect_releases_admission_and_keeps_the_listener_accepting()
{
@@ -214,10 +165,9 @@ public async Task Peer_disconnect_releases_admission_and_keeps_the_listener_acce
var readiness = new AcmeCertificateReadiness();
readiness.MarkReady();
var logger = new CapturingListenerLogger();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
readiness,
logger);
@@ -259,7 +209,7 @@ await ssl2.AuthenticateAsClientAsync(
await WaitUntilAsync(() => service.ActiveConnections == 1);
await service.StopAsync(CancellationToken.None);
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
Assert.Equal(0, service.ActiveConnections);
}
@@ -271,10 +221,9 @@ public async Task Drain_logs_unexpected_faulted_connection_tasks_and_still_compl
var readiness = new AcmeCertificateReadiness();
readiness.MarkReady();
var logger = new CapturingListenerLogger();
- await using var service = new StorageVatpListenerService(
+ await using var service = new StorageNntpListenerService(
runtime,
TestListenerCertificates.CreatePublishedProvider(),
- NullStorageArticleOpenBoundary.Instance,
readiness,
logger);
@@ -288,14 +237,14 @@ public async Task Drain_logs_unexpected_faulted_connection_tasks_and_still_compl
await service.StopAsync(CancellationToken.None);
- Assert.Equal(StorageVatpListenerState.Stopped, service.State);
+ Assert.Equal(StorageNntpListenerState.Stopped, service.State);
Assert.Contains("unexpected task failure", logger.Text, StringComparison.Ordinal);
Assert.Contains("injected-drain-fault", logger.Text, StringComparison.Ordinal);
}
- private static ConcurrentDictionary GetConnectionMap(StorageVatpListenerService service)
+ private static ConcurrentDictionary GetConnectionMap(StorageNntpListenerService service)
{
- var field = typeof(StorageVatpListenerService).GetField(
+ var field = typeof(StorageNntpListenerService).GetField(
"_connections",
BindingFlags.Instance | BindingFlags.NonPublic);
Assert.NotNull(field);
@@ -329,7 +278,7 @@ private static StorageServerRuntimeOptions CreateRuntime(int tlsPort)
return StorageServerRuntimeOptionsFactory.Create(identity, acme);
}
- private sealed class CapturingListenerLogger : ILogger
+ private sealed class CapturingListenerLogger : ILogger
{
private readonly List _messages = [];
@@ -357,13 +306,13 @@ public void Log(
{
lock (_messages)
{
- var message = formatter(state, exception);
+ var line = formatter(state, exception);
if (exception is not null)
{
- message = $"{message} {exception}";
+ line = line + " " + exception;
}
- _messages.Add(message);
+ _messages.Add(line);
}
}
}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpLiveBackpressureTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpLiveBackpressureTests.cs
new file mode 100644
index 00000000..5b7842ae
--- /dev/null
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpLiveBackpressureTests.cs
@@ -0,0 +1,285 @@
+using System.Net;
+using System.Net.Security;
+using System.Net.Sockets;
+using System.Security.Authentication;
+using System.Text;
+using Microsoft.Extensions.Logging.Abstractions;
+using VectorNNTPD.Common.Acme;
+using VectorNNTPD.Common.Articles;
+using VectorNNTPD.Common.Articles.Parsing;
+using VectorNNTPD.StorageService;
+using VectorNNTPD.StorageService.Configuration;
+using VectorNNTPD.StorageService.Ingress;
+using VectorNNTPD.StorageService.Listener;
+using VectorNNTPD.StorageService.Tests.Fixtures;
+using VectorNNTPD.StorageService.Tests.TestDoubles;
+
+namespace VectorNNTPD.StorageService.Tests.Listener
+{
+ ///
+ /// Live TLS proof that TAKETHIS enqueue completes before persistence finishes.
+ /// Full-queue backpressure is covered in-process by
+ /// .
+ ///
+ [Collection("StorageNntpLive")]
+ public sealed class StorageNntpLiveBackpressureTests
+ {
+ private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10);
+
+ [Fact]
+ public async Task Available_capacity_accepts_before_worker_persistence()
+ {
+ var article = Article("avail-0001");
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var processor = new GatePublisher(release.Task);
+ await using var host = await LiveHost.StartAsync(article.Record.ArtSize * 4L, processor, startWorker: true);
+
+ await using var client = await RawNntpClient.ConnectAsync(host.Port);
+ Assert.Equal(239, await client.TakeThisAsync(article));
+ await WaitUntilAsync(() => processor.Entered >= 1);
+ Assert.Equal(0, processor.Finished);
+ release.TrySetResult();
+ await WaitUntilAsync(() => processor.Finished >= 1 && host.Queue.AccountedBytes == 0);
+ }
+
+ private static BuiltArticle Article(string token)
+ {
+ var messageId = $"<{token}@backpressure.test>";
+ var text =
+ "Path: peer.example\r\n" +
+ "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" +
+ $"Message-ID: {messageId}\r\n" +
+ "Newsgroups: alt.test\r\n" +
+ "From: user@example.test\r\n" +
+ "Subject: backpressure\r\n" +
+ "\r\n" +
+ "body-line-for-backpressure\r\n";
+ var logical = Encoding.ASCII.GetBytes(text);
+ var created = ArticleRecordFactory.TryCreate(
+ new NntpArticleParser("nntpd01.usenet.ninja"),
+ logical,
+ ArticlePathMode.Normalize);
+ Assert.True(created.IsAccepted, created.MaterializeFailure.ToString());
+ return new BuiltArticle(messageId, created.Record, logical);
+ }
+
+ private static async Task WaitUntilAsync(Func condition)
+ {
+ using var timeout = new CancellationTokenSource(Safety);
+ while (!condition())
+ {
+ await Task.Delay(5, timeout.Token);
+ }
+ }
+
+ private readonly record struct BuiltArticle(string MessageId, ArticleRecord Record, byte[] Logical);
+
+ private sealed class GatePublisher : IAcceptedArticleProcessor
+ {
+ private readonly Task _release;
+
+ public GatePublisher(Task release) => _release = release;
+
+ public int Entered { get; private set; }
+
+ public int Finished { get; private set; }
+
+ public void EnsureReady()
+ {
+ }
+
+ public async ValueTask ProcessAsync(
+ AcceptedArticle article,
+ CancellationToken cancellationToken)
+ {
+ Entered++;
+ await _release.WaitAsync(cancellationToken).ConfigureAwait(false);
+ Finished++;
+ return AcceptedArticleProcessResult.Completed;
+ }
+ }
+
+ private sealed class RawNntpClient : IAsyncDisposable
+ {
+ private readonly TcpClient _tcp;
+ private readonly SslStream _ssl;
+ private readonly List _pending = [];
+ private readonly byte[] _readBuffer = new byte[16 * 1024];
+
+ private RawNntpClient(TcpClient tcp, SslStream ssl)
+ {
+ _tcp = tcp;
+ _ssl = ssl;
+ }
+
+ public static async Task ConnectAsync(int port)
+ {
+ var tcp = new TcpClient { NoDelay = true };
+ await tcp.ConnectAsync(IPAddress.Loopback, port);
+ var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false);
+ await ssl.AuthenticateAsClientAsync(
+ new SslClientAuthenticationOptions
+ {
+ TargetHost = "localhost",
+ EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13,
+ RemoteCertificateValidationCallback = static (_, _, _, _) => true,
+ },
+ CancellationToken.None);
+ var client = new RawNntpClient(tcp, ssl);
+ Assert.Equal(200, await client.ReadStatusAsync());
+ await ssl.WriteAsync("MODE STREAM\r\n"u8.ToArray());
+ Assert.Equal(203, await client.ReadStatusAsync());
+ return client;
+ }
+
+ public async Task TakeThisAsync(BuiltArticle article)
+ {
+ await _ssl.WriteAsync(Encoding.ASCII.GetBytes("TAKETHIS " + article.MessageId + "\r\n"));
+ await _ssl.WriteAsync(DotStuff(article.Logical));
+ await _ssl.WriteAsync(".\r\n"u8.ToArray());
+ return await ReadStatusAsync();
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ try
+ {
+ await _ssl.DisposeAsync();
+ }
+ catch (Exception ex) when (ex is IOException or ObjectDisposedException)
+ {
+ ExpectedException.Ignore(ex);
+ }
+
+ _tcp.Dispose();
+ }
+
+ private async Task ReadStatusAsync()
+ {
+ using var timeout = new CancellationTokenSource(Safety);
+ while (true)
+ {
+ for (var i = 0; i < _pending.Count - 1; i++)
+ {
+ if (_pending[i] == (byte)'\r' && _pending[i + 1] == (byte)'\n')
+ {
+ var line = Encoding.ASCII.GetString(_pending.GetRange(0, i).ToArray());
+ _pending.RemoveRange(0, i + 2);
+ return ((line[0] - '0') * 100) + ((line[1] - '0') * 10) + (line[2] - '0');
+ }
+ }
+
+ var read = await _ssl.ReadAsync(_readBuffer, timeout.Token);
+ if (read == 0)
+ {
+ throw new EndOfStreamException();
+ }
+
+ for (var i = 0; i < read; i++)
+ {
+ _pending.Add(_readBuffer[i]);
+ }
+ }
+ }
+
+ private static byte[] DotStuff(ReadOnlySpan article)
+ {
+ using var ms = new MemoryStream(article.Length + 8);
+ var atLineStart = true;
+ for (var i = 0; i < article.Length; i++)
+ {
+ var b = article[i];
+ if (atLineStart && b == (byte)'.')
+ {
+ ms.WriteByte((byte)'.');
+ }
+
+ ms.WriteByte(b);
+ atLineStart = b == (byte)'\n';
+ }
+
+ return ms.ToArray();
+ }
+ }
+
+ private sealed class LiveHost : IAsyncDisposable
+ {
+ private readonly StorageNntpListenerService _listener;
+ private readonly AcceptedArticleWorker? _worker;
+
+ private LiveHost(
+ AcceptedArticleQueue queue,
+ StorageNntpListenerService listener,
+ AcceptedArticleWorker? worker,
+ int port)
+ {
+ Queue = queue;
+ _listener = listener;
+ _worker = worker;
+ Port = port;
+ }
+
+ public int Port { get; }
+
+ public AcceptedArticleQueue Queue { get; }
+
+ public static async Task StartAsync(
+ long maxQueuedBytes,
+ IAcceptedArticleProcessor? processor = null,
+ bool startWorker = false)
+ {
+ var port = StorageServerTestOptions.GetFreeTcpPort();
+ var identity = StorageServerTestOptions.CreateValid();
+ identity.BindAddress = ["127.0.0.1"];
+ identity.BindPortTls = port;
+ var acme = StorageServerTestOptions.CreateValidAcme(identity);
+ acme.BindAddress = ["127.0.0.1"];
+ acme.BindPortTls = port;
+ var runtime = StorageServerRuntimeOptionsFactory.Create(identity, acme);
+ var queue = new AcceptedArticleQueue(maxQueuedBytes, TimeProvider.System);
+ var readiness = new AcmeCertificateReadiness();
+ readiness.MarkReady();
+ var listener = new StorageNntpListenerService(
+ runtime,
+ TestListenerCertificates.CreatePublishedProvider(),
+ readiness,
+ NullLogger.Instance,
+ acceptedArticles: queue,
+ assemblyAdmission: new StoreAssemblyAdmission(8));
+ await listener.StartAsync(CancellationToken.None);
+
+ AcceptedArticleWorker? worker = null;
+ if (startWorker)
+ {
+ ArgumentNullException.ThrowIfNull(processor);
+ worker = new AcceptedArticleWorker(
+ queue,
+ processor,
+ NullLogger.Instance,
+ new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 },
+ new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(),
+ new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(),
+ TimeProvider.System);
+ await worker.StartAsync(CancellationToken.None);
+ }
+
+ return new LiveHost(queue, listener, worker, port);
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ if (_worker is not null)
+ {
+ await _worker.StopAsync(CancellationToken.None);
+ }
+
+ await _listener.DisposeAsync();
+ }
+ }
+ }
+
+ [CollectionDefinition("StorageNntpLive", DisableParallelization = true)]
+ public sealed class StorageNntpLiveCollection
+ {
+ }
+}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpQueueIngressTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpQueueIngressTests.cs
new file mode 100644
index 00000000..c3129338
--- /dev/null
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpQueueIngressTests.cs
@@ -0,0 +1,530 @@
+using System.Text;
+using System.Threading.Channels;
+using Microsoft.Extensions.Logging.Abstractions;
+using VectorNNTPD.Common.Articles;
+using VectorNNTPD.Common.Articles.Parsing;
+using VectorNNTPD.StorageService.Configuration;
+using VectorNNTPD.StorageService.Ingress;
+using VectorNNTPD.StorageService.Listener;
+using VectorNNTPD.StorageService;
+
+namespace VectorNNTPD.StorageService.Tests.Listener
+{
+ ///
+ /// In-process NNTP TAKETHIS ingress against over a pipe transport.
+ ///
+ public sealed class StorageNntpQueueIngressTests
+ {
+ private const string CacheFqdn = "cache01.usenet.ninja";
+ private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10);
+
+ [Fact]
+ public async Task TakeThis_enqueues_the_original_article_without_this_servers_path_hop()
+ {
+ var inbound = Build("", "body-line\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(239, await harness.TakeThisAsync(inbound));
+
+ var delivery = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(delivery.HasArticle);
+ Assert.Equal(inbound.Record.ArtId, delivery.Article.ArtId);
+ Assert.Equal(inbound.Record.ArtHash, delivery.Article.ArtHash);
+ Assert.Equal(inbound.Record.ArtSize, delivery.Article.ArtSize);
+ Assert.True(delivery.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
+ var queued = Encoding.ASCII.GetString(delivery.Article.ArtData.Span);
+ Assert.Equal(0, CountHop(queued, CacheFqdn));
+ Assert.Contains("Path: ", queued, StringComparison.Ordinal);
+ Assert.Contains("body-line\r\n", queued, StringComparison.Ordinal);
+ Assert.Contains("news.usenet.ninja", queued, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public async Task Second_takeThis_of_the_same_bytes_enqueues_another_unmodified_copy()
+ {
+ var inbound = Build("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(239, await harness.TakeThisAsync(inbound));
+ Assert.Equal(239, await harness.TakeThisAsync(inbound));
+
+ var first = await queue.DequeueAsync(CancellationToken.None);
+ var second = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(first.HasArticle && second.HasArticle);
+ Assert.True(first.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
+ Assert.True(second.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
+ }
+
+ [Fact]
+ public async Task Status_239_is_sent_before_persistence_finishes()
+ {
+ var inbound = Build("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
+ var processor = new BlockingProcessor(release.Task);
+ var worker = new AcceptedArticleWorker(
+ queue,
+ processor,
+ NullLogger.Instance,
+ new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 },
+ new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(),
+ new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(),
+ TimeProvider.System);
+ await worker.StartAsync(CancellationToken.None);
+ try
+ {
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(239, await harness.TakeThisAsync(inbound).WaitAsync(TimeSpan.FromSeconds(2)));
+ Assert.Equal(0, processor.Finished);
+ }
+ finally
+ {
+ release.TrySetResult();
+ await worker.StopAsync(CancellationToken.None);
+ }
+ }
+
+ [Fact]
+ public async Task Message_id_mismatch_is_rejected_without_enqueue()
+ {
+ var real = Build("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(439, await harness.TakeThisRawAsync("", real.Record.ArtData));
+ Assert.Equal(0, queue.EnqueuedCount);
+ Assert.Equal(0, queue.ReservedBytes);
+ Assert.Equal(0, queue.AccountedBytes);
+ }
+
+ [Fact]
+ public async Task Malformed_article_is_rejected_without_enqueue()
+ {
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ var junk = Encoding.ASCII.GetBytes("not-an-article\r\n");
+ Assert.Equal(439, await harness.TakeThisRawAsync("", junk));
+ Assert.Equal(0, queue.EnqueuedCount);
+ }
+
+ [Fact]
+ public async Task Completed_queue_returns_400_and_closes()
+ {
+ var built = Build("", "body\r\n");
+ var queue = new AcceptedArticleQueue();
+ queue.Complete();
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(400, await harness.TakeThisAsync(built));
+ Assert.Equal(0, queue.ReservedBytes);
+ var empty = await queue.DequeueAsync(CancellationToken.None);
+ Assert.False(empty.HasArticle);
+ }
+
+ [Fact]
+ public async Task Two_sessions_enqueue_distinct_articles()
+ {
+ var queue = new AcceptedArticleQueue();
+ var first = Build("", "alpha\r\n");
+ var second = Build("", "beta\r\n");
+ await using var left = await Harness.StartAsync(queue);
+ await using var right = await Harness.StartAsync(queue);
+ var leftTask = left.TakeThisAsync(first);
+ var rightTask = right.TakeThisAsync(second);
+ Assert.Equal(239, await leftTask);
+ Assert.Equal(239, await rightTask);
+
+ var seen = new HashSet(StringComparer.Ordinal);
+ for (var i = 0; i < 2; i++)
+ {
+ var delivery = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(delivery.HasArticle);
+ seen.Add(Encoding.ASCII.GetString(delivery.Article.ArtData.Span));
+ }
+
+ Assert.Contains(seen, static text => text.Contains("alpha\r\n", StringComparison.Ordinal));
+ Assert.Contains(seen, static text => text.Contains("beta\r\n", StringComparison.Ordinal));
+ }
+
+ [Fact]
+ public async Task Full_queue_blocks_on_reserve_until_dequeue_then_accepts()
+ {
+ var first = Build("", "alpha-body\r\n");
+ var queue = new AcceptedArticleQueue(first.Record.ArtSize, TimeProvider.System);
+ await using var held = await Harness.StartAsync(queue);
+ Assert.Equal(239, await held.TakeThisAsync(first));
+
+ var second = Build("", "beta-body\r\n");
+ await using var waiting = await Harness.StartAsync(queue);
+ var pending = waiting.TakeThisAsync(second);
+ await WaitUntilAsync(() => queue.AdmissionWaiters == 1);
+ Assert.Equal(0, queue.ReservedBytes);
+ Assert.Equal(first.Record.ArtSize, queue.AccountedBytes);
+ Assert.False(pending.IsCompleted);
+
+ Assert.True((await queue.DequeueAsync(CancellationToken.None)).HasArticle);
+ Assert.Equal(239, await pending.WaitAsync(TimeSpan.FromSeconds(2)));
+ Assert.Equal(0, queue.ReservedBytes);
+ Assert.Equal(second.Record.ArtSize, queue.QueuedBytes);
+ var delivery = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(delivery.Article.ArtData.Span.SequenceEqual(second.Record.ArtData.Span));
+ }
+
+ [Fact]
+ public async Task Article_larger_than_queue_budget_is_rejected()
+ {
+ var built = Build("", "too-big-for-budget\r\n");
+ var queue = new AcceptedArticleQueue(8, TimeProvider.System);
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(439, await harness.TakeThisAsync(built));
+ Assert.Equal(0, queue.EnqueuedCount);
+ Assert.Equal(0, queue.ReservedBytes);
+ }
+
+ [Fact]
+ public async Task Disconnect_mid_transfer_does_not_enqueue()
+ {
+ var built = Build("", new string('x', 128) + "\r\n");
+ var queue = new AcceptedArticleQueue(built.Record.ArtSize, TimeProvider.System);
+ var session = await Harness.StartAsync(queue);
+ await session.SendRawAsync(Encoding.ASCII.GetBytes("TAKETHIS \r\n"));
+ await session.SendRawAsync(Encoding.ASCII.GetBytes("Path: peer.example\r\n"));
+ await session.DisposeAsync();
+
+ Assert.Equal(0, queue.EnqueuedCount);
+ Assert.Equal(0, queue.ReservedBytes);
+ Assert.Equal(0, queue.AccountedBytes);
+ Assert.Equal(0, queue.AdmissionWaiters);
+
+ await using var retry = await Harness.StartAsync(queue);
+ Assert.Equal(239, await retry.TakeThisAsync(built));
+ Assert.Equal(built.Record.ArtSize, queue.QueuedBytes);
+ }
+
+ [Fact]
+ public async Task Dot_stuffed_article_enqueues_with_matching_art_id()
+ {
+ var inbound = Build("", "..leading\r\n.middle\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ Assert.Equal(239, await harness.TakeThisAsync(inbound));
+ var delivery = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(delivery.HasArticle);
+ Assert.Equal(inbound.Record.ArtId, delivery.Article.ArtId);
+ Assert.Contains("..leading\r\n", Encoding.ASCII.GetString(delivery.Article.ArtData.Span), StringComparison.Ordinal);
+ Assert.Contains(".middle\r\n", Encoding.ASCII.GetString(delivery.Article.ArtData.Span), StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public async Task Pipelined_second_takeThis_with_large_residue_still_enqueues()
+ {
+ // After the first terminator, leftover must hold the next command plus article bytes
+ // even when that residue exceeds the 512-octet command-line limit.
+ var first = Build("", "first-body\r\n");
+ var second = Build("", new string('y', 700) + "\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+
+ var firstId = Encoding.ASCII.GetString(first.Record.MessageId.ToArray());
+ var secondId = Encoding.ASCII.GetString(second.Record.MessageId.ToArray());
+ var secondCommand = Encoding.ASCII.GetBytes("TAKETHIS " + secondId + "\r\n");
+ var secondBody = DotStuff(second.Logical);
+ Assert.True(
+ secondCommand.Length + secondBody.Length + 3 > 512,
+ "Residue after the first terminator must exceed the 512-octet command buffer.");
+
+ // Complete the first article's body, then deliver terminator + entire second TAKETHIS in one
+ // chunk so the payload reader must PreserveLeftover a residue larger than 512 octets.
+ await harness.SendRawAsync(Encoding.ASCII.GetBytes("TAKETHIS " + firstId + "\r\n"));
+ await harness.SendRawAsync(DotStuff(first.Logical));
+ var afterFirst = new byte[3 + secondCommand.Length + secondBody.Length + 3];
+ ".\r\n"u8.CopyTo(afterFirst);
+ secondCommand.CopyTo(afterFirst.AsSpan(3));
+ secondBody.CopyTo(afterFirst.AsSpan(3 + secondCommand.Length));
+ ".\r\n"u8.CopyTo(afterFirst.AsSpan(3 + secondCommand.Length + secondBody.Length));
+ await harness.SendRawAsync(afterFirst);
+
+ var firstStatus = await harness.ReadStatusAsync();
+ var secondStatus = await harness.ReadStatusAsync();
+ Assert.Equal((239, 239), (firstStatus, secondStatus));
+
+ var a = await queue.DequeueAsync(CancellationToken.None);
+ var b = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(a.HasArticle && b.HasArticle);
+ var ids = new HashSet { a.Article.ArtId, b.Article.ArtId };
+ Assert.Contains(first.Record.ArtId, ids);
+ Assert.Contains(second.Record.ArtId, ids);
+ }
+
+ [Fact]
+ public async Task Split_article_payload_writes_still_enqueue()
+ {
+ var built = Build("", "chunked-body\r\n");
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await Harness.StartAsync(queue);
+ var messageId = Encoding.ASCII.GetString(built.Record.MessageId.ToArray());
+ await harness.SendRawAsync(Encoding.ASCII.GetBytes("TAKETHIS " + messageId + "\r\n"));
+ var stuffed = DotStuff(built.Logical);
+ await harness.Transport.ClientWriteChunkedAsync(stuffed, 5);
+ await harness.SendRawAsync(".\r\n"u8.ToArray());
+ Assert.Equal(239, await harness.ReadStatusAsync());
+ var delivery = await queue.DequeueAsync(CancellationToken.None);
+ Assert.True(delivery.HasArticle);
+ Assert.Equal(built.Record.ArtId, delivery.Article.ArtId);
+ Assert.Contains("chunked-body\r\n", Encoding.ASCII.GetString(delivery.Article.ArtData.Span), StringComparison.Ordinal);
+ }
+
+ private static async Task WaitUntilAsync(Func condition)
+ {
+ using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(2));
+ while (!condition())
+ {
+ await Task.Delay(5, timeout.Token);
+ }
+ }
+
+ private static int CountHop(string article, string hop) =>
+ article.Split(hop, StringSplitOptions.None).Length - 1;
+
+ private static string Body(string article)
+ {
+ var split = article.IndexOf("\r\n\r\n", StringComparison.Ordinal);
+ Assert.True(split >= 0);
+ return article[(split + 4)..];
+ }
+
+ private static byte[] EncodeTakeThis(Built built)
+ {
+ var messageId = built.Record.MessageId.ToArray();
+ var command = Encoding.ASCII.GetBytes("TAKETHIS ");
+ var crlf = "\r\n"u8.ToArray();
+ var stuffed = DotStuff(built.Logical);
+ var terminator = ".\r\n"u8.ToArray();
+ var framed = new byte[command.Length + messageId.Length + crlf.Length + stuffed.Length + terminator.Length];
+ var offset = 0;
+ command.CopyTo(framed.AsSpan(offset));
+ offset += command.Length;
+ messageId.CopyTo(framed.AsSpan(offset));
+ offset += messageId.Length;
+ crlf.CopyTo(framed.AsSpan(offset));
+ offset += crlf.Length;
+ stuffed.CopyTo(framed.AsSpan(offset));
+ offset += stuffed.Length;
+ terminator.CopyTo(framed.AsSpan(offset));
+ return framed;
+ }
+
+ private static byte[] DotStuff(ReadOnlySpan article)
+ {
+ using var ms = new MemoryStream(article.Length + 16);
+ var atLineStart = true;
+ for (var i = 0; i < article.Length; i++)
+ {
+ var b = article[i];
+ if (atLineStart && b == (byte)'.')
+ {
+ ms.WriteByte((byte)'.');
+ }
+
+ ms.WriteByte(b);
+ atLineStart = b == (byte)'\n';
+ }
+
+ return ms.ToArray();
+ }
+
+ private static Built Build(string messageId, string body, string ingressFqdn = "nntpd01.usenet.ninja")
+ {
+ var parser = new NntpArticleParser(CacheFqdn);
+ var path = string.Equals(ingressFqdn, CacheFqdn, StringComparison.Ordinal)
+ ? CacheFqdn + "!peer.example"
+ : "peer.example";
+ var text = new StringBuilder()
+ .Append("Path: ").Append(path).Append("\r\n")
+ .Append("Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n")
+ .Append("Message-ID: ").Append(messageId).Append("\r\n")
+ .Append("Newsgroups: alt.test\r\n")
+ .Append("From: user@example.test\r\n")
+ .Append("Subject: nntp\r\n")
+ .Append("\r\n")
+ .Append(body);
+ var logical = Encoding.ASCII.GetBytes(text.ToString());
+ var created = ArticleRecordFactory.TryCreate(
+ parser,
+ logical,
+ ArticlePathMode.Normalize);
+ Assert.True(created.IsAccepted, created.MaterializeFailure.ToString());
+ return new Built(created.Record, logical);
+ }
+
+ private readonly record struct Built(ArticleRecord Record, byte[] Logical);
+
+ private sealed class BlockingProcessor : IAcceptedArticleProcessor
+ {
+ private readonly Task _release;
+
+ public BlockingProcessor(Task release) => _release = release;
+
+ public int Finished { get; private set; }
+
+ public void EnsureReady()
+ {
+ }
+
+ public async ValueTask ProcessAsync(
+ AcceptedArticle article,
+ CancellationToken cancellationToken)
+ {
+ await _release.WaitAsync(cancellationToken).ConfigureAwait(false);
+ Finished++;
+ return AcceptedArticleProcessResult.Completed;
+ }
+ }
+
+ private sealed class Harness : IAsyncDisposable
+ {
+ private readonly CancellationTokenSource _cts = new();
+ private readonly StorageNntpFeedSession _session;
+ private readonly Task _run;
+ private readonly List _pendingResponse = [];
+
+ private Harness(PipeTransport transport, StorageNntpFeedSession session)
+ {
+ Transport = transport;
+ _session = session;
+ _run = session.RunAsync(_cts.Token);
+ }
+
+ public PipeTransport Transport { get; }
+
+ public static async Task StartAsync(IAcceptedArticleQueue queue)
+ {
+ var transport = new PipeTransport();
+ var session = new StorageNntpFeedSession(
+ transport,
+ new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8),
+ NullLogger.Instance,
+ CacheFqdn,
+ acceptedArticles: queue,
+ assemblyAdmission: new StoreAssemblyAdmission(8));
+ var harness = new Harness(transport, session);
+ var greeting = await harness.ReadStatusAsync();
+ Assert.Equal(200, greeting);
+ await harness.SendRawAsync("MODE STREAM\r\n"u8.ToArray());
+ Assert.Equal(203, await harness.ReadStatusAsync());
+ return harness;
+ }
+
+ public async Task TakeThisAsync(Built built)
+ {
+ var messageId = built.Record.MessageId.ToArray();
+ return await TakeThisRawAsync(Encoding.ASCII.GetString(messageId), built.Logical);
+ }
+
+ public async Task TakeThisRawAsync(string messageId, ReadOnlyMemory logicalArticle)
+ {
+ var command = Encoding.ASCII.GetBytes("TAKETHIS " + messageId + "\r\n");
+ await SendRawAsync(command);
+ await SendRawAsync(DotStuff(logicalArticle.Span));
+ await SendRawAsync(".\r\n"u8.ToArray());
+ return await ReadStatusAsync();
+ }
+
+ public Task SendRawAsync(byte[] frame) => Transport.ClientWriteAsync(frame);
+
+ public async Task ReadStatusAsync()
+ {
+ using var timeout = new CancellationTokenSource(Safety);
+ while (true)
+ {
+ for (var i = 0; i < _pendingResponse.Count - 1; i++)
+ {
+ if (_pendingResponse[i] == (byte)'\r' && _pendingResponse[i + 1] == (byte)'\n')
+ {
+ var line = Encoding.ASCII.GetString(_pendingResponse.GetRange(0, i).ToArray());
+ _pendingResponse.RemoveRange(0, i + 2);
+ Assert.True(line.Length >= 3);
+ return ((line[0] - '0') * 100) + ((line[1] - '0') * 10) + (line[2] - '0');
+ }
+ }
+
+ var chunk = await Transport.ReadServerAsync(timeout.Token);
+ _pendingResponse.AddRange(chunk);
+ }
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ await _cts.CancelAsync();
+ try
+ {
+ await _run;
+ }
+ catch (OperationCanceledException ex)
+ {
+ ExpectedException.Ignore(ex);
+ }
+ catch (EndOfStreamException ex)
+ {
+ ExpectedException.Ignore(ex);
+ }
+
+ _cts.Dispose();
+ await Transport.DisposeAsync();
+ await _session.DisposeAsync();
+ }
+ }
+
+ private sealed class PipeTransport : IStorageListenerTransport
+ {
+ private readonly Channel _toServer = Channel.CreateUnbounded();
+ private readonly Channel _toClient = Channel.CreateUnbounded();
+ private byte[] _pending = [];
+ private int _pendingOffset;
+
+ public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken)
+ {
+ if (_pendingOffset >= _pending.Length)
+ {
+ _pending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
+ _pendingOffset = 0;
+ }
+
+ var count = Math.Min(buffer.Length, _pending.Length - _pendingOffset);
+ _pending.AsSpan(_pendingOffset, count).CopyTo(buffer.Span);
+ _pendingOffset += count;
+ return count;
+ }
+
+ public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken)
+ {
+ await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false);
+ return buffer.Length;
+ }
+
+ public Task ClientWriteAsync(byte[] frame) => _toServer.Writer.WriteAsync(frame).AsTask();
+
+ public async Task ClientWriteChunkedAsync(byte[] frame, int chunkSize)
+ {
+ ArgumentOutOfRangeException.ThrowIfNegativeOrZero(chunkSize);
+ var offset = 0;
+ while (offset < frame.Length)
+ {
+ var count = Math.Min(chunkSize, frame.Length - offset);
+ var slice = new byte[count];
+ frame.AsSpan(offset, count).CopyTo(slice);
+ await _toServer.Writer.WriteAsync(slice).ConfigureAwait(false);
+ offset += count;
+ }
+ }
+
+ public Task ReadServerAsync(CancellationToken cancellationToken) =>
+ _toClient.Reader.ReadAsync(cancellationToken).AsTask();
+
+ public ValueTask DisposeAsync()
+ {
+ _toServer.Writer.TryComplete();
+ _toClient.Writer.TryComplete();
+ return ValueTask.CompletedTask;
+ }
+ }
+ }
+}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpThroughputBench.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpThroughputBench.cs
new file mode 100644
index 00000000..8e4e3d24
--- /dev/null
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpThroughputBench.cs
@@ -0,0 +1,364 @@
+using System.Diagnostics;
+using System.Text;
+using System.Threading.Channels;
+using Microsoft.Extensions.Logging.Abstractions;
+using VectorNNTPD.Common.Acme;
+using VectorNNTPD.Common.Articles;
+using VectorNNTPD.Common.Articles.Parsing;
+using VectorNNTPD.StorageService;
+using VectorNNTPD.StorageService.Configuration;
+using VectorNNTPD.StorageService.Ingress;
+using VectorNNTPD.StorageService.Listener;
+using VectorNNTPD.StorageService.Tests.Fixtures;
+using VectorNNTPD.StorageService.Tests.Support.Placement;
+using VectorNNTPD.StorageService.Tests.TestDoubles;
+using Xunit.Abstractions;
+
+namespace VectorNNTPD.StorageService.Tests.Listener
+{
+ ///
+ /// Minimal NNTP TAKETHIS throughput harness. Sustained measurement runs only when
+ /// VECTORNNTPD_STORAGE_NNTP_BENCH=1.
+ ///
+ public sealed class StorageNntpThroughputBench
+ {
+ private readonly ITestOutputHelper _output;
+
+ public StorageNntpThroughputBench(ITestOutputHelper output) => _output = output;
+
+ public static int BodyBytes => 256;
+
+ [Fact]
+ public async Task Warm_path_places_one_article()
+ {
+ await using var host = await StartPlacementIngressHostAsync();
+ await using var client = new ArticlePlacementClient(NullLogger.Instance, 1)
+ {
+ TestTcpConnectHost = "127.0.0.1",
+ ServerCertificateValidationCallback = static (_, _, _, _) => true,
+ };
+ var record = CreateRecord();
+ var result = await client.PlaceAsync(record, host.Target, CancellationToken.None);
+ Assert.Equal(ArticlePlacementKind.Accepted, result.Kind);
+ Assert.Equal(1, host.Queue.EnqueuedCount);
+ }
+
+ [Fact]
+ public async Task Optional_sustained_sweep()
+ {
+ if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_NNTP_BENCH"), "1", StringComparison.Ordinal))
+ {
+ _output.WriteLine("Set VECTORNNTPD_STORAGE_NNTP_BENCH=1 to measure TAKETHIS throughput.");
+ return;
+ }
+
+ await using var host = await StartPlacementIngressHostAsync();
+ await using var client = new ArticlePlacementClient(NullLogger.Instance, 2)
+ {
+ TestTcpConnectHost = "127.0.0.1",
+ ServerCertificateValidationCallback = static (_, _, _, _) => true,
+ };
+ var record = CreateRecord();
+ var accepted = 0;
+ var clock = Stopwatch.StartNew();
+ while (clock.Elapsed < TimeSpan.FromSeconds(3))
+ {
+ var result = await client.PlaceAsync(record, host.Target, CancellationToken.None);
+ if (result.Kind == ArticlePlacementKind.Accepted)
+ {
+ accepted++;
+ _ = await host.Queue.DequeueAsync(CancellationToken.None);
+ }
+ }
+
+ _output.WriteLine($"accepted={accepted} elapsedMs={clock.ElapsedMilliseconds}");
+ Assert.True(accepted > 0);
+ }
+
+ ///
+ /// In-memory (pipe) TAKETHIS allocation sample. Set VECTORNNTPD_STORAGE_NNTP_BENCH=1.
+ /// Isolates session parse/assembly/materialize/queue from TLS.
+ ///
+ [Fact]
+ public async Task Optional_in_memory_allocation_sample()
+ {
+ if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_NNTP_BENCH"), "1", StringComparison.Ordinal))
+ {
+ _output.WriteLine("Set VECTORNNTPD_STORAGE_NNTP_BENCH=1 to measure in-memory TAKETHIS allocations.");
+ return;
+ }
+
+ const int warmup = 64;
+ const int measured = 512;
+ var queue = new AcceptedArticleQueue();
+ await using var harness = await InMemoryHarness.StartAsync(queue);
+ var wire = BuildWireArticle();
+
+ for (var i = 0; i < warmup; i++)
+ {
+ Assert.Equal(239, await harness.TakeThisAsync(wire));
+ _ = await queue.DequeueAsync(CancellationToken.None);
+ }
+
+ // Snapshot counters after warm-up without forcing GC. Deltas may include ambient
+ // runtime activity; compare relative runs with the same harness only.
+ var gen0 = GC.CollectionCount(0);
+ var gen1 = GC.CollectionCount(1);
+ var gen2 = GC.CollectionCount(2);
+ var allocatedBefore = GC.GetTotalAllocatedBytes(precise: true);
+ var clock = Stopwatch.StartNew();
+ for (var i = 0; i < measured; i++)
+ {
+ Assert.Equal(239, await harness.TakeThisAsync(wire));
+ _ = await queue.DequeueAsync(CancellationToken.None);
+ }
+
+ clock.Stop();
+ var allocated = GC.GetTotalAllocatedBytes(precise: true) - allocatedBefore;
+ // Includes harness status-line reads (PipeDuplex copies) as well as the session path.
+ // Compare before/after only with an identical harness; do not treat the absolute
+ // per-article figure as server-only cost or as a pristine GC baseline.
+ _output.WriteLine(
+ $"in-memory articles={measured} bodyBytes={BodyBytes} elapsedMs={clock.ElapsedMilliseconds} "
+ + $"allocatedBytes={allocated} allocatedPerArticle={allocated / (double)measured:0} "
+ + $"gen0={GC.CollectionCount(0) - gen0} gen1={GC.CollectionCount(1) - gen1} gen2={GC.CollectionCount(2) - gen2} "
+ + $"articlesPerSec={measured / clock.Elapsed.TotalSeconds:0} "
+ + $"note=includes-harness-status-reads;no-forced-gc");
+ }
+
+ private static byte[] BuildWireArticle()
+ {
+ var messageId = "";
+ var text =
+ "Path: peer.example\r\n" +
+ "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" +
+ $"Message-ID: {messageId}\r\n" +
+ "Newsgroups: alt.test\r\n" +
+ "From: user@example.test\r\n" +
+ "Subject: bench\r\n" +
+ "\r\n" +
+ new string('x', BodyBytes) + "\r\n";
+ // Transfer form: body lines that start with '.' are stuffed; terminator appended.
+ var bodyStart = text.IndexOf("\r\n\r\n", StringComparison.Ordinal) + 4;
+ var header = Encoding.ASCII.GetBytes(text[..bodyStart]);
+ var body = Encoding.ASCII.GetBytes(text[bodyStart..]);
+ var stuffed = new byte[header.Length + body.Length + 3];
+ header.CopyTo(stuffed, 0);
+ body.CopyTo(stuffed, header.Length);
+ stuffed[^3] = (byte)'.';
+ stuffed[^2] = (byte)'\r';
+ stuffed[^1] = (byte)'\n';
+ var command = Encoding.ASCII.GetBytes($"TAKETHIS {messageId}\r\n");
+ var framed = new byte[command.Length + stuffed.Length];
+ command.CopyTo(framed, 0);
+ stuffed.CopyTo(framed, command.Length);
+ return framed;
+ }
+
+ private sealed class InMemoryHarness : IAsyncDisposable
+ {
+ private readonly CancellationTokenSource _cts = new();
+ private readonly Task _run;
+ private readonly PipeDuplex _transport;
+
+ private InMemoryHarness(PipeDuplex transport, StorageNntpFeedSession session)
+ {
+ _transport = transport;
+ _run = session.RunAsync(_cts.Token);
+ }
+
+ public static async Task StartAsync(IAcceptedArticleQueue queue)
+ {
+ var transport = new PipeDuplex();
+ var session = new StorageNntpFeedSession(
+ transport,
+ new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8),
+ NullLogger.Instance,
+ "cache01.usenet.ninja",
+ acceptedArticles: queue,
+ assemblyAdmission: new StoreAssemblyAdmission(8));
+ var harness = new InMemoryHarness(transport, session);
+ _ = await harness.ReadStatusAsync();
+ return harness;
+ }
+
+ public async Task TakeThisAsync(byte[] framed)
+ {
+ await _transport.ClientWriteAsync(framed);
+ return await ReadStatusAsync();
+ }
+
+ private async Task ReadStatusAsync()
+ {
+ var line = new List(64);
+ while (true)
+ {
+ var b = new byte[1];
+ var read = await _transport.ClientReadAsync(b);
+ if (read == 0)
+ {
+ throw new EndOfStreamException();
+ }
+
+ if (b[0] == (byte)'\n' && line.Count > 0 && line[^1] == (byte)'\r')
+ {
+ line.RemoveAt(line.Count - 1);
+ break;
+ }
+
+ line.Add(b[0]);
+ }
+
+ var text = Encoding.ASCII.GetString(line.ToArray());
+ return int.Parse(text.AsSpan(0, 3));
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ await _cts.CancelAsync();
+ await _transport.DisposeAsync();
+ try
+ {
+ await _run;
+ }
+ catch (OperationCanceledException ex)
+ {
+ // Expected when DisposeAsync cancels the session RunAsync token.
+ ExpectedException.Ignore(ex);
+ }
+
+ _cts.Dispose();
+ }
+ }
+
+ private sealed class PipeDuplex : IStorageListenerTransport
+ {
+ private readonly Channel _toServer = Channel.CreateUnbounded();
+ private readonly Channel _toClient = Channel.CreateUnbounded();
+ private byte[] _serverPending = [];
+ private int _serverPendingOffset;
+ private byte[] _clientPending = [];
+ private int _clientPendingOffset;
+
+ public Task ClientWriteAsync(byte[] bytes) =>
+ _toServer.Writer.WriteAsync(bytes).AsTask();
+
+ public async Task ClientReadAsync(byte[] buffer)
+ {
+ if (_clientPendingOffset >= _clientPending.Length)
+ {
+ _clientPending = await _toClient.Reader.ReadAsync().ConfigureAwait(false);
+ _clientPendingOffset = 0;
+ }
+
+ var count = Math.Min(buffer.Length, _clientPending.Length - _clientPendingOffset);
+ _clientPending.AsSpan(_clientPendingOffset, count).CopyTo(buffer);
+ _clientPendingOffset += count;
+ return count;
+ }
+
+ public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken)
+ {
+ if (_serverPendingOffset >= _serverPending.Length)
+ {
+ _serverPending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
+ _serverPendingOffset = 0;
+ }
+
+ var count = Math.Min(buffer.Length, _serverPending.Length - _serverPendingOffset);
+ _serverPending.AsSpan(_serverPendingOffset, count).CopyTo(buffer.Span);
+ _serverPendingOffset += count;
+ return count;
+ }
+
+ public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken)
+ {
+ await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false);
+ return buffer.Length;
+ }
+
+ public ValueTask DisposeAsync()
+ {
+ _toServer.Writer.TryComplete();
+ _toClient.Writer.TryComplete();
+ return ValueTask.CompletedTask;
+ }
+ }
+
+ public static async Task StartPlacementIngressHostAsync()
+ {
+ var port = StorageServerTestOptions.GetFreeTcpPort();
+ var identity = StorageServerTestOptions.CreateValid();
+ identity.BindAddress = ["127.0.0.1"];
+ identity.BindPortTls = port;
+ var acme = StorageServerTestOptions.CreateValidAcme(identity);
+ acme.BindAddress = ["127.0.0.1"];
+ acme.BindPortTls = port;
+ var runtime = StorageServerRuntimeOptionsFactory.Create(identity, acme);
+ var queue = new AcceptedArticleQueue();
+ var readiness = new AcmeCertificateReadiness();
+ readiness.MarkReady();
+ var listener = new StorageNntpListenerService(
+ runtime,
+ TestListenerCertificates.CreatePublishedProvider(),
+ readiness,
+ NullLogger.Instance,
+ acceptedArticles: queue,
+ assemblyAdmission: new StoreAssemblyAdmission(64));
+ await listener.StartAsync(CancellationToken.None);
+ var target = new StorageServerFleetEntry(
+ 1,
+ "cache01.usenet.ninja",
+ 10_000,
+ 0,
+ 10_000,
+ DateTimeOffset.UtcNow,
+ port);
+ return new PlacementIngressHost(port, queue, listener, target);
+ }
+
+ private static ArticleRecord CreateRecord()
+ {
+ var text =
+ "Path: peer.example\r\n" +
+ "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" +
+ "Message-ID: \r\n" +
+ "Newsgroups: alt.test\r\n" +
+ "From: user@example.test\r\n" +
+ "Subject: bench\r\n" +
+ "\r\n" +
+ new string('x', BodyBytes) + "\r\n";
+ var created = ArticleRecordFactory.TryCreate(
+ new NntpArticleParser("nntpd01.usenet.ninja"),
+ Encoding.ASCII.GetBytes(text),
+ ArticlePathMode.Normalize);
+ Assert.True(created.IsAccepted);
+ return created.Record;
+ }
+
+ public sealed class PlacementIngressHost : IAsyncDisposable
+ {
+ private readonly StorageNntpListenerService _listener;
+
+ internal PlacementIngressHost(
+ int port,
+ AcceptedArticleQueue queue,
+ StorageNntpListenerService listener,
+ StorageServerFleetEntry target)
+ {
+ Port = port;
+ Queue = queue;
+ _listener = listener;
+ Target = target;
+ }
+
+ public int Port { get; }
+
+ public AcceptedArticleQueue Queue { get; }
+
+ public StorageServerFleetEntry Target { get; }
+
+ public async ValueTask DisposeAsync() => await _listener.DisposeAsync();
+ }
+ }
+}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpTransportTimeoutTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpTransportTimeoutTests.cs
similarity index 77%
rename from tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpTransportTimeoutTests.cs
rename to tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpTransportTimeoutTests.cs
index fcc8d948..f06b516a 100644
--- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpTransportTimeoutTests.cs
+++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpTransportTimeoutTests.cs
@@ -2,13 +2,22 @@
namespace VectorNNTPD.StorageService.Tests.Listener
{
+ ///
+ /// Process-wide I/O and TLS sample counters on must not
+ /// race with listener or placement tests that construct the same type.
+ ///
+ [CollectionDefinition("StorageNntpTransportCounters", DisableParallelization = true)]
+ public sealed class StorageNntpTransportCountersCollection
+ {
+ }
- public sealed class StorageVatpTransportTimeoutTests
+ [Collection("StorageNntpTransportCounters")]
+ public sealed class StorageNntpTransportTimeoutTests
{
[Fact]
public async Task Successive_reads_complete()
{
- await using var transport = new StreamStorageVatpTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2));
+ await using var transport = new StreamStorageListenerTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2));
Assert.Equal(1, await transport.ReadAsync(new byte[4], CancellationToken.None));
Assert.Equal(1, await transport.ReadAsync(new byte[4], CancellationToken.None));
}
@@ -16,7 +25,7 @@ public async Task Successive_reads_complete()
[Fact]
public async Task Second_stalled_read_still_times_out_after_a_successful_read()
{
- await using var transport = new StreamStorageVatpTransport(new ScriptedStream(1), TimeSpan.FromMilliseconds(80));
+ await using var transport = new StreamStorageListenerTransport(new ScriptedStream(1), TimeSpan.FromMilliseconds(80));
Assert.Equal(1, await transport.ReadAsync(new byte[4], CancellationToken.None));
await Assert.ThrowsAsync(async () =>
await transport.ReadAsync(new byte[4], CancellationToken.None));
@@ -26,7 +35,7 @@ await Assert.ThrowsAsync(async () =>
public async Task Write_during_a_blocked_read_does_not_cancel_the_read()
{
var stream = new HoldThenReleaseStream();
- await using var transport = new StreamStorageVatpTransport(stream, TimeSpan.FromSeconds(2));
+ await using var transport = new StreamStorageListenerTransport(stream, TimeSpan.FromSeconds(2));
var read = transport.ReadAsync(new byte[4], CancellationToken.None).AsTask();
await stream.ReadEntered.Task.WaitAsync(TimeSpan.FromSeconds(2));
await transport.WriteAsync(new byte[] { 9, 8, 7 }, CancellationToken.None);
@@ -40,7 +49,7 @@ public async Task Write_during_a_blocked_read_does_not_cancel_the_read()
public async Task Suspended_read_does_not_time_out_until_resumed()
{
var stream = new HoldThenReleaseStream();
- await using var transport = new StreamStorageVatpTransport(stream, TimeSpan.FromMilliseconds(80));
+ await using var transport = new StreamStorageListenerTransport(stream, TimeSpan.FromMilliseconds(80));
var read = transport.ReadAsync(new byte[4], CancellationToken.None).AsTask();
await stream.ReadEntered.Task.WaitAsync(TimeSpan.FromSeconds(2));
transport.SetReadProgressSuspended(true);
@@ -56,7 +65,7 @@ public async Task Suspended_read_does_not_time_out_until_resumed()
public async Task Stalled_write_times_out_without_cancelling_a_suspended_read()
{
var stream = new HoldThenReleaseStream { BlockWrites = true };
- await using var transport = new StreamStorageVatpTransport(stream, TimeSpan.FromMilliseconds(80));
+ await using var transport = new StreamStorageListenerTransport(stream, TimeSpan.FromMilliseconds(80));
var read = transport.ReadAsync(new byte[4], CancellationToken.None).AsTask();
await stream.ReadEntered.Task.WaitAsync(TimeSpan.FromSeconds(2));
transport.SetReadProgressSuspended(true);
@@ -70,7 +79,7 @@ await Assert.ThrowsAsync(async () =>
[Fact]
public async Task Caller_cancellation_is_not_reported_as_an_idle_timeout()
{
- await using var transport = new StreamStorageVatpTransport(new ScriptedStream(0), TimeSpan.FromSeconds(30));
+ await using var transport = new StreamStorageListenerTransport(new ScriptedStream(0), TimeSpan.FromSeconds(30));
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(80));
await Assert.ThrowsAnyAsync(async () =>
await transport.ReadAsync(new byte[4], cts.Token));
@@ -79,32 +88,31 @@ await Assert.ThrowsAnyAsync(async () =>
[Fact]
public async Task Independent_transports_keep_separate_timeout_state_and_share_process_io_counters()
{
- // Io-split counters are intentionally process-wide; timeout CTS ownership stays per instance.
- StreamStorageVatpTransport.ResetIoSplit();
- await using var left = new StreamStorageVatpTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2));
+ StreamStorageListenerTransport.ResetIoSplit();
+ await using var left = new StreamStorageListenerTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2));
var rightStream = new MemoryStream();
rightStream.Write([1, 2, 3]);
rightStream.Position = 0;
- await using var right = new StreamStorageVatpTransport(rightStream, TimeSpan.FromSeconds(2));
+ await using var right = new StreamStorageListenerTransport(rightStream, TimeSpan.FromSeconds(2));
Assert.Equal(1, await left.ReadAsync(new byte[4], CancellationToken.None));
Assert.Equal(3, await right.ReadAsync(new byte[4], CancellationToken.None));
await right.WriteAsync(new byte[] { 9 }, CancellationToken.None);
- var (timeoutSeconds, streamReadSeconds) = StreamStorageVatpTransport.ReadIoSplit();
+ var (timeoutSeconds, streamReadSeconds) = StreamStorageListenerTransport.ReadIoSplit();
Assert.True(timeoutSeconds >= 0);
Assert.True(streamReadSeconds >= 0);
Assert.True(timeoutSeconds + streamReadSeconds > 0);
- var tls = StreamStorageVatpTransport.ReadTlsThreadCost();
+ var tls = StreamStorageListenerTransport.ReadTlsThreadCost();
Assert.True(tls.ReadSameThread + tls.ReadHops >= 2);
Assert.True(tls.WriteSameThread + tls.WriteHops >= 1);
await left.DisposeAsync();
Assert.Equal(0, await right.ReadAsync(new byte[4], CancellationToken.None));
- StreamStorageVatpTransport.ResetIoSplit();
- var afterReset = StreamStorageVatpTransport.ReadIoSplit();
+ StreamStorageListenerTransport.ResetIoSplit();
+ var afterReset = StreamStorageListenerTransport.ReadIoSplit();
Assert.Equal(0, afterReset.TimeoutSetupSeconds);
Assert.Equal(0, afterReset.StreamReadSeconds);
}
@@ -112,10 +120,9 @@ public async Task Independent_transports_keep_separate_timeout_state_and_share_p
[Fact]
public async Task Concurrent_reads_on_separate_transports_do_not_corrupt_instance_lifecycle()
{
- // One outstanding read per transport (contract); both transports may run in parallel.
- StreamStorageVatpTransport.ResetIoSplit();
- await using var left = new StreamStorageVatpTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2));
- await using var right = new StreamStorageVatpTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2));
+ StreamStorageListenerTransport.ResetIoSplit();
+ await using var left = new StreamStorageListenerTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2));
+ await using var right = new StreamStorageListenerTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2));
for (var i = 0; i < 8; i++)
{
@@ -126,7 +133,9 @@ public async Task Concurrent_reads_on_separate_transports_do_not_corrupt_instanc
Assert.Equal(1, pair[1]);
}
- var tls = StreamStorageVatpTransport.ReadTlsThreadCost();
+ var tls = StreamStorageListenerTransport.ReadTlsThreadCost();
+ // Exact sample count: this collection disables parallelization so other tests cannot
+ // increment the process-wide counters between ResetIoSplit and this assert.
Assert.Equal(16, tls.ReadSameThread + tls.ReadHops);
}
@@ -217,16 +226,14 @@ public override void Flush()
public override void SetLength(long value) => throw new NotSupportedException();
- public override void Write(byte[] buffer, int offset, int count) => Written += count;
-
- public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default)
+ public override void Write(byte[] buffer, int offset, int count)
{
if (BlockWrites)
{
- await Task.Delay(Timeout.Infinite, cancellationToken);
+ throw new NotSupportedException();
}
- Written += buffer.Length;
+ Written += count;
}
public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default)
@@ -236,6 +243,17 @@ public override async ValueTask ReadAsync(Memory buffer, Cancellation
buffer.Span[0] = (byte)_releaseByte;
return 1;
}
+
+ public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default)
+ {
+ if (BlockWrites)
+ {
+ await Task.Delay(Timeout.Infinite, cancellationToken);
+ return;
+ }
+
+ Written += buffer.Length;
+ }
}
}
}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpLiveBackpressureTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpLiveBackpressureTests.cs
deleted file mode 100644
index 3c70bb5a..00000000
--- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpLiveBackpressureTests.cs
+++ /dev/null
@@ -1,1039 +0,0 @@
-using System.Buffers;
-using System.Net;
-using System.Net.Security;
-using System.Net.Sockets;
-using System.Security.Authentication;
-using System.Text;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Logging.Abstractions;
-using VectorNNTPD.Common.Acme;
-using VectorNNTPD.Common.Networking.Certificates;
-using VectorNNTPD.Common.Articles;
-using VectorNNTPD.Common.Articles.Parsing;
-using VectorNNTPD.Common.Transport.ArticleTransfer;
-using VectorNNTPD.StorageService.Tests.Support.Placement;
-using VectorNNTPD.StorageService.Configuration;
-using VectorNNTPD.StorageService.Ingress;
-using VectorNNTPD.StorageService.Listener;
-using VectorNNTPD.StorageService.Tests.Fixtures;
-using VectorNNTPD.StorageService.Tests.TestDoubles;
-using VectorNNTPD.StorageService;
-
-namespace VectorNNTPD.StorageService.Tests.Listener
-{
-
- ///
- /// Live TLS proof that STORE withholds body credit until the isolated queue can reserve ArtSize.
- /// The host binds 127.0.0.1 on an ephemeral port and does not publish DNS or touch a spool.
- ///
- public sealed class StorageVatpLiveBackpressureTests
- {
- private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10);
-
- [Fact]
- public async Task Available_capacity_orders_reservation_window_data_result_before_publication()
- {
- var article = Article("avail-0001");
- var budget = article.ArtSize * 4L;
- var publisher = new GatePublisher();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: true);
- await using var trace = await TracingClient.ConnectAsync(host.Port);
-
- await trace.SendStoreAndMetaAsync(article);
- var outcome = await trace.SendBodyAfterWindowAsync(article);
- Assert.Equal(VatpStoreOutcome.Accepted, outcome);
- Assert.Equal(0, trace.DataFramesBeforeWindow);
- Assert.Equal(1, trace.WindowsBeforeData);
- Assert.Equal((uint)ArticleTransferLimits.Default.InitialStreamWindowBytes, trace.FirstWindowCredit);
- Assert.True(trace.DataFrames >= 1);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.True(host.Queue.AccountedBytes <= budget);
-
- await WaitUntilAsync(() => publisher.Entered >= 1);
- Assert.Equal(0, publisher.Finished);
- publisher.Release();
- await WaitUntilAsync(() => publisher.Finished >= 1);
- await WaitUntilAsync(() => host.Queue.AccountedBytes == 0 && host.Queue.AdmissionWaiters == 0);
-
- await using var client = PlacementClient();
- var beforeData = client.Snapshot().DataFramesWritten;
- var placed = await client.PlaceAsync(article, host.Target, CancellationToken.None);
- Assert.Equal(ArticlePlacementKind.Accepted, placed.Kind);
- Assert.True(client.Snapshot().DataFramesWritten > beforeData);
- await WaitUntilAsync(() => publisher.Finished >= 2 && host.Queue.AccountedBytes == 0);
-
- WriteEvidence(
- "available",
- budget,
- article.ArtSize,
- host,
- deferred: host.Logger.Count("waiting for queue capacity"),
- granted: 2,
- dataBeforeAuthorization: trace.DataFramesBeforeWindow,
- results: "Accepted,Accepted",
- published: publisher.Finished,
- cleanup: "accounted=0",
- trace.TraceText());
- }
-
- [Fact]
- public async Task Full_queue_withholds_body_until_capacity_returns_and_then_publishes()
- {
- var filler = Article("full-filler");
- var waiter = Article("full-waiter");
- Assert.Equal(filler.ArtSize, waiter.ArtSize);
- var budget = filler.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false);
-
- await using var fillerClient = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await fillerClient.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
- Assert.Equal(budget, host.Queue.QueuedBytes);
- Assert.Equal(0, host.Queue.ReservedBytes);
-
- await using var blocked = await TracingClient.ConnectAsync(host.Port);
- await blocked.SendStoreAndMetaAsync(waiter);
- await WaitUntilAsync(
- () => host.Queue.AdmissionWaiters == 1
- && host.Logger.Text.Contains("waiting for queue capacity", StringComparison.Ordinal));
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(budget, host.Queue.AccountedBytes);
- Assert.Equal(0, blocked.DataFrames);
- Assert.Equal(0, blocked.WindowsBeforeData);
- Assert.Equal(1, host.Queue.EnqueuedCount);
- Assert.Contains("waiting for queue capacity", host.Logger.Text, StringComparison.Ordinal);
-
- var delivery = await host.Queue.DequeueAsync(CancellationToken.None);
- Assert.True(delivery.HasArticle);
- await blocked.WindowGranted.Task.WaitAsync(Safety);
- Assert.Equal(1, blocked.WindowsBeforeData);
- Assert.Equal(0, blocked.DataFramesBeforeWindow);
- Assert.Equal(0, blocked.DataFrames);
- Assert.Equal(waiter.ArtSize, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.QueuedBytes);
- Assert.True(host.Queue.AccountedBytes <= budget);
-
- Assert.Equal(0, publisher.Published);
- var outcome = await blocked.SendBodyAfterWindowAsync(waiter);
- Assert.Equal(VatpStoreOutcome.Accepted, outcome);
- Assert.Equal(0, blocked.DataFramesBeforeWindow);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes);
- Assert.Equal(0, publisher.Published);
- await publisher.PublishAsync(delivery.Article);
- Assert.Equal(1, publisher.Published);
-
- var queued = await host.Queue.DequeueAsync(CancellationToken.None);
- Assert.True(queued.HasArticle);
- Assert.True(queued.Article.ArtData.Span.SequenceEqual(waiter.ArtData.Span));
- await publisher.PublishAsync(queued.Article);
- await WaitUntilAsync(() =>
- host.Queue.AccountedBytes == 0
- && host.Queue.ReservedBytes == 0
- && host.Queue.AdmissionWaiters == 0);
-
- await using var production = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await production.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
- var dataBefore = production.Snapshot().DataFramesWritten;
- var creditBefore = production.Snapshot().CreditWaitTicks;
- var pending = production.PlaceAsync(waiter, host.Target, CancellationToken.None).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted);
- Assert.Equal(dataBefore, production.Snapshot().DataFramesWritten);
- Assert.Equal(creditBefore, production.Snapshot().CreditWaitTicks);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(budget, host.Queue.AccountedBytes);
-
- var held = await host.Queue.DequeueAsync(CancellationToken.None);
- var accepted = await pending.WaitAsync(Safety);
- Assert.Equal(ArticlePlacementKind.Accepted, accepted.Kind);
- Assert.True(production.Snapshot().CreditWaitTicks > creditBefore);
- Assert.True(production.Snapshot().DataFramesWritten > dataBefore);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes);
- await publisher.PublishAsync(held.Article);
- var second = await host.Queue.DequeueAsync(CancellationToken.None);
- await publisher.PublishAsync(second.Article);
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
-
- WriteEvidence(
- "full-and-recovery",
- budget,
- waiter.ArtSize,
- host,
- deferred: host.Logger.Count("waiting for queue capacity"),
- granted: 4,
- dataBeforeAuthorization: blocked.DataFramesBeforeWindow,
- results: "Accepted",
- published: publisher.Published,
- cleanup: "reserved=0 waiters=0",
- blocked.TraceText());
- }
-
- [Fact]
- public async Task Cancel_and_disconnect_release_waiters_reservations_and_partial_bodies()
- {
- var filler = Article("drop-filler");
- var cancelled = Article("drop-cancel");
- var disconnected = Article("drop-closeX");
- var reserved = Article("drop-window");
- var partial = Article("drop-partia");
- var follow = Article("drop-follow");
- Assert.Equal(filler.ArtSize, cancelled.ArtSize);
- Assert.Equal(filler.ArtSize, disconnected.ArtSize);
- Assert.Equal(filler.ArtSize, reserved.ArtSize);
- Assert.Equal(filler.ArtSize, partial.ArtSize);
- Assert.Equal(filler.ArtSize, follow.ArtSize);
- var budget = filler.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false);
- await using var owner = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
-
- await using var cancelClient = PlacementClient();
- using var cancel = new CancellationTokenSource();
- var dataBefore = cancelClient.Snapshot().DataFramesWritten;
- var cancelTask = cancelClient.PlaceAsync(cancelled, host.Target, cancel.Token).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1);
- Assert.Equal(dataBefore, cancelClient.Snapshot().DataFramesWritten);
- Assert.Equal(0, host.Queue.ReservedBytes);
- await cancel.CancelAsync();
- var cancelResult = await cancelTask.WaitAsync(Safety);
- Assert.Equal(ArticlePlacementKind.Cancelled, cancelResult.Kind);
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(budget, host.Queue.QueuedBytes);
- Assert.Equal(dataBefore, cancelClient.Snapshot().DataFramesWritten);
-
- await using var dropClient = PlacementClient();
- var dropTask = dropClient.PlaceAsync(disconnected, host.Target, CancellationToken.None).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1);
- Assert.Equal(0, dropClient.Snapshot().DataFramesWritten);
- await dropClient.DisposeAsync();
- var dropResult = await dropTask.WaitAsync(Safety);
- Assert.True(
- dropResult.Kind is ArticlePlacementKind.Cancelled or ArticlePlacementKind.TransportFailure,
- dropResult.Kind.ToString());
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0 && host.Queue.ReservedBytes == 0);
- Assert.Equal(budget, host.Queue.QueuedBytes);
- await WaitUntilAsync(() => host.Admission.InFlight == 0);
-
- await using var windowClient = await TracingClient.ConnectAsync(host.Port);
- await windowClient.SendStoreAndMetaAsync(reserved);
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1);
- var freed = await host.Queue.DequeueAsync(CancellationToken.None);
- await publisher.PublishAsync(freed.Article);
- await windowClient.WindowGranted.Task.WaitAsync(Safety);
- Assert.Equal(0, windowClient.DataFrames);
- Assert.Equal(reserved.ArtSize, host.Queue.ReservedBytes);
- await windowClient.DisposeAsync();
- await WaitUntilAsync(() => host.Queue.ReservedBytes == 0 && host.Queue.AdmissionWaiters == 0);
- Assert.Equal(0, host.Queue.QueuedBytes);
- await WaitUntilAsync(() => host.Admission.InFlight == 0);
-
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
- await using var partialClient = await TracingClient.ConnectAsync(host.Port);
- await partialClient.SendStoreAndMetaAsync(partial);
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1);
- var again = await host.Queue.DequeueAsync(CancellationToken.None);
- await publisher.PublishAsync(again.Article);
- await partialClient.WindowGranted.Task.WaitAsync(Safety);
- await partialClient.SendPartialDataAsync(partial);
- Assert.Equal(1, partialClient.DataFrames);
- Assert.Equal(0, partialClient.DataFramesBeforeWindow);
- Assert.Equal(partial.ArtSize, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.QueuedBytes);
- var enqueuedBeforeDrop = host.Queue.EnqueuedCount;
- await partialClient.DisposeAsync();
- await WaitUntilAsync(() => host.Queue.ReservedBytes == 0 && host.Queue.AdmissionWaiters == 0);
- Assert.Equal(enqueuedBeforeDrop, host.Queue.EnqueuedCount);
- Assert.Equal(0, host.Queue.AccountedBytes);
-
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(follow, host.Target, CancellationToken.None)).Kind);
- var delivered = await host.Queue.DequeueAsync(CancellationToken.None);
- Assert.True(delivered.Article.ArtData.Span.SequenceEqual(follow.ArtData.Span));
- await publisher.PublishAsync(delivered.Article);
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
-
- WriteEvidence(
- "cancel-disconnect",
- budget,
- filler.ArtSize,
- host,
- deferred: host.Logger.Count("waiting for queue capacity"),
- granted: 3,
- dataBeforeAuthorization: windowClient.DataFramesBeforeWindow + partialClient.DataFramesBeforeWindow,
- results: cancelResult.Kind + "," + dropResult.Kind + ",Accepted",
- published: publisher.Published,
- cleanup: "reserved=0 waiters=0 accounted=0",
- windowClient.TraceText());
- }
-
- [Fact]
- public async Task Repeated_admission_does_not_leak_reservations_or_send_early_data()
- {
- const int cycles = 8;
- var sample = Article("loop-000-a");
- var budget = sample.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false);
- await using var client = PlacementClient();
- var earlyData = 0;
- var deferred = 0;
- var granted = 0;
-
- for (var i = 0; i < cycles; i++)
- {
- var filler = Article(FormattableString.Invariant($"loop-{i:000}-a"));
- var waiter = Article(FormattableString.Invariant($"loop-{i:000}-b"));
- Assert.Equal(budget, filler.ArtSize);
- Assert.Equal(budget, waiter.ArtSize);
-
- Assert.Equal(ArticlePlacementKind.Accepted, (await client.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
- granted++;
- var dataBefore = client.Snapshot().DataFramesWritten;
- var creditBefore = client.Snapshot().CreditWaitTicks;
- var pending = client.PlaceAsync(waiter, host.Target, CancellationToken.None).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted);
- deferred++;
- if (client.Snapshot().DataFramesWritten != dataBefore || pending.IsCompleted)
- {
- earlyData++;
- }
-
- Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten);
- Assert.Equal(creditBefore, client.Snapshot().CreditWaitTicks);
- Assert.False(pending.IsCompleted);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(budget, host.Queue.QueuedBytes);
- Assert.Equal(budget, host.Queue.AccountedBytes);
-
- var held = await host.Queue.DequeueAsync(CancellationToken.None);
- var accepted = await pending.WaitAsync(Safety);
- Assert.Equal(ArticlePlacementKind.Accepted, accepted.Kind);
- granted++;
- Assert.True(client.Snapshot().CreditWaitTicks > creditBefore);
- Assert.True(client.Snapshot().DataFramesWritten > dataBefore);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes);
- await publisher.PublishAsync(held.Article);
- var queued = await host.Queue.DequeueAsync(CancellationToken.None);
- Assert.True(queued.Article.ArtData.Span.SequenceEqual(waiter.ArtData.Span));
- await publisher.PublishAsync(queued.Article);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.QueuedBytes);
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
- }
-
- Assert.Equal(0, earlyData);
- Assert.Equal(cycles, deferred);
- Assert.Equal(cycles * 2, granted);
- Assert.Equal(cycles * 2, publisher.Published);
-
- WriteEvidence(
- "repeated",
- budget,
- sample.ArtSize,
- host,
- deferred,
- granted,
- earlyData,
- results: "Accepted",
- published: publisher.Published,
- cleanup: "reserved=0 queued=0 waiters=0",
- trace: host.Logger.Text);
- }
-
- [Fact]
- public async Task Capacity_wait_outlives_io_progress_timeout_then_admits_and_publishes()
- {
- var timeout = TimeSpan.FromMilliseconds(200);
- var filler = Article("wait-filler");
- var waiter = Article("wait-waiter");
- Assert.Equal(filler.ArtSize, waiter.ArtSize);
- var budget = filler.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout);
- await using var owner = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
-
- await using var client = PlacementClient(timeout);
- var dataBefore = client.Snapshot().DataFramesWritten;
- var creditBefore = client.Snapshot().CreditWaitTicks;
- var pending = client.PlaceAsync(waiter, host.Target, CancellationToken.None).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted);
-
- // The delay is the subject: admission must outlive both no-progress timeouts.
- await Task.Delay(TimeSpan.FromMilliseconds(600));
- Assert.False(pending.IsCompleted);
- Assert.Equal(1, host.Queue.AdmissionWaiters);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(budget, host.Queue.AccountedBytes);
- Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten);
- Assert.Equal(creditBefore, client.Snapshot().CreditWaitTicks);
-
- var held = await host.Queue.DequeueAsync(CancellationToken.None);
- var accepted = await pending.WaitAsync(Safety);
- Assert.Equal(ArticlePlacementKind.Accepted, accepted.Kind);
- Assert.True(client.Snapshot().DataFramesWritten > dataBefore);
- Assert.True(client.Snapshot().CreditWaitTicks > creditBefore);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
- Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes);
- await publisher.PublishAsync(held.Article);
- var second = await host.Queue.DequeueAsync(CancellationToken.None);
- Assert.True(second.HasArticle);
- Assert.True(second.Article.ArtData.Span.SequenceEqual(waiter.ArtData.Span));
- await publisher.PublishAsync(second.Article);
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
- Assert.Equal(2, publisher.Published);
- }
-
- [Fact]
- public async Task Cancel_during_a_long_capacity_wait_releases_the_waiter()
- {
- var timeout = TimeSpan.FromMilliseconds(200);
- var filler = Article("cncl-filler");
- var waiting = Article("cncl-waiter");
- Assert.Equal(filler.ArtSize, waiting.ArtSize);
- var budget = filler.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout);
- await using var owner = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
-
- await using var client = PlacementClient(timeout);
- using var cancel = new CancellationTokenSource();
- var dataBefore = client.Snapshot().DataFramesWritten;
- var pending = client.PlaceAsync(waiting, host.Target, cancel.Token).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted);
-
- // The delay is the subject: cancellation must still be observed after the no-progress timeout.
- await Task.Delay(TimeSpan.FromMilliseconds(600));
- Assert.False(pending.IsCompleted);
- Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten);
- await cancel.CancelAsync();
- var result = await pending.WaitAsync(Safety);
- Assert.Equal(ArticlePlacementKind.Cancelled, result.Kind);
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(budget, host.Queue.QueuedBytes);
- Assert.Equal(budget, host.Queue.AccountedBytes);
- Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten);
- }
-
- [Fact]
- public async Task Disconnect_during_a_long_capacity_wait_lets_a_later_store_proceed()
- {
- var timeout = TimeSpan.FromMilliseconds(200);
- var filler = Article("disc-filler");
- var waiting = Article("disc-waiter");
- var follow = Article("disc-follow");
- Assert.Equal(filler.ArtSize, waiting.ArtSize);
- Assert.Equal(filler.ArtSize, follow.ArtSize);
- var budget = filler.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout);
- await using var owner = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
-
- await using var client = PlacementClient(timeout);
- var dataBefore = client.Snapshot().DataFramesWritten;
- var pending = client.PlaceAsync(waiting, host.Target, CancellationToken.None).AsTask();
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted);
-
- // The delay is the subject: disconnect cleanup must still run after the no-progress timeout.
- await Task.Delay(TimeSpan.FromMilliseconds(600));
- Assert.False(pending.IsCompleted);
- Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten);
- await client.DisposeAsync();
- var result = await pending.WaitAsync(Safety);
- Assert.Equal(ArticlePlacementKind.Cancelled, result.Kind);
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0 && host.Queue.ReservedBytes == 0);
- Assert.Equal(budget, host.Queue.QueuedBytes);
-
- var held = await host.Queue.DequeueAsync(CancellationToken.None);
- await publisher.PublishAsync(held.Article);
- await using var next = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await next.PlaceAsync(follow, host.Target, CancellationToken.None)).Kind);
- var queued = await host.Queue.DequeueAsync(CancellationToken.None);
- Assert.True(queued.HasArticle);
- Assert.True(queued.Article.ArtData.Span.SequenceEqual(follow.ArtData.Span));
- await publisher.PublishAsync(queued.Article);
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
- Assert.Equal(2, publisher.Published);
- }
-
- [Fact]
- public async Task Silent_connection_after_hello_still_times_out()
- {
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(1024 * 1024, publisher, startWorker: false, ioProgressTimeout: TimeSpan.FromMilliseconds(200));
- await using var trace = await TracingClient.ConnectAsync(host.Port);
- await trace.Closed.WaitAsync(TimeSpan.FromSeconds(2));
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, host.Queue.ReservedBytes);
- Assert.Equal(0, host.Queue.AdmissionWaiters);
- Assert.Equal(0, publisher.Published);
- }
-
- [Fact]
- public async Task Silence_after_window_still_times_out_and_releases_the_reservation()
- {
- var timeout = TimeSpan.FromMilliseconds(200);
- var filler = Article("stall-fillr");
- var waiter = Article("stall-waite");
- Assert.Equal(filler.ArtSize, waiter.ArtSize);
- var budget = filler.ArtSize;
- var publisher = new PublishCounter();
- await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout);
- await using var owner = PlacementClient();
- Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind);
-
- await using var trace = await TracingClient.ConnectAsync(host.Port);
- await trace.SendStoreAndMetaAsync(waiter);
- await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1);
- var held = await host.Queue.DequeueAsync(CancellationToken.None);
- await trace.WindowGranted.Task.WaitAsync(Safety);
- Assert.Equal(waiter.ArtSize, host.Queue.ReservedBytes);
- Assert.Equal(0, trace.DataFrames);
- await trace.Closed.WaitAsync(TimeSpan.FromSeconds(2));
- await WaitUntilAsync(() => host.Queue.ReservedBytes == 0 && host.Queue.AdmissionWaiters == 0);
- Assert.Equal(0, host.Queue.QueuedBytes);
- Assert.Equal(0, host.Queue.AccountedBytes);
- Assert.Equal(0, trace.DataFrames);
- await publisher.PublishAsync(held.Article);
- Assert.Equal(1, publisher.Published);
- }
-
- private static void WriteEvidence(
- string scenario,
- long budget,
- int artSize,
- LiveHost host,
- int deferred,
- int granted,
- int dataBeforeAuthorization,
- string results,
- int published,
- string cleanup,
- string trace)
- {
- var line =
- $"EVIDENCE scenario={scenario} budget={budget} artSize={artSize} queued={host.Queue.QueuedBytes} reserved={host.Queue.ReservedBytes} accounted={host.Queue.AccountedBytes} deferred={deferred} granted={granted} dataBeforeAuthorization={dataBeforeAuthorization} results={results} published={published} cleanup={cleanup}";
- Console.WriteLine(line);
- Console.WriteLine(trace);
- }
-
- private static ArticlePlacementClient PlacementClient(TimeSpan? ioTimeout = null) =>
- new(
- NullLogger.Instance,
- new VatpClientOptions { IoTimeout = ioTimeout ?? TimeSpan.FromSeconds(30) },
- ArticleTransferLimits.Default,
- maxOutstandingStores: 1)
- {
- TestTcpConnectHost = "127.0.0.1",
- ServerCertificateValidationCallback = static (_, _, _, _) => true,
- };
-
- private static async Task WaitUntilAsync(Func condition)
- {
- using var timeout = new CancellationTokenSource(Safety);
- while (!condition())
- {
- await Task.Delay(1, timeout.Token);
- }
- }
-
- private static ArticleRecord Article(string token)
- {
- var parser = new NntpArticleParser("nntpd01.usenet.ninja");
- var text = new StringBuilder()
- .Append("Path: peer.example\r\n")
- .Append("Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n")
- .Append("Message-ID: <").Append(token).Append("@seg.test>\r\n")
- .Append("Newsgroups: alt.test\r\n")
- .Append("From: user@example.test\r\n")
- .Append("Subject: vatp\r\n")
- .Append("\r\n")
- .Append("live-body\r\n");
- var created = ArticleRecordFactory.TryCreate(parser, Encoding.ASCII.GetBytes(text.ToString()));
- Assert.True(created.IsAccepted, created.MaterializeFailure.ToString());
- return created.Record;
- }
-
- private sealed class LiveHost : IAsyncDisposable
- {
- private readonly StorageVatpListenerService _listener;
- private readonly AcceptedArticleWorker? _worker;
- private readonly TlsCertificateContextProvider _certificates;
- private readonly GatePublisher? _gate;
-
- private LiveHost(
- StorageVatpListenerService listener,
- AcceptedArticleWorker? worker,
- TlsCertificateContextProvider certificates,
- GatePublisher? gate,
- AcceptedArticleQueue queue,
- StoreAssemblyAdmission admission,
- ListLogger logger,
- StorageServerFleetEntry target,
- int port)
- {
- _listener = listener;
- _worker = worker;
- _certificates = certificates;
- _gate = gate;
- Queue = queue;
- Admission = admission;
- Logger = logger;
- Target = target;
- Port = port;
- }
-
- public AcceptedArticleQueue Queue { get; }
-
- public StoreAssemblyAdmission Admission { get; }
-
- public ListLogger Logger { get; }
-
- public StorageServerFleetEntry Target { get; }
-
- public int Port { get; }
-
- public static async Task StartAsync(
- long budget,
- IAcceptedArticleProcessor processor,
- bool startWorker,
- TimeSpan? ioProgressTimeout = null)
- {
- var port = StorageServerTestOptions.GetFreeTcpPort();
- var identity = StorageServerTestOptions.CreateValid();
- identity.BindAddress = ["127.0.0.1"];
- identity.BindPortTls = port;
- identity.Listener.IoProgressTimeoutSeconds = 30;
- var acme = StorageServerTestOptions.CreateValidAcme(identity);
- acme.BindAddress = ["127.0.0.1"];
- acme.BindPortTls = port;
- var runtime = StorageServerRuntimeOptionsFactory.Create(identity, acme);
- if (ioProgressTimeout is { } progress)
- {
- runtime = runtime with { Listener = runtime.Listener with { IoProgressTimeout = progress } };
- }
- var readiness = new AcmeCertificateReadiness();
- readiness.MarkReady();
- var certificates = TestListenerCertificates.CreatePublishedProvider();
- var logger = new ListLogger();
- var queue = new AcceptedArticleQueue(budget, TimeProvider.System);
- GatePublisher? gate = processor as GatePublisher;
- var admission = new StoreAssemblyAdmission(8);
- var listener = new StorageVatpListenerService(
- runtime,
- certificates,
- NullStorageArticleOpenBoundary.Instance,
- readiness,
- logger,
- queue,
- admission);
- await listener.StartAsync(CancellationToken.None);
- AcceptedArticleWorker? worker = null;
- if (startWorker)
- {
- worker = new AcceptedArticleWorker(
- queue,
- processor,
- NullLogger.Instance,
- new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 });
- await worker.StartAsync(CancellationToken.None);
- }
-
- return new LiveHost(
- listener,
- worker,
- certificates,
- gate,
- queue,
- admission,
- logger,
- new StorageServerFleetEntry(identity.ServerId!.Value, identity.Fqdn, 1, 0, 1, DateTimeOffset.UtcNow, port),
- port);
- }
-
- public async ValueTask DisposeAsync()
- {
- _gate?.Release();
- await _listener.DisposeAsync();
- if (_worker is not null)
- {
- using var stop = new CancellationTokenSource(Safety);
- await _worker.StopAsync(stop.Token);
- }
-
- await _certificates.DisposeAsync();
- }
- }
-
- private sealed class ListLogger : ILogger
- {
- private readonly List _messages = [];
-
- public string Text
- {
- get
- {
- lock (_messages)
- {
- return string.Join('\n', _messages);
- }
- }
- }
-
- public int Count(string fragment)
- {
- lock (_messages)
- {
- return _messages.Count(message => message.Contains(fragment, StringComparison.Ordinal));
- }
- }
-
- public IDisposable? BeginScope(TState state) where TState : notnull => null;
-
- public bool IsEnabled(LogLevel logLevel) => true;
-
- public void Log(
- LogLevel logLevel,
- EventId eventId,
- TState state,
- Exception? exception,
- Func formatter)
- {
- lock (_messages)
- {
- _messages.Add(formatter(state, exception));
- }
- }
- }
-
- private sealed class PublishCounter : IAcceptedArticleProcessor
- {
- private int _published;
-
- public int Published => Volatile.Read(ref _published);
-
- public void EnsureReady()
- {
- }
-
- public ValueTask PublishAsync(AcceptedArticle article) =>
- ProcessAsync(article, CancellationToken.None);
-
- public ValueTask ProcessAsync(
- AcceptedArticle article,
- CancellationToken cancellationToken)
- {
- Interlocked.Increment(ref _published);
- return new ValueTask(AcceptedArticleProcessResult.Completed);
- }
- }
-
- private sealed class GatePublisher : IAcceptedArticleProcessor
- {
- private readonly TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously);
- private int _entered;
- private int _finished;
-
- public int Entered => Volatile.Read(ref _entered);
-
- public int Finished => Volatile.Read(ref _finished);
-
- public void Release() => _release.TrySetResult();
-
- public void EnsureReady()
- {
- }
-
- public async ValueTask ProcessAsync(
- AcceptedArticle article,
- CancellationToken cancellationToken)
- {
- Interlocked.Increment(ref _entered);
- await _release.Task.WaitAsync(cancellationToken);
- Interlocked.Increment(ref _finished);
- return AcceptedArticleProcessResult.Completed;
- }
- }
-
- private sealed class TracingClient : IAsyncDisposable
- {
- private readonly TcpClient _tcp;
- private readonly SslStream _ssl;
- private readonly CancellationTokenSource _dispose = new();
- private readonly object _gate = new();
- private readonly List _trace = [];
- private readonly Task _reader;
- private int _disposedClient;
- private long _credit;
- private uint _maxFrame = VatpProtocol.DefaultMaxFramePayload;
- private int _dataFrames;
- private int _windowsBeforeData;
- private readonly TaskCompletionSource _peerClosed = new(TaskCreationOptions.RunContinuationsAsynchronously);
-
- private TracingClient(TcpClient tcp, SslStream ssl)
- {
- _tcp = tcp;
- _ssl = ssl;
- WindowGranted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
- Result = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
- _reader = ReadLoopAsync();
- }
-
- public TaskCompletionSource WindowGranted { get; }
-
- public TaskCompletionSource Result { get; }
-
- public Task Closed => _peerClosed.Task;
-
- public int DataFrames => Volatile.Read(ref _dataFrames);
-
- public int DataFramesBeforeWindow { get; private set; }
-
- public int WindowsBeforeData => Volatile.Read(ref _windowsBeforeData);
-
- public uint FirstWindowCredit { get; private set; }
-
- public static async Task ConnectAsync(int port)
- {
- var tcp = new TcpClient();
- await tcp.ConnectAsync(IPAddress.Loopback, port);
- var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false);
- await ssl.AuthenticateAsClientAsync(
- new SslClientAuthenticationOptions
- {
- TargetHost = "localhost",
- EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13,
- RemoteCertificateValidationCallback = static (_, _, _, _) => true,
- },
- CancellationToken.None);
- var client = new TracingClient(tcp, ssl);
- await client.WriteAsync(VatpFrameEncoder.EncodeHello(VatpProtocol.DefaultMaxFramePayload), "C HELLO");
- await client.Hello.Task.WaitAsync(Safety);
- return client;
- }
-
- private TaskCompletionSource Hello { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
-
- public string TraceText()
- {
- lock (_gate)
- {
- return string.Join('\n', _trace);
- }
- }
-
- public async Task SendStoreAndMetaAsync(ArticleRecord record)
- {
- var id = new byte[ArticleId.Length];
- record.ArtId.CopyTo(id);
- await WriteAsync(VatpFrameEncoder.EncodeStore(1, id), "C STORE");
- Assert.True(ArticlePlacementMeta.TryFromRecord(in record, out var meta));
- await WriteAsync(VatpFrameEncoder.EncodeMeta(1, VatpMetaCodec.Encode(in meta)), "C META");
- }
-
- public async Task SendBodyAfterWindowAsync(ArticleRecord record)
- {
- await WindowGranted.Task.WaitAsync(Safety);
- Assert.True(record.ArtSize <= ArticleTransferLimits.Default.InitialStreamWindowBytes);
- await SendDataAsync(record.ArtData, fin: true);
- await WriteAsync(VatpFrameEncoder.EncodeEnd(1), "C END");
- return await Result.Task.WaitAsync(Safety);
- }
-
- public async Task SendPartialDataAsync(ArticleRecord record)
- {
- await WindowGranted.Task.WaitAsync(Safety);
- await SendDataAsync(record.ArtData.Slice(0, 1), fin: false);
- }
-
- public async ValueTask DisposeAsync()
- {
- if (Interlocked.Exchange(ref _disposedClient, 1) != 0)
- {
- return;
- }
-
- await _dispose.CancelAsync();
- try
- {
- await _reader.WaitAsync(TimeSpan.FromSeconds(2));
- }
- catch (TimeoutException ex)
- {
- ExpectedException.Ignore(ex);
- }
- catch (OperationCanceledException ex)
- {
- ExpectedException.Ignore(ex);
- }
-
- await _ssl.DisposeAsync();
- _tcp.Dispose();
- _dispose.Dispose();
- }
-
- private async Task SendDataAsync(ReadOnlyMemory payload, bool fin)
- {
- if (WindowsBeforeData == 0)
- {
- DataFramesBeforeWindow++;
- }
-
- Interlocked.Increment(ref _dataFrames);
- await WriteAsync(
- VatpFrameEncoder.EncodeData(1, payload, fin),
- fin ? $"C DATA {payload.Length} fin" : $"C DATA {payload.Length}");
- }
-
- private async Task WriteAsync(VatpFrameEncoder.EncodedFrame frame, string trace)
- {
- lock (_gate)
- {
- _trace.Add(trace);
- }
-
- var bytes = VatpFrameEncoder.ToSingleBuffer(frame);
- await _ssl.WriteAsync(bytes, _dispose.Token);
- }
-
- private async Task ReadLoopAsync()
- {
- var buffer = new byte[64 * 1024];
- var count = 0;
- try
- {
- while (!_dispose.IsCancellationRequested)
- {
- while (TryTakeFrame(buffer, ref count))
- {
- }
-
- if (count == buffer.Length)
- {
- Array.Resize(ref buffer, buffer.Length * 2);
- }
-
- var read = await _ssl.ReadAsync(buffer.AsMemory(count), _dispose.Token);
- if (read == 0)
- {
- _peerClosed.TrySetResult();
- return;
- }
-
- count += read;
- }
- }
- catch (OperationCanceledException ex)
- {
- ExpectedException.Ignore(ex);
- }
- catch (Exception ex) when (ex is IOException or ObjectDisposedException)
- {
- _peerClosed.TrySetResult();
- }
- }
-
- private bool TryTakeFrame(byte[] buffer, ref int count)
- {
- if (count < VatpProtocol.HeaderLengthBytes)
- {
- return false;
- }
-
- var parsed = VatpFrameParser.ParseOneFrame(new ReadOnlySequence(buffer, 0, count), _maxFrame);
- if (parsed.Status == VatpFrameParseStatus.Incomplete)
- {
- return false;
- }
-
- if (parsed.Status != VatpFrameParseStatus.Success || parsed.Frame is not { } frame)
- {
- throw new InvalidOperationException(parsed.Error.ToString());
- }
-
- Note(frame);
- var consumed = checked((int)parsed.ConsumedBytes);
- Buffer.BlockCopy(buffer, consumed, buffer, 0, count - consumed);
- count -= consumed;
- return true;
- }
-
- private void Note(VatpParsedFrame frame)
- {
- var payload = new byte[frame.Payload.Length];
- frame.Payload.CopyTo(payload);
- switch (frame.Header.Type)
- {
- case VatpFrameType.Hello:
- if (VatpHello.TryDecode(payload, out var hello, out _))
- {
- _maxFrame = Math.Min(hello.MaxFramePayload, VatpProtocol.DefaultMaxFramePayload);
- }
-
- lock (_gate)
- {
- _trace.Add("S HELLO");
- }
-
- Hello.TrySetResult();
- break;
- case VatpFrameType.Window:
- Assert.True(VatpControlPayload.TryDecodeWindow(payload, out var add));
- lock (_gate)
- {
- _trace.Add($"S WINDOW {add}");
- _credit += add;
- if (_dataFrames == 0)
- {
- if (FirstWindowCredit == 0)
- {
- FirstWindowCredit = add;
- }
-
- _windowsBeforeData++;
- }
- }
-
- WindowGranted.TrySetResult();
- break;
- case VatpFrameType.Result:
- lock (_gate)
- {
- _trace.Add($"S RESULT {payload[0]}");
- }
-
- Result.TrySetResult(payload[0]);
- break;
- case VatpFrameType.Fail:
- lock (_gate)
- {
- _trace.Add("S FAIL");
- }
-
- break;
- default:
- lock (_gate)
- {
- _trace.Add("S " + frame.Header.Type);
- }
-
- break;
- }
- }
- }
- }
-}
diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpQueueIngressTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpQueueIngressTests.cs
deleted file mode 100644
index 36514989..00000000
--- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpQueueIngressTests.cs
+++ /dev/null
@@ -1,874 +0,0 @@
-using System.Buffers;
-using System.Text;
-using System.Threading.Channels;
-using Microsoft.Extensions.Logging.Abstractions;
-using VectorNNTPD.Common.Articles;
-using VectorNNTPD.Common.Articles.Parsing;
-using VectorNNTPD.Common.Transport.ArticleTransfer;
-using VectorNNTPD.StorageService.Configuration;
-using VectorNNTPD.StorageService.Ingress;
-using VectorNNTPD.StorageService.Listener;
-using VectorNNTPD.StorageService;
-
-namespace VectorNNTPD.StorageService.Tests.Listener
-{
-
- public sealed class StorageVatpQueueIngressTests
- {
- private const string CacheFqdn = "cache01.usenet.ninja";
- private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10);
-
- [Fact]
- public async Task Store_enqueues_the_original_article_without_this_servers_path_hop()
- {
- var inbound = Build("", "body-line\r\n");
- var queue = new AcceptedArticleQueue();
- await using var harness = await Harness.StartAsync(queue);
- var frames = await harness.StoreAndReadAsync(inbound);
- Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames));
-
- var delivery = await queue.DequeueAsync(CancellationToken.None);
- Assert.True(delivery.HasArticle);
- Assert.Equal(inbound.Record.ArtId, delivery.Article.ArtId);
- Assert.Equal(inbound.Record.ArtHash, delivery.Article.ArtHash);
- Assert.Equal(inbound.Record.ArtSize, delivery.Article.ArtSize);
- Assert.True(delivery.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
- var queued = Encoding.ASCII.GetString(delivery.Article.ArtData.Span);
- Assert.Equal(0, CountHop(queued, CacheFqdn));
- Assert.Contains("Path: ", queued, StringComparison.Ordinal);
- Assert.Contains("body-line\r\n", queued, StringComparison.Ordinal);
- Assert.Contains("news.usenet.ninja", queued, StringComparison.Ordinal);
- }
-
- [Fact]
- public async Task Store_of_bytes_that_already_contain_this_hop_prepends_it_again()
- {
- var inbound = Build("", "kept-body\r\n", CacheFqdn);
- var inboundText = Encoding.ASCII.GetString(inbound.Record.ArtData.Span);
- Assert.Equal(1, CountHop(inboundText, CacheFqdn));
-
- var queue = new AcceptedArticleQueue();
- await using var harness = await Harness.StartAsync(queue);
- Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await harness.StoreAndReadAsync(inbound)));
-
- var delivery = await queue.DequeueAsync(CancellationToken.None);
- Assert.True(delivery.HasArticle);
- var queued = Encoding.ASCII.GetString(delivery.Article.ArtData.Span);
- Assert.True(delivery.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
- Assert.Equal(1, CountHop(queued, CacheFqdn));
- Assert.Equal(Body(inboundText), Body(queued));
- Assert.Contains("news.usenet.ninja", queued, StringComparison.Ordinal);
- }
-
- [Fact]
- public async Task Second_store_of_the_same_bytes_enqueues_another_unmodified_copy()
- {
- var inbound = Build("", "body\r\n");
- var queue = new AcceptedArticleQueue();
- await using var harness = await Harness.StartAsync(queue);
- Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await harness.StoreAndReadAsync(inbound)));
- Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await harness.StoreAndReadAsync(inbound, streamId: 2)));
-
- var first = await queue.DequeueAsync(CancellationToken.None);
- var second = await queue.DequeueAsync(CancellationToken.None);
- Assert.True(first.HasArticle && second.HasArticle);
- Assert.True(first.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
- Assert.True(second.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span));
- Assert.Equal(0, CountHop(Encoding.ASCII.GetString(first.Article.ArtData.Span), CacheFqdn));
- Assert.Equal(0, CountHop(Encoding.ASCII.GetString(second.Article.ArtData.Span), CacheFqdn));
- }
-
- [Fact]
- public async Task Result_is_sent_before_persistence_finishes()
- {
- var inbound = Build("", "body\r\n");
- var queue = new AcceptedArticleQueue();
- var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
- var processor = new BlockingProcessor(release.Task);
- var worker = new AcceptedArticleWorker(
- queue,
- processor,
- NullLogger.Instance,
- new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 },
- new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(),
- new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(),
- TimeProvider.System);
- await worker.StartAsync(CancellationToken.None);
- try
- {
- await using var harness = await Harness.StartAsync(queue);
- var frames = await harness.StoreAndReadAsync(inbound).WaitAsync(TimeSpan.FromSeconds(2));
- Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames));
- Assert.Equal(0, processor.Finished);
- }
- finally
- {
- release.TrySetResult();
- await worker.StopAsync(CancellationToken.None);
- }
- }
-
- [Fact]
- public async Task Hash_id_and_size_mismatches_do_not_enqueue()
- {
- var real = Build("", "body\r\n");
- var other = Build("", "body\r\n");
- var queue = new AcceptedArticleQueue();
-
- await using var idMismatch = await Harness.StartAsync(queue);
- await idMismatch.SendRawAsync(StoreFrame(other.Record.ArtId, 1));
- await idMismatch.SendArticleBodyAsync(real, 1);
- Assert.Equal(VatpErrorCode.ArticleIdMismatch, FailCode(await idMismatch.ReadUntilAsync(static frames =>
- frames.Any(frame => frame.Header.Type == VatpFrameType.Fail))));
-
- await using var hashMismatch = await Harness.StartAsync(queue);
- await hashMismatch.SendStoreAsync(real, corruptLastDataByte: true);
- Assert.Equal(VatpErrorCode.ArtHashMismatch, FailCode(await hashMismatch.ReadUntilAsync(static frames =>
- frames.Any(frame => frame.Header.Type == VatpFrameType.Fail))));
-
- await using var sizeMismatch = await Harness.StartAsync(queue);
- await sizeMismatch.SendRawAsync(StoreFrame(real.Record.ArtId, 1));
- await sizeMismatch.SendRawAsync(MetaFrame(real, 1));
- var shortPayload = real.Record.ArtData.Slice(0, Math.Max(1, real.Record.ArtSize / 2));
- await sizeMismatch.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(
- VatpFrameEncoder.EncodeData(1, shortPayload, fin: true)));
- await sizeMismatch.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(1)));
- Assert.Equal(VatpErrorCode.ArtSizeMismatch, FailCode(await sizeMismatch.ReadUntilAsync(static frames =>
- frames.Any(frame => frame.Header.Type == VatpFrameType.Fail))));
- Assert.Equal(0, queue.ReservedBytes);
- Assert.Equal(0, queue.AccountedBytes);
-
- using var timeout = new CancellationTokenSource(TimeSpan.FromMilliseconds(50));
- await Assert.ThrowsAnyAsync(async () =>
- await queue.DequeueAsync(timeout.Token));
- }
-
- [Fact]
- public async Task Oversized_meta_is_rejected_before_enqueue()
- {
- var built = Build("", "body\r\n");
- var queue = new AcceptedArticleQueue();
- await using var harness = await Harness.StartAsync(queue);
- await harness.SendRawAsync(StoreFrame(built.Record.ArtId, 1));
- var meta = MetaFrame(built, 1);
- System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(
- meta.AsSpan(VatpProtocol.HeaderLengthBytes + 12, 4),
- ArticleResourceLimits.MaxArticleBytes + 1);
- await harness.SendRawAsync(meta);
- Assert.Equal(VatpErrorCode.ArticleTooLarge, FailCode(await harness.ReadUntilAsync(static frames =>
- frames.Any(frame => frame.Header.Type == VatpFrameType.Fail))));
- using var timeout = new CancellationTokenSource(TimeSpan.FromMilliseconds(50));
- await Assert.ThrowsAnyAsync