package jwtauth import ( "context" "log/slog" "time" "commilitia.net/cdrop/internal/db" ) // DeviceSweepInterval is how often the device sweeper wakes up. const DeviceSweepInterval = 1 * time.Hour // RunDeviceSweeper deletes devices whose last_seen is older than ttl. // Per user requirement: device registration must not outlive the longest // valid refresh_token (brief §2: 196h sliding window). Anything ≤ TTL is fine; // 0 disables the sweeper. func RunDeviceSweeper(ctx context.Context, queries *db.Queries, ttl time.Duration) { if ttl <= 0 { slog.Info("device sweeper disabled (ttl <= 0)") return } slog.Info("device sweeper running", "ttl", ttl, "interval", DeviceSweepInterval) t := time.NewTicker(DeviceSweepInterval) defer t.Stop() for { select { case <-ctx.Done(): return case <-t.C: cutoff := time.Now().Add(-ttl).Unix() if err := queries.DeleteStaleDevices(ctx, cutoff); err != nil { slog.Error("device sweeper failed", "err", err) } } } }