using Confluent.Kafka; /// /// Shared transient-failure handling for Kafka consumers that use /// . After Consume, the client position /// advances; without Seek, the next successful Commit would skip /// the failed offset. This helper seeks back before delaying so retries truly /// reprocess the same message. /// public static class KafkaConsumerRetry { public static Task HandleFailureAsync( IConsumer consumer, ConsumeResult? result, Exception ex, PoisonPillGuard guard, ILogger logger, string serviceName, CancellationToken ct, int delayMs = 2000) => HandleFailureAsync( seek: tpo => consumer.Seek(tpo), commit: r => consumer.Commit(r), result, ex, guard, logger, serviceName, ct, delayMs); /// /// Testable overload — production code should prefer the overload. /// public static async Task HandleFailureAsync( Action seek, Action> commit, ConsumeResult? result, Exception ex, PoisonPillGuard guard, ILogger logger, string serviceName, CancellationToken ct, int delayMs = 2000) { if (result is not null && guard.ShouldSkip(result, ex)) { commit(result); return; } logger.LogError(ex, "{Service} failed on topic={Topic} offset={Offset} — will retry", serviceName, result?.Topic, result?.Offset.Value); if (result is not null) seek(result.TopicPartitionOffset); await Task.Delay(delayMs, ct); } }