Add Reddit rate limiting, OAuth2 auth, and response caching#1
Open
Add Reddit rate limiting, OAuth2 auth, and response caching#1
Conversation
Reddit's unauthenticated API allows ~10 req/min, which concurrent workers easily exceed. This adds four layers of protection: - OAuth2 "script" app auth via env vars for 60 req/min (6x improvement) - Token-bucket rate limiter (8/min unauth, 55/min auth) with dynamic adjustment based on X-Ratelimit headers from Reddit - Exponential backoff retry (up to 3x) on 429s with Retry-After support - In-memory TTL response cache (10 min) to avoid redundant fetches - IP-ban detection for Retry-After:0 with no rate-limit headers Falls back to unauthenticated behavior when env vars are absent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
REDDIT_CLIENT_ID,REDDIT_CLIENT_SECRET,REDDIT_USERNAME,REDDIT_PASSWORDfrom env vars to useoauth.reddit.comwith 60 req/min limits. Falls back to unauthenticated access when absent.golang.org/x/time/rate) at 8 req/min (unauth) or 55 req/min (auth), dynamically adjusted from Reddit'sX-Ratelimit-*headers.Retry-Afterheader or exponential backoff with jitter. Detects IP-level blocks (Retry-After: 0 with no rate-limit headers) and fails fast with a helpful message.Test plan
go build ./...passesgo vet ./internal/search/...passesgo test ./internal/search/ -short -v— all 9 tests passREDDIT_CLIENT_IDetc. — confirm OAuth token obtained andoauth.reddit.comusedhiveminer run— confirm no 429 errors with rate limiter active🤖 Generated with Claude Code