fix(events): card reports cannot crowd the event queues or hold an import
The server publishes one account's card progress at most once a second (sooner reports are taken and dropped) and at most 120 card events of an account a minute (429 rate_limited beyond), so an upload-only account can no longer push job and sequence events out of every client's queue. On the client, the end of a card import waits at most two seconds for its reports to reach the server, then cancels whatever is still on its way, so a cancelled import or one that lost its server ends at once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
5 files changed
+146
-12
No files matched your search
@@ -1,6 +1,5 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
@@ -37,8 +36,11 @@ public static class UploadEndpoints
|
||||
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
|
||||
}
|
||||
|
||||
/// <summary>A card import's start, progress or end, published as a <see cref="CardEvent"/> naming who imports it; 204.</summary>
|
||||
private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, EventHub events)
|
||||
/// <summary>
|
||||
/// A card import's start, progress or end, published as a <see cref="CardEvent"/> naming who imports it; 204, also for
|
||||
/// progress dropped because it came within a second of the last (<see cref="UploadService.PublishCard"/>).
|
||||
/// </summary>
|
||||
private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, UploadService service)
|
||||
{
|
||||
if (report.ImportId == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "importId");
|
||||
var card = report.Card?.Trim();
|
||||
@@ -46,8 +48,8 @@ public static class UploadEndpoints
|
||||
if (!CardImportStates.IsKnown(report.State)) return ApiErrors.BadRequest(context, "bad_request", "state");
|
||||
if (report.Total < 0 || report.Done < 0 || report.Done > report.Total || report.Failed < 0 || report.Failed > report.Total)
|
||||
return ApiErrors.BadRequest(context, "bad_request", "done");
|
||||
events.Publish(new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username, caller.ClientName));
|
||||
return Results.NoContent();
|
||||
return service.PublishCard(caller, new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username,
|
||||
caller.ClientName)).ToResult(context, _ => Results.NoContent());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -36,7 +36,14 @@ public sealed class UploadService(
|
||||
/// <summary>Space kept free on the upload volume beyond the announced size.</summary>
|
||||
private const long DiskReserve = 64L * 1024 * 1024;
|
||||
|
||||
/// <summary>Card events one account may publish per minute: progress once a second leaves room for every start and end.</summary>
|
||||
private const int CardEventsPerMinute = 120;
|
||||
|
||||
private static readonly TimeSpan CardProgressInterval = TimeSpan.FromSeconds(1);
|
||||
private static readonly TimeSpan CardEventWindow = TimeSpan.FromMinutes(1);
|
||||
|
||||
private readonly ConcurrentDictionary<Guid, int> _chunksInFlight = new();
|
||||
private readonly ConcurrentDictionary<Guid, CardEvents> _cardEvents = new();
|
||||
|
||||
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, StartUploadRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -204,6 +211,32 @@ public sealed class UploadService(
|
||||
return new CompleteUploadResponse(result.FrameId, result.SequenceId, result.Index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tells every connected client about a card import (BRIEF §14.3 <c>cardEvent</c>), so one account cannot crowd their
|
||||
/// event queues: its progress goes out at most once a second (a report sooner is dropped, the next one replaces it as
|
||||
/// on the client), and at most 120 of its events a minute (429 beyond). False when the report was dropped.
|
||||
/// </summary>
|
||||
public Outcome<bool> PublishCard(Caller caller, CardEvent card)
|
||||
{
|
||||
var now = time.GetUtcNow();
|
||||
var sent = _cardEvents.GetOrAdd(caller.Id, _ => new CardEvents());
|
||||
lock (sent.Gate)
|
||||
{
|
||||
var progress = card.State == CardImportStates.Progress;
|
||||
if (progress && now - sent.LastProgress < CardProgressInterval) return false;
|
||||
while (sent.Recent.Count > 0 && now - sent.Recent.Peek() >= CardEventWindow) sent.Recent.Dequeue();
|
||||
if (sent.Recent.Count >= CardEventsPerMinute)
|
||||
{
|
||||
var wait = Math.Max(1, (int)Math.Ceiling((CardEventWindow - (now - sent.Recent.Peek())).TotalSeconds));
|
||||
return Fail<bool>(429, "rate_limited", wait.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
sent.Recent.Enqueue(now);
|
||||
if (progress) sent.LastProgress = now;
|
||||
}
|
||||
events.Publish(card);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a fully received upload to the archive staging folder and checks its SHA-256. On a mismatch the upload
|
||||
/// fails, its files are deleted and null is returned; the client starts over.
|
||||
@@ -256,4 +289,12 @@ public sealed class UploadService(
|
||||
}
|
||||
|
||||
private static Outcome<T> Fail<T>(int status, string code, string? details = null) => Outcome<T>.Fail(status, code, details);
|
||||
|
||||
/// <summary>The card events one account published lately.</summary>
|
||||
private sealed class CardEvents
|
||||
{
|
||||
public Lock Gate { get; } = new();
|
||||
public Queue<DateTimeOffset> Recent { get; } = new();
|
||||
public DateTimeOffset LastProgress { get; set; } = DateTimeOffset.MinValue;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user