gin笔记
2022-03-30 20:01:00 6 举报
个人参考网上资料和源码整理笔记
作者其他创作
大纲/内容
2
关联
中间件next()实现
type node struct { path string indices string children []*node handlers HandlersChain priority uint32 nType nodeType maxParams uint8 wildChild bool fullPath string}
path:\"zs\"indices:\"\"fullpath: \"/zs\"handles: 1.Logger()2.Recovery()3.zsfun()
engine := New()初始化engine
注意:定义:type HandlerFunc func(*Context) type HandlersChain []HandlerFunc语法糖:...HandlerFunc = HandlersChain
method: GETroot
树状图生成
本质都是调用group.handle
核心
调用
path:\"gs\"indices:\"\"fullpath: \"/gs\"handles: 1.Logger()2.Recovery()3.gsfun()
trees
return &Context{engine: engine}返回初始化的Context,并写入自己的指针
例子:e.GET(\"/config\
defer func() { debugPrintError(err) }()异常回调
if root == nil {root = new(node) root.fullPath = \"/\
type methodTrees []methodTree每种请求类型都有自己的方法树
1
type methodTree struct{ method string root *node }
gin.Default()
root节点
debugPrint(\"Listening and serving HTTP on %s\\
absolutePath := group.calculateAbsolutePath(relativePath)路径拼接
engine := &Engine{RouterGroup..}初始化RouterGroup结构体和一系列参数
大概流程
root := engine.trees.get(method)根据请求方法名在方法树切片寻找
engine.RouterGroup.engine = engine把自己的指针写入自己的RouterGroup的engine属性
method: POSTroot
debugPrintWARNINGDefault()校验版本
finalSize := len(group.Handlers) + len(handlers) //该路由总调用方法数 if finalSize >= int(abortIndex) {panic(\"too many handlers\") }//限制该路由最大调用方法数63
c := engine.pool.Get().(*Context)
engine.pool.Put(c)将context放回pool池
handlers = group.combineHandlers(handlers)将方法添加路由组并返回
net.Listen(\"tcp\
path:\"/\"indices:\"czg\"fullpath: \"/\"handles: nil
gin.Context可以获取req和返回的原因
const abortIndex int8 = math.MaxInt8 / 2
e.GET(\"/config\
生成的GET请求的方法树
Engine实现的ServeHTTP方法
engine.pool = sync.pool调用sync.Pool结构体的new()方法(存放可重用对象容器)
1.engine.pool.New = func() interface{} { 2.return engine.allocateContext()}将初始化的Context放入sync.pool池
go c.serve(connCtx)开启协程接收请求
engine.handleHTTPRequest(c)寻找并执行方法
path:\"4\"indices:\"\"fullpath: \"/config4\"handles: 1.Logger()2.Recovery()3.cfun4()
gin.Run()
path:\"config\"indices:\"4\"fullpath: \"/config\"handles: 1.Logger()2.Recovery()3.cfun()
0 条评论
下一页