Skip to content

fix(api): throttle api_keys last_used_at writes in auth middleware - #381

Merged
Quick104 merged 2 commits into
Silo-Server:mainfrom
JeremySNR:fix/api-key-last-used-throttle
Jul 20, 2026
Merged

Quick104 merged 2 commits into
Silo-Server:mainfrom
JeremySNR:fix/api-key-last-used-throttle

Conversation

@JeremySNR

@JeremySNR JeremySNR commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

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/middleware
  • GOWORK=off go test ./internal/jellycompat -run 'TestRequireAdminAPIKey|TestRequireSessionOrAPIKeySession|TestResolveSession'
  • GOWORK=off go test -race ./internal/auth ./internal/api/middleware

Built with AI assistance. The resulting diff was read, tested, and independently reviewed before merge.

Summary by CodeRabbit

  • Improvements
    • API key usage tracking is now more efficient by consolidating updates and limiting repeated writes.
    • Authentication flows continue recording when API keys were last used, including compatibility session flows.
    • Background updates now use timeouts to improve reliability and prevent unbounded tracking data.

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.
@coderabbitai

coderabbitai Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

API-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.

Changes

API key last-used tracking

Layer / File(s) Summary
Shared last-used tracker and tests
internal/auth/api_key_last_used.go, internal/auth/api_key_last_used_test.go
Adds per-key throttling, stale-entry pruning, asynchronous persistence with deadlines, and tests for these behaviors.
API middleware integration
internal/api/middleware/auth.go
Initializes the tracker and records successful API-key authentication through it.
Jelly compatibility integration
internal/jellycompat/auth_api_key.go
Replaces local last-used state and helper logic with the shared tracker in authentication and session resolution paths.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: quick104

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: throttling api_keys.last_used_at writes in the auth middleware.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread internal/api/middleware/auth.go Resolved

@Quick104 Quick104 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the final dba788e head. Shared tracker ownership and bounded pruning address the prior findings; focused auth, middleware, Jellycompat auth, and race tests pass.

@Quick104
Quick104 merged commit e56a1b3 into Silo-Server:main Jul 20, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants