Home >Backend Development >Golang >Detailed explanation of Gin Engine source code
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.
Let’s take a look at the official benchmark test firstAddress[1]
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.
// 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 // 用于保存的请求路径 ... }
In gin’s Engine
, the most important ones are these three fields.
// 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 }
在RouterGroup
中Handlers
存放的是当前分组的所有中间件,也可以说:请求到这个分组,那么Handlers
中的函数都会根据索引从小到大的按顺序执行。
basePath
就是存放这个分组的基础路由路径。可能直接解释过于抽象;我们来分析它的调用链来看:
在gin.New()
函数中我们可以看到 basePath
默认是 /
而新的RouterGroup
是通过Engine
调用Group
函数产生的;也就是我们自己的Group
。
那么以后在这个Group
注册的路径都会自动加上/user
的路径。
为什么会自动添加路径,后面还会分析相关细节。
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
对于node
的添加和查找我就不展开讲了,如果不懂的可以自行搜索字典树
和查看 httprouter[3] 的实现。
我们刚刚在gin.New
函数中可以看到,trees
是一个methodTree
的切片,并在在初始化给了默认容量为9
,是容量不是长度哦。
在addRoute
函数中可以看到trees
的添加:
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.
In the gin
framework, the following registration methods can be mainly used:
get,post,put...
)Group
group routing Fill in the pathhttp
method 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.
You can see that they are all called group.handle
Method
Let’s take a look at what this method does:
group.calculateAbsolutePath(relativePath)
IRoutes
InterfacecombineHandlers
The maximum number of functions supported is 63-1
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.
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.
添加路由的时候,是获取的全局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") }
运行结果:
可以看到在注册路由之后,再注册中间件,那么前面注册过的路由是没有这个中间件的。
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!