Fix Kafka retry seek, bound buffers, and cut hot-path CPU/memory waste (Phases 41–44).
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.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using System.Text.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Shared JSON options for Kafka clinical event deserialization.
|
||||
/// <see cref="JsonSerializerOptions"/> construction is expensive — reuse one instance.
|
||||
/// </summary>
|
||||
public static class ClinicalEventJson
|
||||
{
|
||||
public static readonly JsonSerializerOptions Options = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user