Go/Gin调试输出中”(x handlers)”的含义是什么?

huangapple go评论72阅读模式
英文:

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的链中有FIRSTSECOND中间件,以及处理程序本身(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).

huangapple
  • 本文由 发表于 2023年1月20日 07:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75179363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定