House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -25,24 +25,17 @@ namespace PrivaPub.Federation.Outbox
|
||||
{
|
||||
// again: a delivery made once more on purpose, which an earlier one of the same activity does not stop; deliveries
|
||||
// with the same again are made once
|
||||
Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, CancellationToken token, string again = default);
|
||||
Task EnqueueToFollowers(LocalActor signer, JsonObject activity, CancellationToken token, IEnumerable<string> extraInboxes = default);
|
||||
Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, string again = default, CancellationToken token = default);
|
||||
Task EnqueueToFollowers(LocalActor signer, JsonObject activity, IEnumerable<string> extraInboxes = default, CancellationToken token = default);
|
||||
Task<IReadOnlyList<string>> FollowerInboxes(LocalActor actor, CancellationToken token);
|
||||
}
|
||||
|
||||
public sealed record DeliveryPayload(string SignerId, LocalActorKind SignerKind, string Inbox, string Body);
|
||||
|
||||
public class DeliveryService : IDeliveryService
|
||||
public class DeliveryService(
|
||||
DbEntities dbEntities,
|
||||
IJobQueue queue) : IDeliveryService
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IJobQueue _queue;
|
||||
|
||||
public DeliveryService(DbEntities dbEntities, IJobQueue queue)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_queue = queue;
|
||||
}
|
||||
|
||||
// what the staff's moderation holds back (completion program R6): a suspended persona (by itself or with its banned
|
||||
// login) delivers nothing, but the Delete of itself, which its deletion sends; and nothing goes to the own inbox of an
|
||||
// account elsewhere the staff suspended (its server's shared inbox still serves the others there)
|
||||
@@ -52,7 +45,7 @@ namespace PrivaPub.Federation.Outbox
|
||||
static bool DeletesItself(LocalActor signer, JsonNode activity) =>
|
||||
ActivityJson.Value(activity, "type") == "Delete" && ActivityJson.Id(activity?["object"]) == signer.Uri;
|
||||
|
||||
public async Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, CancellationToken token, string again = default)
|
||||
public async Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, string again = default, CancellationToken token = default)
|
||||
{
|
||||
if (HeldBack(signer, activity))
|
||||
return;
|
||||
@@ -90,18 +83,18 @@ namespace PrivaPub.Federation.Outbox
|
||||
})
|
||||
.ToList();
|
||||
if (jobs.Count > 0)
|
||||
await _queue.EnqueueMany(jobs, token);
|
||||
await queue.EnqueueMany(jobs, token);
|
||||
}
|
||||
|
||||
public async Task EnqueueToFollowers(LocalActor signer, JsonObject activity, CancellationToken token, IEnumerable<string> extraInboxes = default)
|
||||
public async Task EnqueueToFollowers(LocalActor signer, JsonObject activity, IEnumerable<string> extraInboxes = default, CancellationToken token = default)
|
||||
{
|
||||
var inboxes = (await FollowerInboxes(signer, token)).Concat(extraInboxes ?? Enumerable.Empty<string>());
|
||||
await Enqueue(signer, inboxes, activity, token);
|
||||
await Enqueue(signer, inboxes, activity, token: token);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<string>> FollowerInboxes(LocalActor actor, CancellationToken token)
|
||||
{
|
||||
var followers = await _dbEntities.Followers
|
||||
var followers = await dbEntities.Followers
|
||||
.Match(f => f.LocalActorId == actor.Id && f.LocalActorKind == actor.Kind && f.IsAccepted)
|
||||
.ExecuteAsync(token);
|
||||
return followers.Select(f => string.IsNullOrEmpty(f.SharedInboxURL) ? f.InboxURL : f.SharedInboxURL)
|
||||
@@ -110,26 +103,16 @@ namespace PrivaPub.Federation.Outbox
|
||||
}
|
||||
}
|
||||
|
||||
public class DeliveryJobHandler : IJobHandler
|
||||
public class DeliveryJobHandler(
|
||||
ILocalActorService actors,
|
||||
IFederationHttp http,
|
||||
IHostCircuitBreaker breaker,
|
||||
ILogger<DeliveryJobHandler> logger,
|
||||
IInteractionLedger ledger = default,
|
||||
Microsoft.Extensions.Options.IOptionsMonitor<Infrastructure.Http.FederationOptions> federation = default) : IJobHandler
|
||||
{
|
||||
readonly ILocalActorService _actors;
|
||||
readonly IFederationHttp _http;
|
||||
readonly IHostCircuitBreaker _breaker;
|
||||
readonly ILogger<DeliveryJobHandler> _logger;
|
||||
readonly IInteractionLedger _ledger;
|
||||
readonly int _concurrency = Math.Max(1, federation?.CurrentValue.DeliveryConcurrency ?? 8);
|
||||
|
||||
readonly int _concurrency;
|
||||
|
||||
public DeliveryJobHandler(ILocalActorService actors, IFederationHttp http, IHostCircuitBreaker breaker, ILogger<DeliveryJobHandler> logger,
|
||||
IInteractionLedger ledger = default, Microsoft.Extensions.Options.IOptions<Infrastructure.Http.FederationOptions> federation = default)
|
||||
{
|
||||
_concurrency = Math.Max(1, federation?.Value.DeliveryConcurrency ?? 8);
|
||||
_actors = actors;
|
||||
_http = http;
|
||||
_breaker = breaker;
|
||||
_logger = logger;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
sealed class Attempt
|
||||
{
|
||||
@@ -162,10 +145,10 @@ namespace PrivaPub.Federation.Outbox
|
||||
|
||||
void Record(Job job, Attempt attempt, JobOutcome outcome)
|
||||
{
|
||||
if (_ledger == default)
|
||||
if (ledger == default)
|
||||
return;
|
||||
var shape = attempt.Payload == default ? default : ActivityShape.Of(attempt.Payload.Body);
|
||||
_ledger.Record(new InteractionEvent
|
||||
ledger.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.Out,
|
||||
Host = job.Host,
|
||||
@@ -215,20 +198,20 @@ namespace PrivaPub.Federation.Outbox
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<DeliveryPayload>(job.Payload);
|
||||
attempt.Payload = payload;
|
||||
if (payload == default || !Uri.TryCreate(payload.Inbox, UriKind.Absolute, out var inbox) || !_http.IsAllowed(inbox))
|
||||
if (payload == default || !Uri.TryCreate(payload.Inbox, UriKind.Absolute, out var inbox) || !http.IsAllowed(inbox))
|
||||
{
|
||||
attempt.Reason = "not-deliverable";
|
||||
return JobOutcome.Dead("not a deliverable inbox");
|
||||
}
|
||||
|
||||
var unavailableUntil = await _breaker.UnavailableUntil(job.Host, token);
|
||||
var unavailableUntil = await breaker.UnavailableUntil(job.Host, token);
|
||||
if (unavailableUntil.HasValue)
|
||||
{
|
||||
attempt.Reason = "host-unavailable";
|
||||
return JobOutcome.Defer(unavailableUntil.Value, "the host is unavailable");
|
||||
}
|
||||
|
||||
var signer = await _actors.FindById(payload.SignerKind, payload.SignerId, token);
|
||||
var signer = await actors.FindById(payload.SignerKind, payload.SignerId, token);
|
||||
attempt.Signer = signer;
|
||||
if (signer == default)
|
||||
{
|
||||
@@ -255,14 +238,14 @@ namespace PrivaPub.Federation.Outbox
|
||||
var started = Stopwatch.GetTimestamp();
|
||||
try
|
||||
{
|
||||
using var response = await _http.Send(request, token);
|
||||
using var response = await http.Send(request, token);
|
||||
attempt.LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds;
|
||||
var status = (int)response.StatusCode;
|
||||
attempt.Status = status;
|
||||
attempt.Reason = response.IsSuccessStatusCode ? default : status.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
await _breaker.Succeeded(job.Host, token);
|
||||
await breaker.Succeeded(job.Host, token);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
if (response.StatusCode == HttpStatusCode.TooManyRequests
|
||||
@@ -280,15 +263,15 @@ namespace PrivaPub.Federation.Outbox
|
||||
return JobOutcome.Retry($"{status} {response.ReasonPhrase}");
|
||||
if (status is >= 400 and < 500 && status != 408)
|
||||
{
|
||||
await _breaker.Succeeded(job.Host, token);
|
||||
_logger.LogInformation("Delivery to {Inbox} refused with {Status}", payload.Inbox, status);
|
||||
await breaker.Succeeded(job.Host, token);
|
||||
logger.LogInformation("Delivery to {Inbox} refused with {Status}", payload.Inbox, status);
|
||||
return JobOutcome.Dead($"{status} {response.ReasonPhrase}");
|
||||
}
|
||||
// a gateway's 502, 503 or 504 says the server behind it is down, which counts against the host; any other 5xx
|
||||
// says the server is up and failed on this activity (Smithereen's 500 on a document its JSON-LD reader
|
||||
// refused), retried on its own while the host's other deliveries go on
|
||||
if (status is 502 or 503 or 504)
|
||||
await _breaker.Failed(job.Host, $"{status}", token);
|
||||
await breaker.Failed(job.Host, $"{status}", token);
|
||||
return JobOutcome.Retry($"{status} {response.ReasonPhrase}");
|
||||
}
|
||||
catch (BlockedDestinationException ex)
|
||||
@@ -300,7 +283,7 @@ namespace PrivaPub.Federation.Outbox
|
||||
{
|
||||
attempt.LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds;
|
||||
attempt.Reason = ex is TaskCanceledException ? "timeout" : "network";
|
||||
await _breaker.Failed(job.Host, ex.GetType().Name, token);
|
||||
await breaker.Failed(job.Host, ex.GetType().Name, token);
|
||||
return JobOutcome.Retry(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user