🧹 chore: refactor cache handler lock flow#4492
Conversation
guards Refactor lock management so scope is defer-bounded and unlocks can't be missed: - Add cacheLockGuard with a withCacheLock(mux, fn) helper that acquires the lock and releases it via defer, replacing every raw mux.Lock()/Unlock() pair in the read, store, and eviction sections. - Extract deleteCurrentEntry(), which encapsulates the unlock -> deleteKey -> relock -> removeHeapEntry sequence in one place instead of repeating it at three read-path branches and the post-response deletion. - Replace `goto continueRequest` with an anonymous func returning (handled bool, err error) so control flow is explicit. - Convert the eviction loop's mid-loop `mux.Unlock(); return` to setting an error and returning from the withCacheLock closure, so the lock is always released. - Narrow mux from sync.RWMutex to sync.Mutex, since only exclusive locking is ever used.
|
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughRefactors cache middleware locking and request handling, updates invalidation and eviction flows, and adjusts expiration bookkeeping in the cache path. ChangesCache lock refactor
Estimated code review effort: 4 (Complex) | ~60 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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.
Actionable comments posted: 1
🤖 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 `@middleware/cache/cache.go`:
- Around line 346-352: The revalidation flow in cache handling drops e after
releasing the old entry, but later cleanup only runs through deleteCurrentEntry
when e is still non-nil. Update the revalidation paths in cache.go so the code
that handles reqDirectives/maxAge and the response handling for private,
no-cache, and Vary:* preserves whether there was a prior cached entry and its
oldHeapIdx separately from e, then explicitly delete the key and remove the old
heap entry even when e has been cleared. Use the existing
revalidation/deleteCurrentEntry logic as the place to wire in this cleanup in
the affected branches.
🪄 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: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: d6e26a0f-cf21-4b90-bb8a-c95662184f78
📒 Files selected for processing (1)
middleware/cache/cache.go
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4492 +/- ##
==========================================
- Coverage 93.01% 93.01% -0.01%
==========================================
Files 139 139
Lines 13789 13816 +27
==========================================
+ Hits 12826 12851 +25
Misses 596 596
- Partials 367 369 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Review SummaryReviewed the full diff, traced every lock acquire/release path, and verified locally:
No critical issuesThe refactoring is well-executed:
Bug fix in commit 2 — real correctness improvementThe When revalidation sets This is a correctness issue (RFC 9111 violation), not performance or safety:
Minor notes (non-blocking)
VerdictApprove. The refactoring is sound, and the included bug fix is a genuine correctness improvement worth merging on its own merit. |
|
@pageton thanks for the detailed review, I will work through the addressable notes with specific tests for the lines, removal of redundant guard and make ci checks validation as part of the PR summary. |
There was a problem hiding this comment.
Pull request overview
Refactors the cache middleware handler’s lock/unlock control flow to remove goto-based paths, centralize lock handling via a guard helper, and simplify deletion/eviction bookkeeping by moving storage I/O outside the lock while keeping heap/accounting updates under lock.
Changes:
- Introduces a
cacheLockGuard+withCacheLockhelper and switches the handler mutex fromsync.RWMutextosync.Mutex. - Refactors the cache-lookup phase to return an explicit
(handled, error)result instead of usinggoto continueRequest. - Deduplicates cached-entry deletion and restructures eviction space reservation/restoration under a single lock helper.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 08fcaca7b3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4f449140ef
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 26121f8194
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a4c051f84a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Summary
goto continueRequestby making the cache lookup phase return an explicit handled/error result.sync.Mutexbecause no read-lock paths are used.Closes #4334.
Validation
Manual validation run locally because the PR checks currently only show labeler/Dependabot jobs:
make lint->0 issues.go test ./middleware/cache -race -count=1-> passedgo test ./middleware/cache -run Test_Cache_RevalidationUncacheableResponseDeletesStaleEntry -count=1-> passedgo test ./middleware/cache -coverprofile=/tmp/fiber-cache-cover.out -count=1-> passed,coverage: 92.0% of statementsmake test->DONE 3917 tests, 2 skipped in 54.602s