-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🩹 Fix: behavior of DefaultCtx.Fresh
when 'Last-Modified' and 'If-Modified-Since' are equal
#3150
Conversation
…Ctx.Fresh` now returns true
WalkthroughThe changes involve modifications to the Changes
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3150 +/- ##
=======================================
Coverage 80.18% 80.18%
=======================================
Files 117 117
Lines 9043 9043
=======================================
Hits 7251 7251
Misses 1360 1360
Partials 432 432
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
ctx_test.go (3)
1419-1430
: Consider adding cleanup for acquired contextThe benchmark function
Benchmark_Ctx_Fresh_LastModified
acquires a context but doesn't release it. To ensure proper resource management, consider adding a cleanup step:func Benchmark_Ctx_Fresh_LastModified(b *testing.B) { app := New() c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) // Add this line for cleanup // ... rest of the function }This ensures that the acquired context is properly released after the benchmark, preventing potential resource leaks in extensive benchmark runs.
1400-1403
: Approve new test cases with suggestion for organizationThe new test cases for
If-Modified-Since
andLast-Modified
headers are valuable additions to theTest_Ctx_Fresh
function. They improve the test coverage for theFresh
method.To further enhance the test structure, consider grouping these new cases with other similar header-based tests earlier in the function. This would improve the overall organization and readability of the test suite.
Example:
// Group all header-based tests together c.Request().Header.Set(HeaderIfNoneMatch, "*") // ... existing tests ... // New tests c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT") c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT") require.True(t, c.Fresh()) c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:27:59 GMT") c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT") require.False(t, c.Fresh())This organization would make it easier to understand and maintain all header-related test cases.
1419-1430
: Approve new benchmark with suggestion for cleanupThe new
Benchmark_Ctx_Fresh_LastModified
function is a valuable addition to the benchmark suite. It correctly tests the performance of theFresh
method withLast-Modified
andIf-Modified-Since
headers.To ensure proper resource management, consider adding a cleanup step:
func Benchmark_Ctx_Fresh_LastModified(b *testing.B) { app := New() c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) // Add this line for cleanup for n := 0; n < b.N; n++ { c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT") c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT") c.Fresh() } }This ensures that the acquired context is properly released after the benchmark, preventing potential resource leaks in extensive benchmark runs.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- ctx.go (1 hunks)
- ctx_test.go (2 hunks)
🔇 Additional comments not posted (1)
ctx.go (1)
627-627
: Approval: Improved caching behavior for equal timestampsThis change correctly addresses the issue when 'Last-Modified' and 'If-Modified-Since' headers are equal. By using
Compare
instead ofBefore
, the method now considers equal timestamps as "fresh", which is the correct behavior according to HTTP caching standards.The new implementation
lastModifiedTime.Compare(modifiedSinceTime) != 1
returns true whenlastModifiedTime
is either before or equal tomodifiedSinceTime
, effectively fixing the issue described in the PR objectives.This change will prevent unnecessary cache invalidation when the resource hasn't been modified since the last request, potentially improving performance and reducing server load.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
If 'Last-Modified' and 'If-Modified-Since' are equal, then
DefaultCtx.Fresh
returns false.However, if no updates have been made, the cache should not to be stale.
Thank you.
Type of change
Checklist
/docs/
directory for Fiber's documentation.Benchmarks(on GitHub Codespaces)
go test -v -run=^$ -bench=Benchmark_Ctx_Fresh_LastModified -benchmem -count=4
Before
After