[backend] Fix reply backfill limiting

This commit is contained in:
Kopper 2024-10-26 03:50:32 +03:00 committed by Iceshrimp development
parent 330de26346
commit 90f1e30ef2
2 changed files with 31 additions and 12 deletions

View file

@ -72,7 +72,12 @@ public class BackfillQueue(int parallelism)
continue; continue;
} }
logger.LogTrace("Backfilling {collection} (remaining limit {limit})", current.RepliesCollection, backfillLimit); if (--backfillLimit <= 0)
{
logger.LogDebug("Reached backfill limit");
break;
}
logger.LogTrace("Backfilling collection {collection} (remaining limit {limit})", current.RepliesCollection, backfillLimit);
await db.Notes await db.Notes
.Where(n => n.Id == current.Id) .Where(n => n.Id == current.Id)
@ -83,22 +88,34 @@ public class BackfillQueue(int parallelism)
.Where(p => p.Id != null) .Where(p => p.Id != null)
.WithCancellation(token)) .WithCancellation(token))
{ {
if (--backfillLimit <= 0) logger.LogTrace("Backfilling note {note} (remaining limit {limit})", asNote.Id, backfillLimit);
try
{
var note = await noteSvc.ResolveNoteAsync(asNote.Id!, asNote as ASNote, user, clearHistory: true,
forceRefresh: false);
backfillLimit -= Math.Max(noteSvc.NotesFetched, 1);
if (backfillLimit <= 0)
{ {
logger.LogDebug("Reached backfill limit"); logger.LogDebug("Reached backfill limit");
toBackfill.Clear(); toBackfill.Clear();
break; break;
} }
var note = await noteSvc.ResolveNoteAsync(asNote.Id!, asNote as ASNote, user, clearHistory: true, forceRefresh: false); if (note is { UserHost: not null, RepliesCollection: not null, RepliesCount: < MaxRepliesPerNote } &&
note.CreatedAt <= DateTime.UtcNow - cfg.NewNoteDelayTimeSpan &&
if (note is { UserHost: not null, RepliesCollection: not null, RepliesCount: < MaxRepliesPerNote } (note.RepliesFetchedAt == null ||
&& note.CreatedAt <= DateTime.UtcNow - cfg.NewNoteDelayTimeSpan note.RepliesFetchedAt <= DateTime.UtcNow - cfg.RefreshAfterTimeSpan))
&& (note.RepliesFetchedAt == null || note.RepliesFetchedAt <= DateTime.UtcNow - cfg.RefreshAfterTimeSpan))
{ {
toBackfill.Enqueue(new BackfillData(note.Id, note.RepliesCollection!)); toBackfill.Enqueue(new BackfillData(note.Id, note.RepliesCollection!));
} }
} }
catch (Exception e)
{
logger.LogWarning(e, "Failed to backfill {note}", asNote.Id);
}
}
} }
await db.NoteThreads await db.NoteThreads

View file

@ -55,6 +55,8 @@ public class NoteService(
private readonly List<string> _resolverHistory = []; private readonly List<string> _resolverHistory = [];
private int _recursionLimit = DefaultRecursionLimit; private int _recursionLimit = DefaultRecursionLimit;
internal int NotesFetched => DefaultRecursionLimit - _recursionLimit;
public class NoteCreationData public class NoteCreationData
{ {