fix(discord): add backoff to scheduler to prevent 403 spam
- SessionSchedulerService now backs off for 15 minutes after any handler failure (confirmation, one-hour reminder, join link), preventing infinite retry loops on Discord 403 Missing Access. - Added per-session ConcurrentDictionary backoff tracking with automatic cleanup on success. - Enhanced DiscordPlatformMessenger logging for SendConfirmation and SendJoinLink to aid permission diagnostics. - Added 3 regression tests for backoff behavior. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using GmRelay.Shared.Features.Confirmation.SendConfirmation;
|
||||
using GmRelay.Shared.Features.Reminders.SendJoinLink;
|
||||
using GmRelay.Shared.Features.Reminders.SendOneHourReminder;
|
||||
@@ -20,6 +21,11 @@ public sealed class SessionSchedulerService(
|
||||
ILogger<SessionSchedulerService> logger) : BackgroundService
|
||||
{
|
||||
private static readonly TimeSpan TickInterval = TimeSpan.FromMinutes(1);
|
||||
private static readonly TimeSpan BackoffDuration = TimeSpan.FromMinutes(15);
|
||||
|
||||
private readonly ConcurrentDictionary<Guid, DateTimeOffset> _confirmationBackoff = new();
|
||||
private readonly ConcurrentDictionary<Guid, DateTimeOffset> _oneHourBackoff = new();
|
||||
private readonly ConcurrentDictionary<Guid, DateTimeOffset> _joinLinkBackoff = new();
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
@@ -71,14 +77,30 @@ public sealed class SessionSchedulerService(
|
||||
|
||||
foreach (var sessionId in sessionIds)
|
||||
{
|
||||
if (_confirmationBackoff.TryGetValue(sessionId, out var backoffUntil) && backoffUntil > now)
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Skipping confirmation for session {SessionId} until {Backoff}",
|
||||
sessionId,
|
||||
backoffUntil);
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await confirmationHandler.HandleAsync(sessionId, ct);
|
||||
_confirmationBackoff.TryRemove(sessionId, out _);
|
||||
logger.LogInformation("Confirmation sent for session {SessionId}", sessionId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to send confirmation for session {SessionId}", sessionId);
|
||||
var nextAttempt = now.Add(BackoffDuration);
|
||||
_confirmationBackoff[sessionId] = nextAttempt;
|
||||
logger.LogError(
|
||||
ex,
|
||||
"Failed to send confirmation for session {SessionId}, backing off until {Backoff}",
|
||||
sessionId,
|
||||
nextAttempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,14 +120,30 @@ public sealed class SessionSchedulerService(
|
||||
|
||||
foreach (var sessionId in sessionIds)
|
||||
{
|
||||
if (_oneHourBackoff.TryGetValue(sessionId, out var backoffUntil) && backoffUntil > now)
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Skipping one-hour reminder for session {SessionId} until {Backoff}",
|
||||
sessionId,
|
||||
backoffUntil);
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await oneHourReminderHandler.HandleAsync(sessionId, ct);
|
||||
_oneHourBackoff.TryRemove(sessionId, out _);
|
||||
logger.LogInformation("One-hour reminder processed for session {SessionId}", sessionId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to process one-hour reminder for session {SessionId}", sessionId);
|
||||
var nextAttempt = now.Add(BackoffDuration);
|
||||
_oneHourBackoff[sessionId] = nextAttempt;
|
||||
logger.LogError(
|
||||
ex,
|
||||
"Failed to process one-hour reminder for session {SessionId}, backing off until {Backoff}",
|
||||
sessionId,
|
||||
nextAttempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,14 +163,30 @@ public sealed class SessionSchedulerService(
|
||||
|
||||
foreach (var sessionId in sessionIds)
|
||||
{
|
||||
if (_joinLinkBackoff.TryGetValue(sessionId, out var backoffUntil) && backoffUntil > now)
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Skipping join link for session {SessionId} until {Backoff}",
|
||||
sessionId,
|
||||
backoffUntil);
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await joinLinkHandler.HandleAsync(sessionId, ct);
|
||||
_joinLinkBackoff.TryRemove(sessionId, out _);
|
||||
logger.LogInformation("Join link sent for session {SessionId}", sessionId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to send join link for session {SessionId}", sessionId);
|
||||
var nextAttempt = now.Add(BackoffDuration);
|
||||
_joinLinkBackoff[sessionId] = nextAttempt;
|
||||
logger.LogError(
|
||||
ex,
|
||||
"Failed to send join link for session {SessionId}, backing off until {Backoff}",
|
||||
sessionId,
|
||||
nextAttempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user