🐛 bug: Fix maintain CustomCtx across middlewares#3852
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds per-request custom context support: DefaultCtx gains an unexported Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Client
participant App
participant Acquire as AcquireCtx
participant Ctx as DefaultCtx
participant Handlers as HandlerChain
Client->>App: HTTP request
App->>Acquire: AcquireCtx()
Acquire-->>Ctx: provide DefaultCtx (may hold CustomCtx)
alt handlerCtx (CustomCtx) present
Note right of Ctx `#a2d5c6`: active handlerCtx used
Ctx->>Handlers: continueHandlers() -> app.nextCustom(path with CustomCtx)
Handlers->>Ctx: handler invoked with CustomCtx via activeHandler()
else no CustomCtx
Note right of Ctx `#f3e8c6`: default flow
Ctx->>Handlers: continueHandlers() -> app.next (standard)
Handlers->>Ctx: handler invoked with DefaultCtx
end
Handlers->>Client: response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 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 |
Summary of ChangesHello @ReneWerner87, 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 addresses an issue where custom context implementations were not consistently maintained across middleware continuations in the application's request handling flow. By introducing mechanisms to store, load, and clear the active custom context within the Highlights
Using Gemini Code AssistThe 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 by creating a comment using either
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 Limitations & Feedback Gemini Code Assist 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3852 +/- ##
=======================================
Coverage 92.10% 92.11%
=======================================
Files 115 115
Lines 9754 9774 +20
=======================================
+ Hits 8984 9003 +19
Misses 490 490
- Partials 280 281 +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 introduces a mechanism to maintain a custom context across middleware handlers. The changes involve storing the custom context on the fasthttp.RequestCtx and then loading it into the Fiber Ctx during its lifecycle. The implementation looks correct and is supported by a new test case. I've provided a couple of suggestions to improve code maintainability and idiomatic Go usage.
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for custom context implementations to work correctly with middleware in Fiber. Previously, custom contexts were not properly passed through middleware chains, causing type assertion failures and incorrect behavior.
- Introduces a
handlerContextKeyto track custom context instances through the request lifecycle - Modifies the
Next()andRestartRouting()methods to properly route through custom context handlers - Adds comprehensive test coverage for custom context behavior with middleware
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ctx_interface.go | Stores custom context reference in fasthttp request context during acquisition to enable proper routing |
| ctx.go | Adds custom context tracking field and constants, updates Next/RestartRouting to use custom handlers when appropriate, manages custom context lifecycle in Reset/release |
| ctx_test.go | Adds test validating custom context works correctly with middleware chains |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
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 (3)
ctx.go(3 hunks)ctx_interface.go(1 hunks)ctx_interface_gen.go(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- ctx_interface.go
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go
📄 CodeRabbit inference engine (AGENTS.md)
**/*.go: Apply formatting using gofumpt (Make target: format)
Optimize struct field alignment using betteralign (Make target: betteralign)
Modernize code using gopls modernize (Make target: modernize)
Files:
ctx.goctx_interface_gen.go
🧠 Learnings (4)
📓 Common learnings
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.
📚 Learning: 2024-11-08T04:10:42.990Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/cache/cache_test.go:897-897
Timestamp: 2024-11-08T04:10:42.990Z
Learning: In the Fiber framework, `Context()` is being renamed to `RequestCtx()`, and `UserContext()` to `Context()` to improve clarity and align with Go's context conventions.
Applied to files:
ctx.goctx_interface_gen.go
📚 Learning: 2024-11-10T23:44:13.704Z
Learnt from: gaby
Repo: gofiber/fiber PR: 3193
File: middleware/adaptor/adaptor.go:111-111
Timestamp: 2024-11-10T23:44:13.704Z
Learning: In the `middleware/adaptor/adaptor.go` file of the Fiber framework, when updating context handling, replacing `c.Context()` with `c.RequestCtx()` is appropriate to access the `fasthttp.RequestCtx`.
Applied to files:
ctx.goctx_interface_gen.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: sixcolors
Repo: gofiber/fiber PR: 3016
File: middleware/session/config.go:122-122
Timestamp: 2024-10-08T19:06:06.583Z
Learning: In `DefaultErrorHandler(c *fiber.Ctx, err error)`, since `c` is a pointer to an interface, we need to dereference `*c` when calling interface methods like `SendStatus`.
Applied to files:
ctx.go
🧬 Code graph analysis (2)
ctx.go (2)
ctx_interface.go (1)
CustomCtx(15-35)ctx_interface_gen.go (1)
Ctx(18-431)
ctx_interface_gen.go (1)
ctx_interface.go (1)
CustomCtx(15-35)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: lint
- GitHub Check: unit (1.25.x, macos-latest)
- GitHub Check: unit (1.25.x, macos-13)
- GitHub Check: repeated
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
last change is the fastest i can produce @ksw2000 @arturmelanchyk do you have ideas to improve this ? |
|
nothing I can think of |
Summary
Testing
Codex Task