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.method = sqlc.arg(method)
|
||||||
AND k.path = sqlc.arg(path)
|
AND k.path = sqlc.arg(path)
|
||||||
AND k.key = sqlc.arg(key);
|
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"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -63,14 +64,16 @@ func Run() error {
|
|||||||
log.Info("session store configured", "store", cfg.SessionStore)
|
log.Info("session store configured", "store", cfg.SessionStore)
|
||||||
|
|
||||||
cleanupCtx, stopCleanup := context.WithCancel(context.Background())
|
cleanupCtx, stopCleanup := context.WithCancel(context.Background())
|
||||||
cleanupDone := make(chan struct{})
|
var cleanupWG sync.WaitGroup
|
||||||
go func() {
|
cleanupWG.Go(func() {
|
||||||
defer close(cleanupDone)
|
|
||||||
runSessionCleanup(cleanupCtx, log, queries, sessionCleanupInterval)
|
runSessionCleanup(cleanupCtx, log, queries, sessionCleanupInterval)
|
||||||
}()
|
})
|
||||||
|
cleanupWG.Go(func() {
|
||||||
|
runIdempotencyCleanup(cleanupCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval)
|
||||||
|
})
|
||||||
defer func() {
|
defer func() {
|
||||||
stopCleanup()
|
stopCleanup()
|
||||||
<-cleanupDone
|
cleanupWG.Wait()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log)
|
authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log)
|
||||||
|
|||||||
@@ -7,11 +7,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const sessionCleanupInterval = time.Hour
|
const sessionCleanupInterval = time.Hour
|
||||||
|
const idempotencyCleanupInterval = time.Hour
|
||||||
|
|
||||||
type sessionCleaner interface {
|
type sessionCleaner interface {
|
||||||
DeleteExpiredSessions(ctx context.Context) (int64, error)
|
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) {
|
func runSessionCleanup(ctx context.Context, log *slog.Logger, q sessionCleaner, interval time.Duration) {
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
defer ticker.Stop()
|
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
|
Port string
|
||||||
MaxInflightRequests int
|
MaxInflightRequests int
|
||||||
SessionTTL time.Duration
|
SessionTTL time.Duration
|
||||||
|
IdempotencyTTL time.Duration
|
||||||
InstanceID string
|
InstanceID string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +63,7 @@ func Load() Config {
|
|||||||
Port: getEnv("APP_PORT", "8080"),
|
Port: getEnv("APP_PORT", "8080"),
|
||||||
MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0),
|
MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0),
|
||||||
SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second,
|
SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second,
|
||||||
|
IdempotencyTTL: time.Duration(getEnvPositiveInt("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second,
|
||||||
InstanceID: getInstanceID("INSTANCE_ID"),
|
InstanceID: getInstanceID("INSTANCE_ID"),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,19 @@ func (q *Queries) CreateIdempotencyKey(ctx context.Context, arg CreateIdempotenc
|
|||||||
return result.RowsAffected(), nil
|
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
|
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
|
||||||
SELECT
|
SELECT
|
||||||
k.request_hash,
|
k.request_hash,
|
||||||
|
|||||||
Reference in New Issue
Block a user