forked from mtusi2026/mtusi2026lab3
feat(idempotency): purge expired keys
This commit is contained in:
@@ -30,3 +30,7 @@ WHERE k.user_id = sqlc.arg(user_id)
|
||||
AND k.method = sqlc.arg(method)
|
||||
AND k.path = sqlc.arg(path)
|
||||
AND k.key = sqlc.arg(key);
|
||||
|
||||
-- name: DeleteExpiredIdempotencyKeys :execrows
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE created_at < sqlc.arg(cutoff);
|
||||
|
||||
+8
-5
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -63,14 +64,16 @@ func Run() error {
|
||||
log.Info("session store configured", "store", cfg.SessionStore)
|
||||
|
||||
cleanupCtx, stopCleanup := context.WithCancel(context.Background())
|
||||
cleanupDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(cleanupDone)
|
||||
var cleanupWG sync.WaitGroup
|
||||
cleanupWG.Go(func() {
|
||||
runSessionCleanup(cleanupCtx, log, queries, sessionCleanupInterval)
|
||||
}()
|
||||
})
|
||||
cleanupWG.Go(func() {
|
||||
runIdempotencyCleanup(cleanupCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval)
|
||||
})
|
||||
defer func() {
|
||||
stopCleanup()
|
||||
<-cleanupDone
|
||||
cleanupWG.Wait()
|
||||
}()
|
||||
|
||||
authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log)
|
||||
|
||||
@@ -7,11 +7,16 @@ import (
|
||||
)
|
||||
|
||||
const sessionCleanupInterval = time.Hour
|
||||
const idempotencyCleanupInterval = time.Hour
|
||||
|
||||
type sessionCleaner interface {
|
||||
DeleteExpiredSessions(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
type idempotencyCleaner interface {
|
||||
DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error)
|
||||
}
|
||||
|
||||
func runSessionCleanup(ctx context.Context, log *slog.Logger, q sessionCleaner, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
@@ -34,3 +39,32 @@ func runSessionCleanup(ctx context.Context, log *slog.Logger, q sessionCleaner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runIdempotencyCleanup(
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
q idempotencyCleaner,
|
||||
ttl time.Duration,
|
||||
interval time.Duration,
|
||||
) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
n, err := q.DeleteExpiredIdempotencyKeys(ctx, time.Now().Add(-ttl))
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
log.Error("delete expired idempotency keys", "error", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if n > 0 {
|
||||
log.Info("expired idempotency keys deleted", "count", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ type ServerConfig struct {
|
||||
Port string
|
||||
MaxInflightRequests int
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
InstanceID string
|
||||
}
|
||||
|
||||
@@ -62,6 +63,7 @@ func Load() Config {
|
||||
Port: getEnv("APP_PORT", "8080"),
|
||||
MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0),
|
||||
SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second,
|
||||
IdempotencyTTL: time.Duration(getEnvPositiveInt("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second,
|
||||
InstanceID: getInstanceID("INSTANCE_ID"),
|
||||
},
|
||||
|
||||
|
||||
@@ -55,6 +55,19 @@ func (q *Queries) CreateIdempotencyKey(ctx context.Context, arg CreateIdempotenc
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteExpiredIdempotencyKeys = `-- name: DeleteExpiredIdempotencyKeys :execrows
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE created_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteExpiredIdempotencyKeys, cutoff)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
|
||||
SELECT
|
||||
k.request_hash,
|
||||
|
||||
Reference in New Issue
Block a user