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
@@ -1,4 +1,5 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
public static class BufferedSyncWriter
{
@@ -8,8 +9,21 @@ public static class BufferedSyncWriter
object payload,
string idempotencyKey,
Guid encounterId,
DateTimeOffset recordedAt)
DateTimeOffset recordedAt,
int maxUnsyncedItems = 50_000)
{
var persisted = db.BufferedSyncItems.Count(b => !b.Synced);
var pendingAdd = db.ChangeTracker.Entries<BufferedSyncItem>()
.Count(e => e.State == EntityState.Added);
if (persisted + pendingAdd >= maxUnsyncedItems)
{
throw new ConflictException(
$"Sync buffer is full ({maxUnsyncedItems} unsynced items). " +
"Central sync must catch up before accepting more buffered events.",
"SYNC_BUFFER_FULL");
}
db.BufferedSyncItems.Add(new BufferedSyncItem
{
Id = Guid.NewGuid(),
@@ -21,4 +35,4 @@ public static class BufferedSyncWriter
CreatedAt = DateTimeOffset.UtcNow
});
}
}
}
@@ -1,10 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
public class LocalAlertService : ILocalAlertService
{
private readonly GatewayDbContext _db;
private readonly GatewayOptions _gateway;
public LocalAlertService(GatewayDbContext db) => _db = db;
public LocalAlertService(GatewayDbContext db, IOptions<GatewayOptions> gateway)
{
_db = db;
_gateway = gateway.Value;
}
public async Task<LocalClinicalAlert> AcknowledgeAsync(
Guid id, AcknowledgeAlertRequest req, string clinicianId)
@@ -26,7 +32,8 @@ public class LocalAlertService : ILocalAlertService
clinicianId,
acknowledgedAt = alert.AcknowledgedAt,
req.Note
}, $"ack:{alert.ClientAlertId}:{alert.AcknowledgedAt:o}", alert.EncounterId, alert.AcknowledgedAt!.Value);
}, $"ack:{alert.ClientAlertId}:{alert.AcknowledgedAt:o}", alert.EncounterId, alert.AcknowledgedAt!.Value,
_gateway.MaxBufferedSyncItems);
await _db.SaveChangesAsync();
return alert;
@@ -50,7 +57,8 @@ public class LocalAlertService : ILocalAlertService
clinicianId = alert.AcknowledgedBy,
resolvedAt = alert.ResolvedAt,
note = (string?)null
}, $"resolve:{alert.ClientAlertId}", alert.EncounterId, alert.ResolvedAt!.Value);
}, $"resolve:{alert.ClientAlertId}", alert.EncounterId, alert.ResolvedAt!.Value,
_gateway.MaxBufferedSyncItems);
await _db.SaveChangesAsync();
return alert;
@@ -1,5 +1,6 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
public class LocalObservationService : ILocalObservationService
@@ -8,6 +9,7 @@ public class LocalObservationService : ILocalObservationService
private readonly IConnectionMultiplexer _redis;
private readonly LocalWarningEvaluator _warnings;
private readonly LocalPagingPublisher _paging;
private readonly GatewayOptions _gateway;
private readonly ILogger<LocalObservationService> _logger;
public LocalObservationService(
@@ -15,12 +17,14 @@ public class LocalObservationService : ILocalObservationService
IConnectionMultiplexer redis,
LocalWarningEvaluator warnings,
LocalPagingPublisher paging,
IOptions<GatewayOptions> gateway,
ILogger<LocalObservationService> logger)
{
_db = db;
_redis = redis;
_warnings = warnings;
_paging = paging;
_gateway = gateway.Value;
_logger = logger;
}
@@ -94,7 +98,8 @@ public class LocalObservationService : ILocalObservationService
severity = alert.Severity.ToDbString(),
alert.Details,
generatedAt = triggeredAt
}, $"alert:{alert.ClientAlertId}", encounterId, triggeredAt);
}, $"alert:{alert.ClientAlertId}", encounterId, triggeredAt,
_gateway.MaxBufferedSyncItems);
await _paging.PublishCriticalAsync(alert, encounter);
}
@@ -114,7 +119,8 @@ public class LocalObservationService : ILocalObservationService
req.Unit,
source = req.Source.ToDbString(),
req.RecordedAt
}, req.IdempotencyKey ?? $"obs:{observation.Id}", encounterId, req.RecordedAt);
}, req.IdempotencyKey ?? $"obs:{observation.Id}", encounterId, req.RecordedAt,
_gateway.MaxBufferedSyncItems);
await _db.SaveChangesAsync();
await tx.CommitAsync();
@@ -1,13 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
public class LocalWarningEvaluator
{
private readonly GatewayDbContext _db;
private readonly GatewayOptions _gateway;
private readonly ILogger<LocalWarningEvaluator> _logger;
public LocalWarningEvaluator(GatewayDbContext db, ILogger<LocalWarningEvaluator> logger)
public LocalWarningEvaluator(
GatewayDbContext db,
IOptions<GatewayOptions> gateway,
ILogger<LocalWarningEvaluator> logger)
{
_db = db;
_gateway = gateway.Value;
_logger = logger;
}
@@ -52,7 +58,8 @@ public class LocalWarningEvaluator
severity = AlertSeverity.Warning.ToDbString(),
details,
generatedAt = triggeredAt
}, $"alert:{clientAlertId}", encounterId, triggeredAt);
}, $"alert:{clientAlertId}", encounterId, triggeredAt,
_gateway.MaxBufferedSyncItems);
_logger.LogInformation(
"WARNING alert {AlertId} created locally for encounter {EncounterId}",