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 News2ScoringService : BackgroundService
_logger.LogInformation("News2ScoringService started — consumer group: news2-scoring");
var guard = new PoisonPillGuard("news2-scoring", _kafkaOptions.MaxPoisonRetries, _logger);
try
{
while (!stoppingToken.IsCancellationRequested)
@@ -62,6 +64,7 @@ public class News2ScoringService : BackgroundService
evt.EncounterId, outcome.TotalScore, outcome.RiskLevel);
consumer.Commit(result);
guard.OnSuccess();
}
catch (OperationCanceledException)
{
@@ -69,8 +72,14 @@ public class News2ScoringService : BackgroundService
}
catch (Exception ex)
{
if (result is not null && guard.ShouldSkip(result, ex))
{
consumer.Commit(result);
continue;
}
_logger.LogError(ex,
"News2ScoringService failed on topic={Topic} offset={Offset} — not committing",
"News2ScoringService failed on topic={Topic} offset={Offset} — will retry",
result?.Topic, result?.Offset.Value);
await Task.Delay(2000, stoppingToken);
}