Fix Kafka retry seek, bound buffers, and cut hot-path CPU/memory waste (Phases 41–44).
CI / changes (push) Successful in 17s
CI / frontend (push) Skipped
CI / backend (push) Successful in 15m10s

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:
2026-11-30 21:32:43 +08:00
parent 1c1065938f
commit 68a2ab3204
54 changed files with 959 additions and 311 deletions
@@ -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);
}
}