You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As brought up and discussed in #3252 (comment), this issue proposes to add the End() method to Ctx in v3 to allow instant flushing and closing of an active connection.
While Ctx's Drop() method added in the issue mentioned above allows to close a connection before sending a response, this new End() method would be to flush the current response before closing the connection.
This feature would be useful for stopping middleware from controlling the connection after a handler further up the method chain. Some middleware (like fiberprometheus) will work with a response after calling c.Next(). In addition, Fiber's default error handler may also write to the response, while a user may want to handle an error in different ways from within specific handlers and only log detailed errors internally.
It also can be an alternative to simply using return nil at the end of a handler, as done in ExpressJS (see below).
Alignment with Express API
The Express API provides an equivalent method res.end():
Calling res.end() closes the connection after flushing the response.
HTTP RFC Standards Compliance
N/A
API Stability
ExpressJS uses res.end(), so the API will be unlikely to be changed to match Express.
In addition, the relying c.RequestCtx().Conn() function is also consistent, so it would only require minor changes if that method is updated. Flushing data to the connection is also consistent, so no API updates would be needed there either.
Feature Examples
app.Use(func (c fiber.Ctx) error {
err:=c.Next()
iferr!=nil {
log.Println("Got error: %v", err)
returnc.SendString(err.Error()) // Will be unsuccessful since the response ended below
}
returnnil
})
app.Get("/hello", func (c fiber.Ctx) error {
query:=c.Query("name", "")
ifquery=="" {
c.SendString("You don't have a name?")
c.End() // Closes the underlying connectionreturnerrors.New("No name provided")
}
returnc.SendString("Hello, "+query+"!")
})
Feature Proposal Description
As brought up and discussed in #3252 (comment), this issue proposes to add the
End()
method to Ctx in v3 to allow instant flushing and closing of an active connection.While Ctx's
Drop()
method added in the issue mentioned above allows to close a connection before sending a response, this newEnd()
method would be to flush the current response before closing the connection.This feature would be useful for stopping middleware from controlling the connection after a handler further up the method chain. Some middleware (like fiberprometheus) will work with a response after calling
c.Next()
. In addition, Fiber's default error handler may also write to the response, while a user may want to handle an error in different ways from within specific handlers and only log detailed errors internally.It also can be an alternative to simply using
return nil
at the end of a handler, as done in ExpressJS (see below).Alignment with Express API
The Express API provides an equivalent method
res.end()
:Calling
res.end()
closes the connection after flushing the response.HTTP RFC Standards Compliance
N/A
API Stability
ExpressJS uses
res.end()
, so the API will be unlikely to be changed to match Express.In addition, the relying
c.RequestCtx().Conn()
function is also consistent, so it would only require minor changes if that method is updated. Flushing data to the connection is also consistent, so no API updates would be needed there either.Feature Examples
Checklist:
The text was updated successfully, but these errors were encountered: