当前位置: 首页 > news >正文

Gin 源码概览 - 路由

本文基于gin 1.1 源码解读
https://github.com/gin-gonic/gin/archive/refs/tags/v1.1.zip

1. 注册路由

我们先来看一段gin代码,来看看最终得到的一颗路由树长啥样

func TestGinDocExp(t *testing.T) {engine := gin.Default()engine.GET("/api/user", func(context *gin.Context) {fmt.Println("api user")})engine.GET("/api/user/info/a", func(context *gin.Context) {fmt.Println("api user info a")})engine.GET("/api/user/information", func(context *gin.Context) {fmt.Println("api user information")})engine.Run()
}

看起来像是一颗前缀树,我们后面再仔细深入源码

1.1 gin.Default

gin.Default() 返回了一个Engine 结构体指针,同时添加了2个函数,LoggerRecovery

func Default() *Engine {engine := New()engine.Use(Logger(), Recovery())return engine
}

New方法中初始化了 Engine,同时还初始化了一个RouterGroup结构体,并将Engine赋值给RouterGroup,相当于互相套用了

func New() *Engine {engine := &Engine{RouterGroup: RouterGroup{Handlers: nil,  // 业务handle,也可以是中间件basePath: "/",  // 根地址root:     true, // 根路由},RedirectTrailingSlash:  true,RedirectFixedPath:      false,HandleMethodNotAllowed: false,ForwardedByClientIP:    true,trees:                  make(methodTrees, 0, 9), // 路由树}engine.RouterGroup.engine = engine// 这里使用pool来池化Context,主要目的是减少频繁创建和销毁对象带来的内存分配和垃圾回收的开销engine.pool.New = func() interface{} {   return engine.allocateContext()}return engine
}

在执行完gin.Defualt后,gin的内容,里面已经默认初始化了2个handles

1.2 Engine.Get

在Get方法内部,最终都是调用到了group.handle方法,包括其他的POST,DELETE等

  • group.handle
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes {// 获取绝对路径, 将group中的地址和当前地址进行组合absolutePath := group.calculateAbsolutePath(relativePath)// 将group中的handles(Logger和Recovery)和当前的handles合并handlers = group.combineHandlers(handlers)   // 核心在这里,将handles添加到路由树中group.engine.addRoute(httpMethod, absolutePath, handlers)return group.returnObj()
}
  • group.engine.addRoute
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {// 通过遍历trees中的内容,判断是否在这之前,同一个http方法下已经添加过路由// trees 是一个[]methodTree  切片,有2个字段,method 表示方法,root 表示当前节点,是一个node结构体root := engine.trees.get(method)  // 如果这是第一个路由则创建一个新的节点if root == nil {root = new(node)engine.trees = append(engine.trees, methodTree{method: method, root: root})}root.addRoute(path, handlers)
}
  • root.addRoute(path, handlers)

这里的代码比较多,其实大家可以简单的认为就是将handlepath进行判断,注意这里的path不是一个完整的注册api,而是去掉了公共前缀后的那部分字符串。

func (n *node) addRoute(path string, handlers HandlersChain) {fullPath := path               // 存储当前节点的完整路径n.priority++                   // 优先级自增1numParams := countParams(path) // 统计当前节点的动态参数个数// non-empty tree,非空路由树if len(n.path) > 0 || len(n.children) > 0 {walk:for {// Update maxParams of the current node// 统计当前节点及子节点中最大数量的参数个数// maxParams 可以快速判断当前节点及其子树是否能匹配包含一定数量参数的路径,从而加速匹配过程。(GPT)if numParams > n.maxParams {n.maxParams = numParams}// Find the longest common prefix.// This also implies that the common prefix contains no ':' or '*'// since the existing key can't contain those chars.// 计算最长公共前缀i := 0max := min(len(path), len(n.path))for i < max && path[i] == n.path[i] {i++}// Split edge,当前路径和节点路径只有部分重叠,且存在分歧if i < len(n.path) {// 将后缀部分创建一个新的节点,作为当前节点的子节点。child := node{path:      n.path[i:],wildChild: n.wildChild,indices:   n.indices,children:  n.children,handlers:  n.handlers,priority:  n.priority - 1,}// Update maxParams (max of all children)// 路径分裂时,确保当前节点及子节点中是有最大的maxParamsfor i := range child.children {if child.children[i].maxParams > child.maxParams {child.maxParams = child.children[i].maxParams}}n.children = []*node{&child}// []byte for proper unicode char conversion, see #65n.indices = string([]byte{n.path[i]})n.path = path[:i]n.handlers = niln.wildChild = false}// Make new node a child of this nodeif i < len(path) {path = path[i:] // 获取当前节点中非公共前缀的部分// 当前节点是一个动态路径// TODO 还需要好好研究一下if n.wildChild {n = n.children[0]n.priority++// Update maxParams of the child nodeif numParams > n.maxParams {n.maxParams = numParams}numParams--// Check if the wildcard matches// 确保新路径的动态部分与已有动态路径不会冲突。if len(path) >= len(n.path) && n.path == path[:len(n.path)] {// check for longer wildcard, e.g. :name and :namesif len(n.path) >= len(path) || path[len(n.path)] == '/' {continue walk}}panic("path segment '" + path +"' conflicts with existing wildcard '" + n.path +"' in path '" + fullPath + "'")}// 获取非公共前缀部分的第一个字符c := path[0]// slash after param// 当前节点是动态参数节点,且最后一个字符时/,同时当前节点还只有一个字节if n.nType == param && c == '/' && len(n.children) == 1 {n = n.children[0]n.priority++continue walk}// Check if a child with the next path byte existsfor i := 0; i < len(n.indices); i++ {if c == n.indices[i] {i = n.incrementChildPrio(i)n = n.children[i]continue walk}}// Otherwise insert itif c != ':' && c != '*' {// []byte for proper unicode char conversion, see #65n.indices += string([]byte{c})child := &node{maxParams: numParams,}n.children = append(n.children, child)// 增加当前子节点的优先级n.incrementChildPrio(len(n.indices) - 1)n = child}n.insertChild(numParams, path, fullPath, handlers)return} else if i == len(path) { // Make node a (in-path) leafif n.handlers != nil {panic("handlers are already registered for path ''" + fullPath + "'")}n.handlers = handlers}return}} else { // Empty treen.insertChild(numParams, path, fullPath, handlers)n.nType = root}
}

1.3 Engine.Run

func (engine *Engine) Run(addr ...string) (err error) {defer func() { debugPrintError(err) }()// 处理一下web的监听地址address := resolveAddress(addr)debugPrint("Listening and serving HTTP on %s\n", address)// 最终还是使用http来启动了一个web服务err = http.ListenAndServe(address, engine)return
}

2. 路由查找

在上一篇文章中介绍了,http 的web部分的实现,http.ListenAndServe(address, engine) 在接收到请求后,最终会调用engineServeHTTP方法

func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {// 从context池中获取一个Contextc := engine.pool.Get().(*Context)  // 对Context进行一些初始值操作,比如赋值w和reqc.writermem.reset(w)c.Request = reqc.reset()// 最终进入这个方法来处理请求engine.handleHTTPRequest(c)// 处理结束后将Conetxt放回池中,供下一次使用engine.pool.Put(c)
}

2.1 engine.handleHTTPRequest()

func (engine *Engine) handleHTTPRequest(context *Context) {httpMethod := context.Request.Method  // 当前客户端请求的http 方法path := context.Request.URL.Path // 查询客户端请求的完整请求地址// Find root of the tree for the given HTTP methodt := engine.treesfor i, tl := 0, len(t); i < tl; i++ {if t[i].method == httpMethod {root := t[i].root// Find route in treehandlers, params, tsr := root.getValue(path, context.Params)if handlers != nil {context.handlers = handlers  // 所有的handles 请求对象context.Params = params      // 路径参数,例如/api/user/:id , 此时id就是一个路径参数context.Next()  			 // 执行所有的handles方法context.writermem.WriteHeaderNow()return}}}// 这里客户端请求的地址没有匹配上,同时检测请求的方法有没有注册,若没有注册过则提供请求方法错误if engine.HandleMethodNotAllowed {for _, tree := range engine.trees {if tree.method != httpMethod {if handlers, _, _ := tree.root.getValue(path, nil); handlers != nil {context.handlers = engine.allNoMethodserveError(context, 405, default405Body)return}}}}// 路由地址没有找到context.handlers = engine.allNoRouteserveError(context, 404, default404Body)
}

2.2 context.Next()

这个方法在注册中间件的使用会使用的较为频繁

func (c *Context) Next() {// 初始化的时候index 是 -1c.index++s := int8(len(c.handlers))for ; c.index < s; c.index++ {c.handlers[c.index](c)  // 依次执行注册的handles}
}

我们来看一段gin 是执行中间件的流程

func TestGinMdls(t *testing.T) {engine := gin.Default()engine.Use(func(ctx *gin.Context) {fmt.Println("请求过来了")// 这里可以做一些横向操作,比如处理用户身份,cors等ctx.Next()fmt.Println("返回响应")})engine.GET("/index", func(context *gin.Context) {fmt.Println("index")})engine.Run()
}

curl http://127.0.0.1:8080/index

通过响应结果我们可以分析出,请求过来时,先执行了Use中注册的中间件,然后用户调用ctx.Next() 可以执行下一个handle,也就是用户注册的/index方法的handle

http://www.lryc.cn/news/524626.html

相关文章:

  • 第6章 ThreadGroup详细讲解(Java高并发编程详解:多线程与系统设计)
  • CentOS 7乱码问题如何解决?
  • JavaScript语言的多线程编程
  • OpenSeaOtter使用手册-变更通知和持续部署
  • (2)STM32 USB设备开发-USB虚拟串口
  • 他把智能科技引入现代农业领域
  • ingress-nginx代理tcp使其能外部访问mysql
  • 麒麟操作系统服务架构保姆级教程(十三)tomcat环境安装以及LNMT架构
  • 亚博microros小车-原生ubuntu支持系列:4-手部检测
  • 关于回调函数(callback)
  • Linux Bash 中使用重定向运算符的 5 种方法
  • 硬件作品3----STM32F103RCT6最小系统板MCU配置
  • 人脸识别打卡系统--基于QT(附源码)
  • 【深度学习入门】深度学习知识点总结
  • 通过视觉语言模型蒸馏进行 3D 形状零件分割
  • 机器学习10-解读CNN代码Pytorch版
  • 微服务学习-Gateway 统一微服务入口
  • 2025寒假备战蓝桥杯02---朴素二分查找升级版本的学习+分别求解左右端点
  • PHP语言的软件工程
  • linux-FTP服务配置与应用
  • 靠右行驶数学建模分析(2014MCM美赛A题)
  • (1)STM32 USB设备开发-基础知识
  • Spring中如何动态的创建、监听MQ以及创建Exchange
  • 中国综合算力指数(2024年)报告汇总PDF洞察(附原数据表)
  • 【Python项目】小区监控图像拼接系统
  • 常用排序算法之插入排序
  • Elasticsearch(ES)基础查询语法的使用
  • 一篇文章学会Milvus【Docker 中运行 Milvus(Windows),Python实现对Milvus的操作,源代码案例,已经解决巨坑】【程序员猫爪】
  • 前端之移动端
  • 记一次 SpringBoot 启动慢的问题