fix: Kafka consumer poison pill causes infinite retry

This commit is contained in:
voltsrage
2026-06-21 17:36:00 +08:00
parent 0d0bba19e6
commit 2d22ff06b6
10 changed files with 175 additions and 10 deletions
@@ -33,6 +33,8 @@ public class WarningAlertService : BackgroundService
_logger.LogInformation("WarningAlertService started — consumer group: warning-evaluator");
var guard = new PoisonPillGuard("warning-evaluator", _kafkaOptions.MaxPoisonRetries, _logger);
try
{
while (!stoppingToken.IsCancellationRequested)
@@ -58,6 +60,7 @@ public class WarningAlertService : BackgroundService
stoppingToken);
consumer.Commit(result);
guard.OnSuccess();
}
catch (OperationCanceledException)
{
@@ -65,8 +68,14 @@ public class WarningAlertService : BackgroundService
}
catch (Exception ex)
{
if (result is not null && guard.ShouldSkip(result, ex))
{
consumer.Commit(result);
continue;
}
_logger.LogError(ex,
"WarningAlertService failed on topic={Topic} offset={Offset} — not committing",
"WarningAlertService failed on topic={Topic} offset={Offset} — will retry",
result?.Topic, result?.Offset.Value);
await Task.Delay(2000, stoppingToken);
}