fix: add DB stale-cache fallback to stats, prices, markets/stats, and crank#164
Conversation
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 27 minutes and 53 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThe PR refactors four route handlers (crank, markets, prices, stats) to use a centralized Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/routes/stats.test.ts (1)
329-340: Please cover the stale-cache hit path too.This only verifies the cache-miss failure case. The behavior this PR actually adds is “successful read seeds cache, later DB failure returns stale data with
Warning/X-Cache-Statusheaders”; without that case, the main fallback path can regress unnoticed.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/routes/stats.test.ts` around lines 329 - 340, Add a test that exercises the stale-cache hit path by first seeding the cache with a successful response then simulating a DB error and asserting the stale fallback behavior: use mockSupabase.from to return valid count/data and call statsRoutes().request("/stats") to populate the cache (via the same code path that seeds it), then reconfigure mockSupabase.from to return error (new Error("Database error")) and call statsRoutes().request("/stats") again; assert the second response is 200, the JSON matches the previously returned data, and response headers include the stale indicators (e.g., Warning and X-Cache-Status set to a stale value). Ensure you reuse chainable/mock setup and statsRoutes() so the same cache mechanism is exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/routes/stats.test.ts`:
- Around line 329-340: Add a test that exercises the stale-cache hit path by
first seeding the cache with a successful response then simulating a DB error
and asserting the stale fallback behavior: use mockSupabase.from to return valid
count/data and call statsRoutes().request("/stats") to populate the cache (via
the same code path that seeds it), then reconfigure mockSupabase.from to return
error (new Error("Database error")) and call statsRoutes().request("/stats")
again; assert the second response is 200, the JSON matches the previously
returned data, and response headers include the stale indicators (e.g., Warning
and X-Cache-Status set to a stale value). Ensure you reuse chainable/mock setup
and statsRoutes() so the same cache mechanism is exercised.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6c75a538-3fca-4491-bb78-82b0e4f2ee74
📒 Files selected for processing (7)
src/routes/crank.tssrc/routes/markets.tssrc/routes/prices.tssrc/routes/stats.tstests/routes/crank.test.tstests/routes/prices.test.tstests/routes/stats.test.ts
Upstream changed /prices/:slab to ascending order + limit 1500 (for chart rendering), but the test still expected descending + 100. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
… crank endpoints Four critical aggregate endpoints returned hard 500 errors during Supabase outages while /markets and /funding/global already had graceful degradation via withDbCacheFallback. These endpoints power frontend dashboards and landing pages — a 500 here makes the entire app appear broken. Now wraps these endpoints with the existing withDbCacheFallback pattern, serving stale cached data (up to 1 hour, with Warning and X-Cache-Status headers) during DB outages, or 503 if no cache is available. Endpoints added: - GET /stats (cache key: stats:platform) — all 4 queries wrapped atomically - GET /prices/markets (cache key: prices:markets) - GET /markets/stats (cache key: markets:stats) - GET /crank/status (cache key: crank:status) Tests updated to expect 503 (not 500) on DB failure with no stale cache, and to clear DB cache between tests to prevent cross-test cache pollution. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
69e4c6d to
871a32d
Compare
Summary
/marketsand/funding/globalalready had graceful degradation viawithDbCacheFallback. These endpoints power frontend dashboards — a 500 makes the entire app appear broken.withDbCacheFallbackpattern, serving stale cached data (up to 1 hour, withWarningandX-Cache-Statusheaders) during outages, or 503 if no cache available.Endpoints protected
GET /statsstats:platformGET /prices/marketsprices:marketsGET /markets/statsmarkets:statsGET /crank/statuscrank:statusDesign decisions
/statswraps all 4 queries as one unit — partial data (e.g., volume but 0 trades) would be worse than a consistently-stale snapshotdomain:scopeconvention matching existingmarkets:allandfunding:globalTest changes
clearDbCache()tobeforeEachto prevent cross-test cache pollutionTest plan
tsc --noEmitpassesvitest runpasses (186/186 tests)🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Refactor