英文:
What is the meaning of Go/Gin debug output - (x handlers)
问题
在以下的Go/Gin调试输出中,(5 handlers)的含义是什么?正如你所看到的,显然我有超过5个处理程序。
[GIN-debug] GET / --> .../handlers.APIDetail (5 handlers)
[GIN-debug] POST /signup --> .../handlers.SignUp (5 handlers)
[GIN-debug] POST /signin --> .../handlers.SignIn (5 handlers)
[GIN-debug] POST /refresh-token --> .../handlers.RefreshToken (5 handlers)
[GIN-debug] POST /verify-email --> .../handlers.VerifyEmailVerificationToken (5 handlers)
[GIN-debug] POST /resend-verification-email --> .../handlers.ResendEmailVerificationEmail (5 handlers)
[GIN-debug] POST /reset-password --> .../handlers.ResetPassword (5 handlers)
[GIN-debug] POST /change-password --> .../handlers.ChangePassword (5 handlers)
[GIN-debug] PATCH /users/me --> .../handlers.UpdateProfile (5 handlers)
[GIN-debug] POST /signout --> .../handlers.SignOut (5 handlers)
[GIN-debug] GET /orgs/:id --> .../handlers.GetOrganizations (5 handlers)
[GIN-debug] GET /orgs --> .../handlers.GetOrganizations (5 handlers)
在这里,(5 handlers)表示每个路由处理程序的数量。它告诉你在处理特定路由时,有多少个处理程序被调用。在这种情况下,每个路由都有5个处理程序。
英文:
In the following Go/Gin debug output, what is the meaning of (5 handlers). As you can see, I obviously have more than 5 handlers.
[GIN-debug] GET / --> .../handlers.APIDetail (5 handlers)
[GIN-debug] POST /signup --> .../handlers.SignUp (5 handlers)
[GIN-debug] POST /signin --> .../handlers.SignIn (5 handlers)
[GIN-debug] POST /refresh-token --> .../handlers.RefreshToken (5 handlers)
[GIN-debug] POST /verify-email --> .../handlers.VerifyEmailVerificationToken (5 handlers)
[GIN-debug] POST /resend-verification-email --> .../handlers.ResendEmailVerificationEmail (5 handlers)
[GIN-debug] POST /reset-password --> .../handlers.ResetPassword (5 handlers)
[GIN-debug] POST /change-password --> .../handlers.ChangePassword (5 handlers)
[GIN-debug] PATCH /users/me --> .../handlers.UpdateProfile (5 handlers)
[GIN-debug] POST /signout --> .../handlers.SignOut (5 handlers)
[GIN-debug] GET /orgs/:id --> .../handlers.GetOrganizations (5 handlers)
[GIN-debug] GET /orgs --> .../handlers.GetOrganizations (5 handlers)
答案1
得分: 1
每个路由的处理程序链中应该是处理程序的数量,即在将请求路由到特定端点时将执行的最大处理程序数量,包括中间件。
来自Gin源代码@latest的相关代码:
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
if IsDebugging() {
nuHandlers := len(handlers)
handlerName := nameOfFunction(handlers.Last())
if DebugPrintRouteFunc == nil {
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
} else {
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
}
}
}
如果在声明路由之前使用了一些全局中间件,例如使用router.Use
,并且没有路由具有每个路由中间件,那就解释了为什么数量始终相同。
例如,考虑以下代码:
r.Use(func(*gin.Context) { fmt.Println("FIRST") })
r.GET("/foo", func(c *gin.Context) {
c.Status(http.StatusOK)
})
r.Use(func(*gin.Context) { fmt.Println("SECOND") })
r.GET("/bar", func(c *gin.Context) {
c.Status(http.StatusOK)
})
这将打印:
[GIN-debug] GET /foo --> main.main.func2 (2 handlers)
[GIN-debug] GET /bar --> main.main.func4 (3 handlers)
因为/foo
的链中有FIRST
中间件和处理程序本身(2个),/bar
的链中有FIRST
和SECOND
中间件,以及处理程序本身(3个)。
英文:
It should be the number of handlers in each route's handler chain, i.e. the maximum number of handlers, including middleware, that will be executed when a request is routed to a certain endpoint.
Relevant code from Gin sources @latest:
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
if IsDebugging() {
nuHandlers := len(handlers)
handlerName := nameOfFunction(handlers.Last())
if DebugPrintRouteFunc == nil {
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
} else {
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
}
}
}
If you are setting a few global middlewares, e.g. with router.Use
, before declaring the routes, and no route has per-route middlewares, that explains why the number is always the same.
For example, consider the following:
r.Use(func(*gin.Context) { fmt.Println("FIRST") })
r.GET("/foo", func(c *gin.Context) {
c.Status(http.StatusOK)
})
r.Use(func(*gin.Context) { fmt.Println("SECOND") })
r.GET("/bar", func(c *gin.Context) {
c.Status(http.StatusOK)
})
This prints:
[GIN-debug] GET /foo --> main.main.func2 (2 handlers)
[GIN-debug] GET /bar --> main.main.func4 (3 handlers)
Because /foo
's chain has the FIRST
middleware, and the handler itself (2), and /bar
's chain has both FIRST
and SECOND
middleware, plus the handler itself (3).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论