Skip to content

do not cache 500 responses#61

Merged
Aiiion merged 10 commits into
masterfrom
development
Jun 2, 2026
Merged

do not cache 500 responses#61
Aiiion merged 10 commits into
masterfrom
development

Conversation

@Aiiion

@Aiiion Aiiion commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • CI now includes Redis for end-to-end testing.
  • Bug Fixes

    • Responses with server errors (5xx) are no longer cached.
  • Chores

    • Cache/storage unified to use Redis consistently across environments.
    • Test runner configured to run tests sequentially for more stable results.
  • Tests

    • Test suites now explicitly close Redis connections during teardown.

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Aiiion, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 20 minutes and 10 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3d531320-2353-4c92-a26c-f809de3783f2

📥 Commits

Reviewing files that changed from the base of the PR and between a028dc0 and f7e1808.

📒 Files selected for processing (3)
  • .github/copilot-instructions.md
  • src/middleware/cache.middleware.mjs
  • src/tests/cache.middleware.test.mjs
📝 Walkthrough

Walkthrough

Removes in-memory Redis test-store fallbacks, makes the Redis service the single implementation for caching, queueing, locking, and test helpers, adds a Redis container and REDIS_URL to CI, updates cache middleware to avoid caching 5xx responses, and ensures tests close Redis connections on teardown.

Changes

Redis Integration and Test-Store Removal

Layer / File(s) Summary
CI Redis Service Setup
.github/workflows/tests.yml
Adds REDIS_URL environment variable and a Redis 7 service container with health checks to the test workflow.
Cache Middleware Status Filtering
src/middleware/cache.middleware.mjs
Cache middleware now conditionally writes responses to Redis only when res.statusCode < 500 or unset, preventing caching of server errors.
Redis Connection and Client Management
src/services/redis.service.mjs
Module-level Redis client state and URL/client helpers introduced; removed NODE_ENV test early-return so client logic always targets Redis.
JSON Storage, Close, and Deletion
src/services/redis.service.mjs
setJsonValue/getJsonValue and deleteValue always operate through Redis; closeRedisConnection always quits the client and clears module state; JSON parse failures delete the Redis key.
Request Log Queue Operations
src/services/redis.service.mjs
Queue helpers (enqueueRequestLog, getRequestLogQueueLength, getRequestLogProcessingLength, moveRequestLogsToProcessing) now use Redis list commands and RPOPLPUSH looping.
Request Log Retrieval
src/services/redis.service.mjs
getProcessingRequestLogs and getQueuedRequestLogs now return Redis LRANGE results with no in-memory fallback.
Peeking and Lock Management
src/services/redis.service.mjs
peekOldestRequestLog, clearProcessingRequestLogs, acquireRequestLogsFlushLock, and releaseRequestLogsFlushLock now use LINDEX, DEL, SET NX EX, and token-checked Lua EVAL exclusively.
Test Data Cleanup
src/services/redis.service.mjs
clearRedisTestData now throws outside NODE_ENV === 'test' and calls Redis flushDb() when allowed.
Test Teardown: Close Redis Connection
src/tests/*
Tests import closeRedisConnection and add afterAll hooks to explicitly close Redis connections after suites.
Test runner flag
package.json
npm test now runs Jest with --runInBand to execute tests sequentially.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I nudge the Redis burrow gate,
lists and locks all tidy and straight,
CI hums with a brand new key,
tests close gently — safe as can be,
hop, stash, and flush — a rabbit's state.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'do not cache 500 responses' directly reflects a key change in cache.middleware.mjs that now conditionally skips Redis caching for 5xx status codes, making it clear and specific to the main change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch development

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/middleware/cache.middleware.mjs (1)

6-8: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Reconsider the NODE_ENV === 'test' bypass in cache.middleware.mjs

  • src/middleware/cache.middleware.mjs currently skips caching entirely in test mode (if (process.env.NODE_ENV === 'test') return next()), so route-level caching never runs under normal test execution.
  • src/tests/cache.middleware.test.mjs explicitly sets process.env.NODE_ENV = 'development' in beforeEach, meaning the unit tests only cover the caching path by bypassing the bypass.
  • Since Redis is used in tests too (e.g., src/services/redis.service.mjs’s clearRedisTestData() calls flushDb()), the test-mode bypass prevents exercising the real Redis-backed cache behavior during CI tests—especially the new res.statusCode < 500 behavior.

Either remove the test bypass (or gate it on Redis availability rather than NODE_ENV) and/or add tests that run caching in NODE_ENV='test' (mock Redis or use the test Redis instance) and assert setJsonValue is not called for 5xx responses.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/middleware/cache.middleware.mjs` around lines 6 - 8, The current cache
middleware short-circuits caching when NODE_ENV === 'test' which prevents
exercising Redis-backed behavior; remove that environment-only bypass in
src/middleware/cache.middleware.mjs (delete the `if (process.env.NODE_ENV ===
'test') return next()` branch) or replace it with a runtime check for Redis
availability (e.g., verify the Redis client/connection used by the middleware is
present/connected before returning early), and ensure the middleware still
applies the `res.statusCode < 500` guard before calling the cache writer (e.g.,
where it invokes `setJsonValue`). Also update tests to run the caching path
under test environment (either keep setting NODE_ENV='test' and mock Redis
client methods like `setJsonValue` and assert it is not called for 5xx
responses, or use the real test Redis and assert no cache writes on 5xx).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/services/redis.service.mjs`:
- Around line 156-158: clearRedisTestData currently calls getClient() then
flushDb(), which will wipe DB 0 (the default) and can delete unrelated keys;
change it so it only touches a dedicated test DB or targeted keys: either
require/validate that getRedisUrl() points to a test-only DB and call
client.select(testDb) before flushDb(), or replace flushDb() with a safe cleanup
that SCANs and deletes only test prefixes (e.g., keys matching "test_*" or the
module's prefix) using client.scan() + DEL. Update clearRedisTestData and its
callers (e.g., tests invoking clearRedisTestData, getClient/getRedisUrl) to
enforce the test-DB or prefix constraint to prevent wiping shared session/cache
keys like auth_session_*.

---

Outside diff comments:
In `@src/middleware/cache.middleware.mjs`:
- Around line 6-8: The current cache middleware short-circuits caching when
NODE_ENV === 'test' which prevents exercising Redis-backed behavior; remove that
environment-only bypass in src/middleware/cache.middleware.mjs (delete the `if
(process.env.NODE_ENV === 'test') return next()` branch) or replace it with a
runtime check for Redis availability (e.g., verify the Redis client/connection
used by the middleware is present/connected before returning early), and ensure
the middleware still applies the `res.statusCode < 500` guard before calling the
cache writer (e.g., where it invokes `setJsonValue`). Also update tests to run
the caching path under test environment (either keep setting NODE_ENV='test' and
mock Redis client methods like `setJsonValue` and assert it is not called for
5xx responses, or use the real test Redis and assert no cache writes on 5xx).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5b0ab372-2d77-47ea-90b0-2fb5e345aa7c

📥 Commits

Reviewing files that changed from the base of the PR and between 790b419 and f9dc88f.

📒 Files selected for processing (3)
  • .github/workflows/tests.yml
  • src/middleware/cache.middleware.mjs
  • src/services/redis.service.mjs

Comment thread src/services/redis.service.mjs
@Aiiion

Aiiion commented Jun 2, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Aiiion Aiiion merged commit 21117f1 into master Jun 2, 2026
2 checks passed
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