Home > Backend Development > Golang > What does Go/Gin debug output mean - (x handlers)

What does Go/Gin debug output mean - (x handlers)

WBOY
Release: 2024-02-09 08:15:19
forward
543 people have browsed it

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

php editor Youzi will explain to you the meaning of "(x handlers)" in the Go/Gin debugging output. In the Gin framework of the Go language, "(x handlers)" represents the number of middleware that the request passes through. Middleware is a mechanism in the Gin framework for processing requests. It can perform some operations before or after the request reaches the routing processing function. Each middleware passes the request to the next middleware until it finally reaches the handler function. The "x" in "(x handlers)" represents the number of middlewares, which can help developers understand how many middlewares have been processed during request processing. By observing the number of middlewares, developers can better understand the request processing process and facilitate debugging and optimizing the code. Hope this answer helps you!

Question content

In the go/gin debug output below, 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)
Copy after login

Workaround

It should be the number of handlers in the handler chain for each route, i.e. the processing that will be performed when a request is routed to an endpoint The maximum number of programs (including middleware).

Relevant code comes from gin source @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)
        }
    }
}
Copy after login

If you are setting up some global middleware, for example using router.use, before declaring the route, and no route has middleware for each route, this 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)
    })
Copy after login

Print:

[GIN-debug] GET    /foo    --> main.main.func2 (2 handlers)
[GIN-debug] GET    /bar    --> main.main.func4 (3 handlers)
Copy after login

Because /foo's chain has first middleware and the handler itself (2), while /bar's chain has first and second middleware, plus the handler itself (3).

The above is the detailed content of What does Go/Gin debug output mean - (x handlers). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template