Сделать отправку сообщения идемпотентной по Idempotency-Key с общей таблицей ключей и их чисткой

This commit is contained in:
2026-11-02 17:11:04 +03:00
parent 7fa8881eb3
commit 951fd0d517
19 changed files with 856 additions and 40 deletions
+33
View File
@@ -0,0 +1,33 @@
package chat
import (
"context"
"log/slog"
"time"
)
// RunKeyCleanup deletes expired Idempotency-Keys every period until ctx is
// cancelled. Every replica runs it: the deletes skip rows another replica or
// a request holds, so concurrent runs share the work instead of waiting, and
// a failed run is simply retried at the next tick.
func RunKeyCleanup(ctx context.Context, log *slog.Logger, svc *Service, every time.Duration) {
ticker := time.NewTicker(every)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
deleted, err := svc.PurgeExpiredKeys(ctx)
if err != nil {
if ctx.Err() == nil {
log.Warn("idempotency key cleanup failed", "deleted", deleted, "error", err.Error())
}
continue
}
if deleted > 0 {
log.Info("idempotency keys purged", "deleted", deleted)
}
}
}