π [Proposal]: Make Fiber.Ctx Implement Context.Context
π [Proposal]: Make fiber.Ctx implement context.Context
Feature Proposal Description
Having multiple types of contexts in Go can be confusing, especially for developers transitioning from other languages. This proposal aims to simplify the context handling in Fiber by making fiber.Ctx
implement the context.Context
interface.
Why not just make fiber.Ctx implement context.Context?
In Go, the context.Context
interface is a standard way to handle context in various packages. Implementing this interface in fiber.Ctx
would allow developers to use the same context handling mechanisms across different packages, making it easier to write and maintain code.
The context.Context
interface provides a set of methods for handling context, including:
type context.Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any)any
}
By implementing this interface in fiber.Ctx
, we can leverage the existing context handling mechanisms in Go and provide a more consistent and predictable experience for developers.
Alignment with Express API
Fiber is a popular Go web framework that aims to provide a similar experience to Express.js. However, in this proposal, we are not aligning with the Express API, as the context handling mechanisms in Fiber are different from those in Express.
HTTP RFC Standards Compliance
This proposal does not involve any changes to the HTTP RFC standards compliance of Fiber.
API Stability
Making fiber.Ctx
implement context.Context
would not be backward compatible with v2, as it would require changes to the existing API.
Feature Examples
// Create a new context
ctx := context.Background()
// Get the deadline from the context
deadline, ok := ctx.Deadline()
if !ok {
// Handle the case where the deadline is not set
}
// Get the error from the context
err := ctx.Err()
if err != nil {
// Handle the error
}
// Get the value from the context
value := ctx.Value("key")
if value != nil {
// Use the value
}
Checklist:
- [x] I agree to follow Fiber's Code of Conduct.
- [x] I have searched for existing issues that describe my proposal before opening this one.
- [x] I understand that a proposal that does not meet these guidelines may be closed without explanation.
Benefits
Implementing context.Context
in fiber.Ctx
would provide several benefits, including:
- Simplified context handling: By using the same context handling mechanisms across different packages, developers can write and maintain code more easily.
- Improved consistency: Implementing
context.Context
infiber.Ctx
would provide a more consistent and predictable experience for developers. - Better integration with other packages: By using the same context handling mechanisms, Fiber can better integrate with other packages that use
context.Context
.
Drawbacks
There are no significant drawbacks to implementing context.Context
in fiber.Ctx
. However, it would require changes to the existing API, which may break backward compatibility with v2.
Alternatives
There are no alternative solutions that would achieve the same benefits as implementing context.Context
in fiber.Ctx
. However, developers could use the context.Context
interface directly in their code, which would require more boilerplate code and may not provide the same level of consistency and predictability.
Conclusion
Implementing context.Context
in fiber.Ctx
would provide several benefits, including simplified context handling, improved consistency, and better integration with other packages. While it would require changes to the existing API, which may break backward compatibility with v2, the benefits outweigh the drawbacks. Therefore, we propose making fiber.Ctx
implement context.Context
.
π [Proposal]: Make fiber.Ctx implement context.Context
Q&A
Q: What is the purpose of this proposal?
A: The purpose of this proposal is to simplify the context handling in Fiber by making fiber.Ctx
implement the context.Context
interface.
Q: Why is it necessary to implement context.Context in fiber.Ctx?
A: Implementing context.Context
in fiber.Ctx
would allow developers to use the same context handling mechanisms across different packages, making it easier to write and maintain code.
Q: What are the benefits of implementing context.Context in fiber.Ctx?
A: The benefits of implementing context.Context
in fiber.Ctx
include:
- Simplified context handling: By using the same context handling mechanisms across different packages, developers can write and maintain code more easily.
- Improved consistency: Implementing
context.Context
infiber.Ctx
would provide a more consistent and predictable experience for developers. - Better integration with other packages: By using the same context handling mechanisms, Fiber can better integrate with other packages that use
context.Context
.
Q: What are the drawbacks of implementing context.Context in fiber.Ctx?
A: The only drawback of implementing context.Context
in fiber.Ctx
is that it would require changes to the existing API, which may break backward compatibility with v2.
Q: Are there any alternative solutions that would achieve the same benefits?
A: There are no alternative solutions that would achieve the same benefits as implementing context.Context
in fiber.Ctx
. However, developers could use the context.Context
interface directly in their code, which would require more boilerplate code and may not provide the same level of consistency and predictability.
Q: How would this proposal affect existing code?
A: This proposal would require changes to the existing API, which may break backward compatibility with v2. However, the changes would be minimal and would not affect the functionality of existing code.
Q: What is the expected timeline for implementing this proposal?
A: The expected timeline for implementing this proposal is not yet determined. However, it is expected to be completed within the next few releases of Fiber.
Q: How can I provide feedback on this proposal?
A: You can provide feedback on this proposal by commenting on this issue or by reaching out to the Fiber maintainers directly.
Q: What are the next steps for this proposal?
A: The next steps for this proposal are to:
- Discuss the proposal: Discuss the proposal with the Fiber maintainers and the community to gather feedback and input.
- Make changes to the API: Make changes to the existing API to implement
context.Context
infiber.Ctx
. - Test and validate: Test and validate the changes to ensure that they do not break existing code.
Additional Resources
- Fiber documentation: The Fiber documentation provides more information on the
fiber.Ctx
type and thecontext.Context
interface. - Go documentation: The Go documentation provides more information on the
context.Context
interface and its methods. - Fiber issue tracker: The Fiber issue tracker provides a place for developers to report issues and provide feedback on the proposal.