Repository navigation
fix(api): throttle api_keys last_used_at writes in auth middleware - #381
Conversation
Every API key request spawned a goroutine that ran an UPDATE on api_keys, so a key driving HLS segments or a polling integration hit the table with one write per request, and a stalled database could pile those goroutines up without bound. The jellycompat authenticator already guards this same write with a once-per-minute throttle per key; the main middleware was missing it. Bring the two in line. Track the last write per key ID and only launch the update once a minute has passed, with a timeout on the background write. The map is keyed by key ID so it stays bounded.
📝 WalkthroughWalkthroughAPI-key last-used persistence is centralized in a reusable tracker that throttles updates, prunes stale keys, and applies write timeouts. API middleware and Jelly compatibility authentication now use this tracker. ChangesAPI key last-used tracking
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 788ffb7974
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Every API key request was spawning a goroutine that issued an UPDATE on api_keys, so a key driving HLS segments or a polling integration hammered the table with one write per request, and a stalled database could pile those goroutines up without bound. The main middleware was missing the once-per-minute throttle already used by the Jellycompat authenticator.
This moves the behavior into a shared auth-owned tracker used by both paths. It records usage at most once per minute per key, puts a five-second timeout on each background write, logs failures, and prunes expired throttle entries inline once per interval so deleted or rotated key IDs do not accumulate for the life of the process.
Validated locally:
GOWORK=off go test ./internal/auth ./internal/api/middlewareGOWORK=off go test ./internal/jellycompat -run 'TestRequireAdminAPIKey|TestRequireSessionOrAPIKeySession|TestResolveSession'GOWORK=off go test -race ./internal/auth ./internal/api/middlewareBuilt with AI assistance. The resulting diff was read, tested, and independently reviewed before merge.
Summary by CodeRabbit