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
@@ -54,6 +54,8 @@ public class EsIndexerService : BackgroundService
_logger.LogInformation("EsIndexerService started. Subscribed to 5 topics.");
var guard = new PoisonPillGuard("es-indexer", _kafkaOptions.MaxPoisonRetries, _logger);
try
{
while (!stoppingToken.IsCancellationRequested)
@@ -64,6 +66,7 @@ public class EsIndexerService : BackgroundService
result = consumer.Consume(stoppingToken);
await DispatchAsync(result.Topic, result.Message.Value, stoppingToken);
consumer.Commit(result);
guard.OnSuccess();
}
catch (OperationCanceledException)
{
@@ -71,10 +74,15 @@ public class EsIndexerService : BackgroundService
}
catch (Exception ex)
{
if (result is not null && guard.ShouldSkip(result, ex))
{
consumer.Commit(result);
continue;
}
_logger.LogError(ex,
"EsIndexer failed processing topic={Topic} offset={Offset} — not committing",
"EsIndexer failed processing topic={Topic} offset={Offset} — will retry",
result?.Topic, result?.Offset.Value);
// Do not commit: message will be redelivered on restart
await Task.Delay(1000, stoppingToken);
}
}