Skip to content

feat: handle GitHub API rate limits gracefully#40

Closed
Ai-chan-0411 wants to merge 2 commits intoO2sa:mainfrom
Ai-chan-0411:feat/rate-limit-handling
Closed

feat: handle GitHub API rate limits gracefully#40
Ai-chan-0411 wants to merge 2 commits intoO2sa:mainfrom
Ai-chan-0411:feat/rate-limit-handling

Conversation

@Ai-chan-0411
Copy link
Copy Markdown
Contributor

Summary

Closes #32

Gracefully handles GitHub API rate limit errors (403/429) with fallback to stale cached data and user-friendly error responses.

Changes

  • lib/cache.ts — Add getStale() to retrieve expired cache entries as fallback
  • lib/github.ts — Catch rate limit errors, serve stale data when available, throw RateLimitError otherwise
  • app/api/compare/route.ts — Return 429 status with Retry-After header and friendly message

How it works

  1. GitHub returns 403/429 → check for stale cached data for that username
  2. Stale data found → return it transparently (users still get results)
  3. No stale data → return 429 with retryAfter seconds until reset

Note

Builds on the caching foundation from #33 (PR #39).

Test plan

  • Normal requests work unchanged
  • When rate limited with prior cached data, stale results are served
  • When rate limited without cache, 429 response includes retryAfter

Add in-memory cache with 5-minute TTL for GitHub GraphQL responses.
Duplicate requests for the same username are served from cache,
reducing API calls and improving response times.

- Add lib/cache.ts with generic get/set/stats helpers
- Wrap fetchGitHubUserData with cache lookup
- Add Cache-Control header to compare API response

Closes O2sa#33
When the GitHub API returns 403/429 (rate limited), the app now:
1. Falls back to stale cached data if available
2. Returns a friendly 429 response with Retry-After header when
   no cached data exists

Adds RateLimitError class and stale cache lookup helper.

Closes O2sa#32
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 11, 2026

@aoi-dev-0411 is attempting to deploy a commit to the osama's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions
Copy link
Copy Markdown

Thank you for the pull request! ✅

A maintainer will review this soon. Please be patient while we take a look. 🙌

@Ai-chan-0411
Copy link
Copy Markdown
Contributor Author

Closing this PR as there has been no maintainer feedback for several weeks. Thank you for the opportunity to contribute! Feel free to reopen if you would like these changes.

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.

Handle GitHub API rate limits gracefully

1 participant