🐛 bug: Fix Cache middleware handling of Age#3547
Conversation
|
""" WalkthroughThis update refines the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CacheMiddleware
participant UpstreamHandler
Client->>CacheMiddleware: HTTP Request
alt Cache Hit
CacheMiddleware->>Client: Response with updated Age header
else Cache Miss
CacheMiddleware->>UpstreamHandler: Forward Request
UpstreamHandler->>CacheMiddleware: Response (may include Age header)
CacheMiddleware->>CacheMiddleware: Store response, parse/set Age
CacheMiddleware->>Client: Response with Age header
end
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (8)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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 (
|
There was a problem hiding this comment.
Summary of Changes
Hello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request refines the cache middleware's behavior by improving its compliance with RFC 9111 for the Age header and by discontinuing the caching of 418 I'm a teapot responses. These changes involve modifications to the cache entry structure, its serialization, and corresponding updates to documentation and tests.
Highlights
- RFC 9111 Compliance for Age Header: The cache middleware now correctly calculates and sets the
Ageheader for cached responses. It achieves this by storing theAgevalue received from the upstream server and adding the time the response has been resident in the cache, adhering to the specifications of RFC 9111. - Stop Caching 418 I'm a teapot Responses: The middleware has been updated to no longer cache responses with the
418 I'm a teapotstatus code. This change removes a non-standard caching behavior and aligns the middleware with typical HTTP caching practices. - Cache Entry Structure and Serialization Updates: The internal
itemstruct, which represents a cached entry, now includes a dedicatedagefield to store the upstreamAgeheader value. Themsgpserialization code for this struct has been regenerated to properly handle the newagefield, as well as ensuring explicit serialization/deserialization for the existingttlfield. - Documentation and Test Updates: The project documentation has been updated to reflect that
418 I'm a teapotresponses are no longer cached. Corresponding tests have also been adjusted to verify that418responses are now correctly identified as uncacheable.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3547 +/- ##
==========================================
- Coverage 91.02% 91.00% -0.02%
==========================================
Files 110 110
Lines 10955 10967 +12
==========================================
+ Hits 9972 9981 +9
- Misses 731 733 +2
- Partials 252 253 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request implements RFC 9111 for Age header handling, stops caching 418 Teapot responses, and updates documentation and tests. A fix for serializing the ttl field for cached items when using external storage is also included. One suggestion has been made to improve the robustness of object pooling by resetting the heapidx field.
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: b71be30 | Previous: 55818fb | Ratio |
|---|---|---|---|
Benchmark_Cache |
14180 ns/op 41319 B/op 7 allocs/op |
423 ns/op 16 B/op 2 allocs/op |
33.52 |
Benchmark_Cache - ns/op |
14180 ns/op |
423 ns/op |
33.52 |
Benchmark_Cache - B/op |
41319 B/op |
16 B/op |
2582.44 |
Benchmark_Cache - allocs/op |
7 allocs/op |
2 allocs/op |
3.50 |
Benchmark_Cache_Storage |
14296 ns/op 41319 B/op 7 allocs/op |
796.5 ns/op 134 B/op 5 allocs/op |
17.95 |
Benchmark_Cache_Storage - ns/op |
14296 ns/op |
796.5 ns/op |
17.95 |
Benchmark_Cache_Storage - B/op |
41319 B/op |
134 B/op |
308.35 |
Benchmark_Cache_AdditionalHeaders |
107052 ns/op 324 B/op 4 allocs/op |
554.2 ns/op 16 B/op 2 allocs/op |
193.16 |
Benchmark_Cache_AdditionalHeaders - ns/op |
107052 ns/op |
554.2 ns/op |
193.16 |
Benchmark_Cache_AdditionalHeaders - B/op |
324 B/op |
16 B/op |
20.25 |
Benchmark_Cache_AdditionalHeaders - allocs/op |
4 allocs/op |
2 allocs/op |
2 |
BenchmarkAppendMsgitem-4_middleware_csrf - MB/s |
3215.63 MB/s |
356.77 MB/s |
9.01 |
BenchmarkAppendMsgstorageManager - MB/s |
1606.91 MB/s |
244.34 MB/s |
6.58 |
BenchmarkAppendMsgdata - MB/s |
1604.91 MB/s |
267.69 MB/s |
6.00 |
BenchmarkEncodedata - MB/s |
242.11 MB/s |
152.83 MB/s |
1.58 |
This comment was automatically generated by workflow using github-action-benchmark.
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
middleware/cache/cache.go(3 hunks)
🧰 Additional context used
🪛 GitHub Check: unit (1.24.x, ubuntu-latest)
middleware/cache/cache.go
[failure] 246-246:
cannot use v (variable of type int) as uint64 value in assignment
🪛 GitHub Check: govulncheck-check
middleware/cache/cache.go
[failure] 246-246:
cannot use v (variable of type int) as uint64 value in assignment
🪛 GitHub Check: unit (1.24.x, windows-latest)
middleware/cache/cache.go
[failure] 246-246:
cannot use v (variable of type int) as uint64 value in assignment
🪛 GitHub Actions: Run govulncheck
middleware/cache/cache.go
[error] 246-246: Cannot use variable 'v' of type int as uint64 value in assignment.
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: unit (1.24.x, macos-13)
- GitHub Check: unit (1.24.x, macos-latest)
- GitHub Check: lint
- GitHub Check: repeated
- GitHub Check: Compare
- GitHub Check: Analyse
🔇 Additional comments (2)
middleware/cache/cache.go (2)
15-15: Import addition looks correct.The
fasthttpimport is appropriately added to support theParseUintfunction used in the Age header parsing logic.
171-174: Excellent RFC 9111 compliant Age header implementation.The Age header calculation correctly implements RFC 9111 by:
- Computing resident time as
e.ttl - (e.exp - ts)- Adding the stored upstream age (
e.age) to the resident time- Providing the total age to clients
This ensures proper cache age tracking across the cache hierarchy.
Summary
Related #3383