Stop consumers from committing past failed offsets, harden Rabbit/DataLake/gateway backpressure, dispose JsonDocument/MinIO correctly, and batch expensive DB/HTTP paths. Track progress in docs/performance.
56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using Confluent.Kafka;
|
|
|
|
/// <summary>
|
|
/// Shared transient-failure handling for Kafka consumers that use
|
|
/// <see cref="PoisonPillGuard"/>. After <c>Consume</c>, the client position
|
|
/// advances; without <c>Seek</c>, the next successful <c>Commit</c> would skip
|
|
/// the failed offset. This helper seeks back before delaying so retries truly
|
|
/// reprocess the same message.
|
|
/// </summary>
|
|
public static class KafkaConsumerRetry
|
|
{
|
|
public static Task HandleFailureAsync(
|
|
IConsumer<string, string> consumer,
|
|
ConsumeResult<string, string>? 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);
|
|
|
|
/// <summary>
|
|
/// Testable overload — production code should prefer the <see cref="IConsumer{TKey,TValue}"/> overload.
|
|
/// </summary>
|
|
public static async Task HandleFailureAsync(
|
|
Action<TopicPartitionOffset> seek,
|
|
Action<ConsumeResult<string, string>> commit,
|
|
ConsumeResult<string, string>? 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);
|
|
}
|
|
}
|