feat: Implement API Caching, Resilient Request Retries, and Error Backoff (#219)#260
Conversation
|
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
|
|
@jagdish-15 you can review it! |
|
/format |
|
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:
|
|
/format |
|
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:
|
Description
This PR implements caching for the
/api/user/:usernameendpoint 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 JSMapwith self-pruning). Modified the/api/user/:usernameendpoint to utilize the cache, serve stale cache entries as a fallback when the downstream API fails, and return a graceful JSON502 Bad Gatewaypayload if no cache exists.scripts/sync-leaderboard.js: Configured a globalaxiosresponse 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
Testing
Checklist
npx prettier --write .before submittingfeature/*branch, not themainbranchScreenshots / Screen Recording
(None required - backend/script logic changes)