“server.serve => conn.serve ==>defer cancleCtx/ w.cancelCtx() ”node
所以, 最要紧的就是看tree.go里面定义的数据结构。曹大的图能够帮助理解。python
- 具体的路由规则要烂熟,由此解读node的几个类型
- priority/wildchild/indices的意思和做用
- test文件的测试有没有没覆盖到的状况?
type node struct { path string indices string wildChild bool nType nodeType priority uint32 children []*node handle Handle }
-pathgolang
一截儿path,绝非全路径,这点能够肯定。具体怎么表述?
// incrementChildPrio if newPos != pos { n.indices = n.indices[:newPos] + n.indices[pos:pos+1] + n.indices[newPos:pos] + n.indices[pos+1:] }
可见每一个node为chidren指定惟一的一字节字符,和排好序的chidren slice一一对应。问题是怎么指定的呢?
是什么?干吗的?
干吗的?
- 事实上,在router.go中,只用到了addRoute为咱们的router添加路由以及对应的handle, insertChild为addRoute服务。
- 进一步的事实是,调用addRoute的老是根节点root.
// router.go Handle if root == nil { root = new(node) r.trees[method] = root // 其余逻辑 ]}
// tree.go addRoute fullpath := path n.priority ++ //Empty tree if len(n.path) == 0 && len(n.indices) == 0 { n.insertChild(path, fullpath, handle) n.nType == root // 常量: root return }
- 没有“:”“*”都好说,直接:
//insertChild n.path = path n.handle = handle
- 有呢?有点复杂。将状况简单化,若是以前是空树的话(如今要有第一个节点了),咱们想一想,有“:”“*”有什么说头?
- 问题一: 遇到无效的wildcard怎么处理?
- 问题二:可能要设置wildcard冲突,怎么设置呢?
if !valid { panic("only one wildcard per path segment is allowed, has: '" + wildcard + "' in path '" + fullPath + "'") }
if wildcard[0] == ':' { // param if i > 0 { // Insert prefix before the current wildcard n.path = path[:i] path = path[i:] } n.wildChild = true child := &node{ nType: param, path: wildcard, } n.children = []*node{child} n = child n.priority++ // If the path doesn't end with the wildcard, then there will be another non-wildcard subpath starting with '/' if len(wildcard) < len(path) { path = path[len(wildcard):] child := &node{ priority: 1, } n.children = []*node{child} n = child continue } // Otherwise we're done. Insert the handle in the new leaf n.handle = handle return }
if i > 0 { n.path = path[:i] path = path[i:] }
n.wildchild = true
child := &node{ nType: param, path: wildcard, } n.children = []*node{child} n = child n.priority++
n.handle = handle return
if len(wildcard) < len(path) { path = path[len(wildcard):] child := &node{ priority: 1, } n.children = []*node{child} n = child continue }
if i+len(wildcard) < len(path) { panic("catch-all routes are only allowed at the end of the path in path '" + fullPath + "'") }
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' { panic("catch-all conflicts with existing handle for the path segment root in path '" + fullPath + "'") }
i-- if path[i] != '/' { panic("no / before catch-all in path '" + fullPath + "'")}
小结一下: 调用插入方法的node的path会是参数path的前缀,是空串最保险了。参数path不会以“*”开头, 以“:”开头是可能的。node不会是param类型(catch也不会)。
n.path = path[:i] // First node: catchAll node with empty path child := &node{ wildChild: true, nType: catchAll, } n.children = []*node{child} n.indices = string('/') n = child n.priority++ // Second node: node holding the variable child = &node{ path: path[i:], nType: catchAll, handle: handle, priority: 1, } n.children = []*node{child} return
回到addRoute吧。数据结构
// 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 := longestCommonPrefix(path, n.path)
再回顾一遍insertChild方法: 设想向空树插入:“/:name” , ":name", "/*catchall", 根结点不会带“:”“*”,要么空串, 要么"/"。注意插入“*catchall”非法(index out of range!!!)。能够理解他的注释。
if i < len(n.path) { child := node{ path: n.path[i:], wildChild: n.wildChild, nType: static, indices: n.indices, children: n.children, handle: n.handle, priority: n.priority - 1, } n.children = []*node{&child} // []byte for proper unicode char conversion, see #65 n.indices = string([]byte{n.path[i]}) n.path = path[:i] n.handle = nil n.wildChild = false }
-n.indices的含义。从上能够瞥见一点端倪。记得咱们说过,n.indices 和 n.children一一对应?这里n.indices就是这个child的开头字母,正如其名,起到的是索引的做用。架构
if n.handle != nil { panic("a handle is already registered for path '" + fullPath + "'") } n.handle = handle
if i < len(path) { path = path[i:] // 节点n的子节点是通配,杀伤力比较大,先掌权,后清洗 if n.wildchild { // 1. 掌权 // 2.清洗,生存者能够进入下一轮。 // 思考: // 1. 什么样的能逃过清洗? // 2. 进入下一轮的初始状态是怎样的: 虚拟根结点 } // 如今面临的状况:n的孩子节点没有param或catchall //可是n自己多是 idxc := path[0] // 1. 一种简单的状况的处理 n是param,只有一个孩子 // 那必然是“/”开头的或空串,直接重走循环 // 2. n的某个孩子和path有公共前缀,提高priority并重走循环 // 3.其余状况插入新的子节点,使树增加 // 如今的情形是: n.path 和 path 绝对并无公共前缀了 // 3.1 等待拼接的全新path不以":" "*" 打头,须要作一点工做 // 插入空节点,做用不明, 保护做用? // 结合insertChild的状况,有个状况能够排除: // 若是 idxc是“:” "*" 打头的,n必然没孩子 // 若是n是param, path是普通字母打头的呢?按理来讲,这是个不合法现象。这在上面的判断(wildcard冲突判断中)已经枪毙。 // 建立孩子(空串),插入此节点。 n.insertChild(path, fullpPath, handle) } }
- 公共前缀不为空或n.path为空串
- 若n为param(模拟根结点没法作到的), path必定是“:param” 或 “:param/” 或 “/”打头三种状况之一。
- param子节点如有必定是独生子!!
- param没孩子的话,要加一个空串“”
源码参考:app
if i < len(path) { path = path[i:] if n.wildchild { n = n.children[0] n.prority ++ // Check if the wildcard mathes if len(path) >= len(n.path) && n.path == path[:len(n.path)] && n.nType != catchAll && (len(n.path) >= len(path) || path[len(n.path)] == "/") { continue walk } else { pathSeg := path if n.nType != cathAll { pathSeg = strings.SplitN(pathSeg, "/", 2)[0] } prefix := fullPath[:strings.Index(fullPath, pathSeg)] + n.path panic("'" + pathSeg + "' in new path '" + fullPath + "' conflicts with existing wildcard '" + n.path + "' in existing prefix '" + prefix + "'") } } idxc := path[0] if n.nType == param && idxc == "/" && len(n.children) == 1 { n = n.children[0] n.prority++ continue walk } for i, c := range []byte(n.indices) { if c == idxc { i = n.incrementChildPrio(i) n = n.children[i] continue walk } } if idxc != ':' && idxc != '*' { n.indices += string([]byte{idxc}) child := &node{} n.children = append(n.children, child) n.incrementChildPrio(len(n.indices) - 1) n = child } n.insertChild(path, fullpPath, handle) }
做用猜测:根据树,匹配手头的path,把全部参数取出来,放在从sync.Pool里拿出临时对象[]Params, 不断把解析的param加入。 catchall怎么处理?拭目以待!函数