High latency route #2246
Replies: 1 comment 2 replies
-
TLDR: Echo serves by default all errors as JSON responses (from default global error handler) and JSON serialization has its cost. Other examples write response with status code for 404 directly as string in handler to the client. Longer: You are somewhat comparing apples to oranges. Concept of Echo handlers and middlewares is that when something wrong/unexpected happens you return an error and let calling middleware or eventually global error handler to handle it. Default global error handler returns errors responses to the client as JSON. Returning HTTP status code and response body in handler (as so other examples use
httplog middleware is calling global error handler here and doing the serialization if err := next(c); err != nil {
c.Error(err) // <-- error gets written to the client as JSON. middlewares up in chain are screwed now if they are intended to interact with errors
} NB: You must add warning somewhere that by using NB: p.s. these examples are butchering how Echo/Gin etc handlers are written. This is more Echolike example func happyHandler(c echo.Context) error {
return c.String(http.StatusOK, "I am happy!")
}
func routeWithError(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "I am unhappy!")
} using Gin example: var happyHandler = func(c *gin.Context) {
fmt.Println("I am happy handler")
c.Writer.Write([]byte("I am happy!"))
} should be var happyHandler = func(c *gin.Context) {
fmt.Println("I am happy handler")
c.String(http.StatusOK, "I am happy!")
} or how Gin handles middleware execution chains and errors g.GET("/error", func(c *gin.Context) {
c.AbortWithError(http.StatusNotFound, errors.New("I am unhappy!"))
}) etc. I think these examples should be idiomatic to their frameworks/libraries way of doing things p.s.s. I think you should not use g.NoRoute(func(c *gin.Context) {
c.String(http.StatusNotFound, "custom not Found")
}) NB: Echo has also concept of handling cases for when no route is found. e.RouteNotFound("/*", func(c echo.Context) error {
return c.String(http.StatusNotFound, "custom not found")
}) so usage of |
Beta Was this translation helpful? Give feedback.
-
I came across httplog with examples of different routes framework:
https://github.com/MadAppGang/httplog/tree/main/examples/echo
On macOS with M1, I've found echo returns 404 for a /not_found is of >100 microseconds which is significant large than other framework. I presume it could be helpful to improve latency for logging.
alice
28.708µs
chi
17.708µs
echo
139.792µs
gin
26.709µs
goji
21.542µs
httprouter
17µs
gorilla
27.375µs
negroni
34.666µs
Beta Was this translation helpful? Give feedback.
All reactions