Detailed explanation of Gin Engine source code

Release: 2023-08-04 17:41:43
forward
745 people have browsed it

What is gin?

Gin is an HTTP web framework written in Go (Golang). It has a Martini-like API but performs 40 times faster than Martini. If you need extreme performance, use Gin.

gin Why is it fast?

Let’s take a look at the official benchmark test firstAddress[1]

Detailed explanation of Gin Engine source code

This is just one of them Partially, you can view the complete version by yourself at the above address.

We can see from the performance test that gin is indeed much faster than other web frameworks in processing request performance.

Performance issues are mainly caused by using httprouter[2], while httprouter uses Trie Tree (dictionary tree) as Path storage structure with high-performance lookup.

gin.Engine source code

// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
// Create an instance of Engine, by using New() or Default()
type Engine struct {
   RouterGroup       // 用于注册根路由组使用
    ...
    pool             sync.Pool    // 用于 gin.context 对象池
    trees            methodTrees  // 用于保存的请求路径
    ...
}
Copy after login

In gin’s Engine, the most important ones are these three fields.

gin.RouterGroup 源码

// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)

// HandlersChain defines a HandlerFunc array.
type HandlersChain []HandlerFunc

// RouterGroup is used internally to configure router, a RouterGroup is associated with
// a prefix and an array of handlers (middleware).
type RouterGroup struct {
    Handlers HandlersChain
    basePath string
    engine   *Engine
    root     bool
}
Copy after login

RouterGroupHandlers存放的是当前分组的所有中间件,也可以说:请求到这个分组,那么Handlers 中的函数都会根据索引从小到大的按顺序执行。

basePath 就是存放这个分组的基础路由路径。可能直接解释过于抽象;我们来分析它的调用链来看:

  1. Detailed explanation of Gin Engine source code
  2. Detailed explanation of Gin Engine source code
  3. Detailed explanation of Gin Engine source code

gin.New() 函数中我们可以看到 basePath 默认是 /Detailed explanation of Gin Engine source code

而新的RouterGroup是通过Engine调用Group函数产生的;也就是我们自己的Group

Detailed explanation of Gin Engine source code

那么以后在这个Group注册的路径都会自动加上/user的路径。

为什么会自动添加路径,后面还会分析相关细节。

gin.methodTree 源码

type node struct {
   path      string
   indices   string
   wildChild bool
   nType     nodeType
   priority  uint32
   children  []*node // child nodes, at most 1 :param style node at the end of the array
   handlers  HandlersChain
   fullPath  string
}

type methodTree struct {
    method string
    root   *node
}

type methodTrees []methodTree
Copy after login

对于node的添加和查找我就不展开讲了,如果不懂的可以自行搜索字典树和查看 httprouter[3] 的实现。

我们刚刚在gin.New 函数中可以看到,trees是一个methodTree的切片,并在在初始化给了默认容量为9是容量不是长度哦

Detailed explanation of Gin Engine source code

addRoute 函数中可以看到trees的添加:

Detailed explanation of Gin Engine source code

If there is no root path node object corresponding to the method, one will be created, and then the corresponding route registration will be added.

Overall process analysis

In the gin framework, the following registration methods can be mainly used:

  • The name of the corresponding http request method (get,post,put...)
  • Reduced by Group group routing Fill in the path
  • Register allhttpmethod routes throughAny

These registrations are below In fact, they all call the same method. Next, we will open her coat and see what it looks like inside. Detailed explanation of Gin Engine source code

You can see that they are all called group.handle MethodDetailed explanation of Gin Engine source code

Let’s take a look at what this method does:

  • Calculate the path of routing group.calculateAbsolutePath(relativePath)
  • Add a middleware function to the currently registered path and register it The function is placed at the end
  • Add to the global routing table
  • Returns an IRoutesInterface

combineHandlers The maximum number of functions supported is 63-1Detailed explanation of Gin Engine source code

Group function

Now let’s look at the implementation of Group. By calling this method, a new RouterGroup will be generated and based on the basic path set by the parameters, and the global engine Entity and copy the basic middleware into it.

Detailed explanation of Gin Engine source code

Using group routing finally calls the group.handle method to register, but the basePath has been set during grouping. Okay, adding the path when registering the function is the routing path of this request.

One thing to note is that after I used ·Group· before, the subsequent registration did not use the return value. How the registered route was registered in the global routing table, I only understood it after reading the source code.

Detailed explanation of Gin Engine source code

添加路由的时候,是获取的全局engine 实体,所以也是添加到全局路由表中的。

最后需要注意的

在使用注册中间件和注册路由的时候,需要注意他们注册的顺序。

上一点错误的注册方式代码:

package main

import (
 "github.com/gin-gonic/gin"
)

func main() {
 eng := gin.New()
 eng.POST("login", func(context *gin.Context) {
  // 处理登录信息
 })
 eng.Use(gin.Logger())

 userGroup := eng.Group("/user")
 userGroup.GET("info", func(context *gin.Context) {
  // 参数验证

  // 处理逻辑
 })

 adminGroup := eng.Group("/admin")
 adminGroup.GET("info", func(context *gin.Context) {
  // 参数验证

  // 处理逻辑
 })

 eng.Use(gin.Recovery())
 adminGroup.PUT("info", func(context *gin.Context) {
  // 参数验证

  // 处理逻辑
 })
 eng.GET("logout", func(context *gin.Context) {
  // 处理登录信息
 })
 eng.Run(":8080")
}
Copy after login

运行结果:

Detailed explanation of Gin Engine source code

可以看到在注册路由之后,再注册中间件,那么前面注册过的路由是没有这个中间件的。

The above is the detailed content of Detailed explanation of Gin Engine source code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
gin
source:Golang菜鸟
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