Skip to content

Add Reddit rate limiting, OAuth2 auth, and response caching#1

Open
hev wants to merge 1 commit intomainfrom
reddit-rate-limits
Open

Add Reddit rate limiting, OAuth2 auth, and response caching#1
hev wants to merge 1 commit intomainfrom
reddit-rate-limits

Conversation

@hev
Copy link
Copy Markdown
Owner

@hev hev commented Mar 3, 2026

Summary

  • OAuth2 authentication: Reads REDDIT_CLIENT_ID, REDDIT_CLIENT_SECRET, REDDIT_USERNAME, REDDIT_PASSWORD from env vars to use oauth.reddit.com with 60 req/min limits. Falls back to unauthenticated access when absent.
  • Rate limiter: Token-bucket limiter (golang.org/x/time/rate) at 8 req/min (unauth) or 55 req/min (auth), dynamically adjusted from Reddit's X-Ratelimit-* headers.
  • Retry with backoff: Up to 3 retries on HTTP 429 using Retry-After header or exponential backoff with jitter. Detects IP-level blocks (Retry-After: 0 with no rate-limit headers) and fails fast with a helpful message.
  • Response caching: In-memory TTL cache (10 min) keyed by URL, avoiding redundant fetches for repeated searches and thread lookups.
  • Tests: 9 unit tests covering retry, cache, rate limiting, backoff calculation, IP-ban detection, and 403 handling.

Test plan

  • go build ./... passes
  • go vet ./internal/search/... passes
  • go test ./internal/search/ -short -v — all 9 tests pass
  • Run without env vars — confirm unauthenticated fallback with rate limiting
  • Set REDDIT_CLIENT_ID etc. — confirm OAuth token obtained and oauth.reddit.com used
  • Run hiveminer run — confirm no 429 errors with rate limiter active

🤖 Generated with Claude Code

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

1 participant