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.
39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
public static class BufferedSyncWriter
|
|
{
|
|
public static void Enqueue(
|
|
GatewayDbContext db,
|
|
BufferedSyncItemType itemType,
|
|
object payload,
|
|
string idempotencyKey,
|
|
Guid encounterId,
|
|
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(),
|
|
ItemType = itemType,
|
|
Payload = JsonSerializer.Serialize(payload),
|
|
IdempotencyKey = idempotencyKey,
|
|
EncounterId = encounterId,
|
|
RecordedAt = recordedAt,
|
|
CreatedAt = DateTimeOffset.UtcNow
|
|
});
|
|
}
|
|
}
|