[backend/federation] Skip dead instances in deliver queue

This commit is contained in:
Laura Hausmann 2024-02-26 22:32:28 +01:00
parent 01b7135208
commit 489941adcc
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 34 additions and 9 deletions

View file

@ -37,4 +37,12 @@ public class FederationControlService(
return await db.BlockedInstances.AnyAsync(p => finalHosts.Any(host => host == p.Host || return await db.BlockedInstances.AnyAsync(p => finalHosts.Any(host => host == p.Host ||
host.EndsWith("." + p.Host))); host.EndsWith("." + p.Host)));
} }
public async Task<bool> ShouldSkipAsync(string host)
{
return await db.Instances.AnyAsync(p => p.Host == host.ToLowerInvariant() &&
((p.IsNotResponding &&
p.LastCommunicatedAt < DateTime.UtcNow - TimeSpan.FromDays(7)) ||
p.IsSuspended));
}
} }

View file

@ -34,6 +34,12 @@ public class DeliverQueue
return; return;
} }
if (await fedCtrl.ShouldSkipAsync(job.RecipientHost))
{
logger.LogDebug("fedCtrl.ShouldSkipAsync returned true, skipping");
return;
}
logger.LogDebug("Delivering activity to: {uri}", job.InboxUrl); logger.LogDebug("Delivering activity to: {uri}", job.InboxUrl);
var key = await cache.FetchAsync($"userPrivateKey:{job.UserId}", TimeSpan.FromMinutes(60), async () => var key = await cache.FetchAsync($"userPrivateKey:{job.UserId}", TimeSpan.FromMinutes(60), async () =>
@ -53,8 +59,8 @@ public class DeliverQueue
_ = followup.ExecuteTask("UpdateInstanceMetadata", async provider => _ = followup.ExecuteTask("UpdateInstanceMetadata", async provider =>
{ {
var instanceSvc = provider.GetRequiredService<InstanceService>(); var instanceSvc = provider.GetRequiredService<InstanceService>();
var webDomain = new Uri(job.InboxUrl).Host; await instanceSvc.UpdateInstanceStatusAsync(job.RecipientHost, new Uri(job.InboxUrl).Host,
await instanceSvc.UpdateInstanceStatusAsync(job.RecipientHost, webDomain, (int)response.StatusCode); (int)response.StatusCode, response.IsSuccessStatusCode);
}); });
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();

View file

@ -78,22 +78,33 @@ public class InstanceService(DatabaseContext db, HttpClient httpClient)
} }
} }
public async Task UpdateInstanceStatusAsync(string host, string webDomain, int? statusCode = null) public async Task UpdateInstanceStatusAsync(string host, string webDomain)
{ {
var instance = await GetUpdatedInstanceMetadataAsync(host, webDomain); var instance = await GetUpdatedInstanceMetadataAsync(host, webDomain);
if (statusCode != null) instance.LastCommunicatedAt = DateTime.UtcNow;
instance.LatestRequestReceivedAt = DateTime.UtcNow;
await db.SaveChangesAsync();
}
public async Task UpdateInstanceStatusAsync(string host, string webDomain, int statusCode, bool notResponding)
{
var instance = await GetUpdatedInstanceMetadataAsync(host, webDomain);
instance.LatestStatus = statusCode;
instance.LatestRequestSentAt = DateTime.UtcNow;
if (notResponding)
{ {
instance.LatestStatus = statusCode; instance.IsNotResponding = true;
instance.LatestRequestSentAt = DateTime.UtcNow;
} }
else else
{ {
instance.LatestRequestReceivedAt = DateTime.UtcNow; instance.IsNotResponding = false;
instance.LastCommunicatedAt = DateTime.UtcNow;
} }
instance.LastCommunicatedAt = DateTime.UtcNow;
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }
} }