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