Сделать отправку сообщения идемпотентной по Idempotency-Key с общей таблицей ключей и их чисткой
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user