Skip to content

feat: Implement API Caching, Resilient Request Retries, and Error Backoff (#219)#260

Merged
jagdish-15 merged 4 commits into
codepvg:mainfrom
yashvi-3106:feat/api-caching-retries
Jun 28, 2026
Merged

feat: Implement API Caching, Resilient Request Retries, and Error Backoff (#219)#260
jagdish-15 merged 4 commits into
codepvg:mainfrom
yashvi-3106:feat/api-caching-retries

Conversation

@yashvi-3106

Copy link
Copy Markdown
Contributor

Description

This PR implements caching for the /api/user/:username endpoint to reduce response times and unnecessary outbound queries to the external LeetCode API wrapper. It also implements robust request retries with exponential backoff and randomized jitter inside the daily synchronization script to safeguard against rate limits (HTTP 429) or transient server errors. Lastly, it ensures that downstream API failures are handled gracefully, logging the errors and returning a clean JSON error response (or falling back to expired cached data if available).

Linked Issue

Fixes #219

Changes Made

  • scripts/fetch-user-info.js: Updated the live API fetch logic to propagate network/status errors instead of swallowing them internally.
  • server.js: Added a 2-minute TTL in-memory cache system (using a JS Map with self-pruning). Modified the /api/user/:username endpoint to utilize the cache, serve stale cache entries as a fallback when the downstream API fails, and return a graceful JSON 502 Bad Gateway payload if no cache exists.
  • scripts/sync-leaderboard.js: Configured a global axios response interceptor to handle rate limiting (429), server errors (5xx), and timeout errors, retrying up to 3 times using exponential backoff and jitter.

Type of Change

  • Bug fix
  • New feature
  • UI/Visual update
  • Documentation update
  • Refactor

Testing

  • Tested locally
  • Tested on mobile viewport (if applicable)
  • No console errors introduced

Checklist

  • My code follows the project's coding style
  • I have formatted my code locally by running npx prettier --write . before submitting
  • I am submitting my PR from a dedicated feature/* branch, not the main branch
  • I have performed a self-review of my code
  • My changes generate no new warnings or errors
  • I have updated documentation if required
  • I have linked the relevant issue

Screenshots / Screen Recording

(None required - backend/script logic changes)

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for submitting a pull request.

Please ensure your changes comply with the project's contribution guidelines and that all workflow checks pass successfully.

Formatting and Branching

  • Please confirm you have formatted your code locally using npx prettier --write . before requesting a review.
  • Ensure this PR is made from a feature/* branch and not main.

Note: This project is currently maintained by a solo maintainer, so reviews and responses may sometimes take a little time. Thanks for your patience.

@yashvi-3106

Copy link
Copy Markdown
Contributor Author

@jagdish-15 you can review it!

@jagdish-15

Copy link
Copy Markdown
Collaborator

/format

@github-actions

Copy link
Copy Markdown
Contributor

All done! 💅 I've run Prettier and pushed the formatting fixes to this PR.

Just a heads up: Since this commit was pushed by a bot, GitHub won't automatically re-run your CI checks. To trigger them, you can either:

  • Push an empty commit (git commit --allow-empty -m "Trigger builds" and push)
  • Close and immediately reopen this Pull Request.

@jagdish-15

Copy link
Copy Markdown
Collaborator

/format

@github-actions

Copy link
Copy Markdown
Contributor

All done! 💅 I've run Prettier and pushed the formatting fixes to this PR.

Just a heads up: Since this commit was pushed by a bot, GitHub won't automatically re-run your CI checks. To trigger them, you can either:

  • Push an empty commit (git commit --allow-empty -m "Trigger builds" and push)
  • Close and immediately reopen this Pull Request.

@jagdish-15 jagdish-15 merged commit 990a736 into codepvg:main Jun 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] API Caching, Resilient Request Retries, and Error Backoff

2 participants