目录
一. httprouter 基础
- 通过前面几篇文章我们了解了标准库net包下的http服务实现,我们了解到了多路复用器ServeMux,了解到了Header与ServeHTTP()
- 但是标准库中的http有很多不足
- 不能单独的对请求方法(POST,GET等)注册特定的处理函数
- 不支持Path变量参数
- 不能自动对Path进行校准
- 性能一般并且扩展性不足
- httprouter 是一个高性能,可扩展的HTTP路由,解决了上面列举的net/http默认路由的不足,有一下特性
- 仅提供精准匹配
- 对结尾斜杠 / 自动重定向
- 路径自动修正
- 路由参数
- 错误处理
- 一个简单示例
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
//接口函数
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
//接口函数
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func main() {
//1.初始化路由
router := httprouter.New()
//2.注册路由接口
router.GET("/", Index)
//3.注册路由接口
router.GET("/hello/:name", Hello)
//4.监听端口启动服务
log.Fatal(http.ListenAndServe(":8080", router))
}
二. httprouter.New() 初始化路由
- httprouter.New()跟踪进去可以发现两个重要的struct,一个是Router,一个是node
// Router 是一个 http.Handler 可以通过定义的路由将请求分发给不同的函数
type Router struct {
trees map[string]*node
// 这个参数是否自动处理当访问路径最后带的 /,一般为 true 就行。
// 例如: 当访问 /foo/ 时, 此时没有定义 /foo/ 这个路由,但是定义了
// /foo 这个路由,就对自动将 /foo/ 重定向到 /foo (GET 请求
// 是 http 301 重定向,其他方式的请求是 http 307 重定向)。
RedirectTrailingSlash bool
// 是否自动修正路径, 如果路由没有找到时,Router 会自动尝试修复。
// 首先删除多余的路径,像 ../ 或者 // 会被删除。
// 然后将清理过的路径再不区分大小写查找,如果能够找到对应的路由, 将请求重定向到
// 这个路由上 ( GET 是 301, 其他是 307 ) 。
RedirectFixedPath bool
// 用来配合下面的 MethodNotAllowed 参数。
HandleMethodNotAllowed bool
// 如果为 true ,会自动回复 OPTIONS 方式的请求。
// 如果自定义了 OPTIONS 路由,会使用自定义的路由,优先级高于这个自动回复。
HandleOPTIONS bool
// 路由没有匹配上时调用这个 handler 。
// 如果没有定义这个 handler ,就会返回标准库中的 http.NotFound 。
NotFound http.Handler
// 当一个请求是不被允许的,并且上面的 HandleMethodNotAllowed 设置为 ture 的时候,
// 如果这个参数没有设置,将使用状态为 with http.StatusMethodNotAllowed 的 http.Error
// 在 handler 被调用以前,为允许请求的方法设置 "Allow" header 。
MethodNotAllowed http.Handler
// 当出现 panic 的时候,通过这个函数来恢复。会返回一个错误码为 500 的 http error
// (Internal Server Error) ,这个函数是用来保证出现 painc 服务器不会崩溃。
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
}
- node
type node struct {
// 当前节点的 URL 路径
// 如上面图中的例子的首先这里是一个 /
// 然后 children 中会有 path 为 [s, blog ...] 等的节点
// 然后 s 还有 children node [earch,upport] 等,就不再说明了
path string
// 判断当前节点路径是不是含有参数的节点, 上图中的 :post 的上级 blog 就是wildChild节点
wildChild bool
// 节点类型: static, root, param, catchAll
// static: 静态节点, 如上图中的父节点 s (不包含 handler 的)
// root: 如果插入的节点是第一个, 那么是root节点
// catchAll: 有*匹配的节点
// param: 参数节点,比如上图中的 :post 节点
nType nodeType
// path 中的参数最大数量,最大只能保存 255 个(超过这个的情况貌似太难见到了)
// 这里是一个非负的 8 进制数字,最大也只能是 255 了
maxParams uint8
// 和下面的 children 对应,保留的子节点的第一个字符
// 如上图中的 s 节点,这里保存的就是 eu (earch 和 upport)的首字母
indices string
// 当前节点的所有直接子节点
children []*node
// 当前节点对应的 handler
handle Handle
// 优先级,查找的时候会用到,表示当前节点加上所有子节点的数目
priority uint32
}
Router struct
- 了解一下Router内部结构
// Router 是一个 http.Handler 可以通过定义的路由将请求分发给不同的函数
type Router struct {
trees map[string]*node
// 这个参数是否自动处理当访问路径最后带的 /,一般为 true 就行。
// 例如: 当访问 /foo/ 时, 此时没有定义 /foo/ 这个路由,但是定义了
// /foo 这个路由,就对自动将 /foo/ 重定向到 /foo (GET 请求
// 是 http 301 重定向,其他方式的请求是 http 307 重定向)。
RedirectTrailingSlash bool
// 是否自动修正路径, 如果路由没有找到时,Router 会自动尝试修复。
// 首先删除多余的路径,像 ../ 或者 // 会被删除。
// 然后将清理过的路径再不区分大小写查找,如果能够找到对应的路由, 将请求重定向到
// 这个路由上 ( GET 是 301, 其他是 307 ) 。
RedirectFixedPath bool
// 用来配合下面的 MethodNotAllowed 参数。
HandleMethodNotAllowed bool
// 如果为 true ,会自动回复 OPTIONS 方式的请求。
// 如果自定义了 OPTIONS 路由,会使用自定义的路由,优先级高于这个自动回复。
HandleOPTIONS bool
// 路由没有匹配上时调用这个 handler 。
// 如果没有定义这个 handler ,就会返回标准库中的 http.NotFound 。
NotFound http.Handler
// 当一个请求是不被允许的,并且上面的 HandleMethodNotAllowed 设置为 ture 的时候,
// 如果这个参数没有设置,将使用状态为 with http.StatusMethodNotAllowed 的 http.Error
// 在 handler 被调用以前,为允许请求的方法设置 "Allow" header 。
MethodNotAllowed http.Handler
// 当出现 panic 的时候,通过这个函数来恢复。会返回一个错误码为 500 的 http error
// (Internal Server Error) ,这个函数是用来保证出现 painc 服务器不会崩溃。
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
}
- Router是 httprouter 的一个核心的部分,定义了路由的一些初始配置,在Router中存在一个 trees 属性,用来保存注册的路由,可以这样说了解httprouter实际就是了解Router中通过trees 保存路由与路由发现的过程
- Router 也是基于 http.Handler 做的实现,根据前面学习net/http我们了解到如果要实现 http.Handler 接口,就必须实现 ServeHTTP(w http.ResponseWriter, req *http.Request) 这个方法,ServeHTTP()实际就是接收请求,匹配到指定路由后执行的函数
三. 路由的注册(trees 树的构建过程)
- 我们可以追一下router.GET()或POST()等方法的源码,以GET方法为例,内部会调用Router的Handle()方法
//1.以get方法为例
func (r *Router) GET(path string, handle Handle) {
//方法内部会调用Router的Handle()方法
r.Handle("GET", path, handle)
}
- 查看Router的Handle()
- 该方法需要三个参数: method请求类型, path路径,Handle函数
- 该方法内首先会判断是否存在节点,若不存在执行new()初始化根节点,将Handle添加到根节点
- 如果存执行addRoute()将请求路径与Handle添加到trees 树中
func (r *Router) Handle(method, path string, handle Handle) {
if path[0] != '/' {
panic("path must begin with '/' in path '" + path + "'")
}
if r.trees == nil {
r.trees = make(map[string]*node)
}
// 从这里可以看出每种http方法(也可以是自定义的方法)都在同一颗树,作为不同根节点。
root := r.trees[method]
if root == nil {
// 初始化各个方法的根节点
root = new(node)
r.trees[method] = root
}
// addRoute方法是把handle添加到路径对应的节点上
root.addRoute(path, handle)
}
- addRoute()添加节点
func (n *node) addRoute(path string, handle Handle) {
fullPath := path
n.priority++
numParams := countParams(path)
// 此树非空
if len(n.path) > 0 || len(n.children) > 0 {
walk:
for {
// 更新当前节点最大的参数数量值
if numParams > n.maxParams {
n.maxParams = numParams
}
// 找到最长公共前缀
// 最长的公共前缀不包含 ':' 和 '*'
i := 0
// 获取当前节点和当前传入路径最小的字符数量
max := min(len(path), len(n.path))
// 寻找分裂点位置
for i < max && path[i] == n.path[i] {
i++
}
// 如果分裂字符的位置小于当前节点的n.path,那么需要从当前节点分裂出来一个子节点
// 比如: 当前节点是/search 输入的path是/support,则需要把/search分裂出一个子节点 earch
if i < len(n.path) {
child := node{
// 分裂出来的部分,比如/search 遇到 /support,先分裂出 earch
path: n.path[i:],
// 和原来的节点一致
wildChild: n.wildChild,
nType: static,
// 和原来的节点一致
indices: n.indices,
// 原来的节点的子节点变为分裂后节点的子节点
children: n.children,
handle: n.handle,
priority: n.priority - 1,
}
// 更新当前树枝下节点的最大参数数量值
for i := range child.children {
if child.children[i].maxParams > child.maxParams {
child.maxParams = child.children[i].maxParams
}
}
// 将分裂后的节点作为当前节点的子节点
n.children = []*node{&child}
// 存放子节点的第一个字符
n.indices = string([]byte{n.path[i]})
n.path = path[:i]
// 分裂后就没有handle了
n.handle = nil
n.wildChild = false
}
// 为当前节点添加新的子节点
if i < len(path) {
// 去除公共部分,剩余的字符
path = path[i:]
if n.wildChild {
n = n.children[0]
n.priority++
// 更新当前节点最大参数个数
if numParams > n.maxParams {
n.maxParams = numParams
}
numParams--
// 检查通配符是否匹配
if len(path) >= len(n.path) && n.path == path[:len(n.path)] &&
// 检查更长的通配符参数,例如::name和:names
(len(n.path) >= len(path) || path[len(n.path)] == '/') {
continue walk
} else {
// 通配符冲突
var pathSeg string
if n.nType == catchAll {
pathSeg = path
} else {
pathSeg = strings.SplitN(path, "/", 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 +
"'")
}
}
c := path[0]
// 参数后面的 /
if n.nType == param && c == '/' && len(n.children) == 1 {
n = n.children[0]
n.priority++
continue walk
}
// 检查是否有子路径
for i := 0; i < len(n.indices); i++ {
if c == n.indices[i] {
i = n.incrementChildPrio(i)
n = n.children[i]
continue walk
}
}
// 如果没有就插入
if c != ':' && c != '*' {
// unicode 转 string
n.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, handle)
return
} else if i == len(path) { 生成一个叶子节点
if n.handle != nil {
panic("a handle is already registered for path '" + fullPath + "'")
}
n.handle = handle
}
return
}
} else { // 空树
n.insertChild(numParams, path, fullPath, handle)
n.nType = root
}
}
- insertChild()
func (n *node) insertChild(numParams uint8, path, fullPath string, handle Handle) {
var offset int // 已经处理的字符位置
// 找到':' 或者 '*' 出现的位置
for i, max := 0, len(path); numParams > 0; i++ {
c := path[i]
if c != ':' && c != '*' {
continue
}
fmt.Println("c=", string(c))
// 在通配符往后以'/'或者路径结束的位置
end := i + 1
for end < max && path[end] != '/' {
switch path[end] {
// 通配符':' 或者'*' 后面不能再出现 ':', '*'
case ':', '*':
panic("only one wildcard per path segment is allowed, has: '" +
path[i:] + "' in path '" + fullPath + "'")
default:
end++
}
}
// 检查此节点是否有子节点
if len(n.children) > 0 {
panic("wildcard route '" + path[i:end] +
"' conflicts with existing children in path '" + fullPath + "'")
}
// 参数节点必须有名称
if end-i < 2 {
panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
}
if c == ':' { // 参数 :
// 分裂通配符路径
if i > 0 {
n.path = path[offset:i]
offset = i
}
child := &node{
nType: param,
maxParams: numParams,
}
n.children = []*node{child}
n.wildChild = true
n = child
n.priority++
numParams--
// 如果通配符后面还有路径
if end < max {
n.path = path[offset:end]
offset = end
child := &node{
maxParams: numParams,
priority: 1,
}
n.children = []*node{child}
n = child
}
} else { // catchAll
if end != max || numParams > 1 {
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 + "'")
}
n.path = path[offset:i]
child := &node{
wildChild: true,
nType: catchAll,
maxParams: 1,
}
n.children = []*node{child}
n.indices = string(path[i])
n = child
n.priority++
child = &node{
path: path[i:],
nType: catchAll,
maxParams: 1,
handle: handle,
priority: 1,
}
n.children = []*node{child}
return
}
}
n.path = path[offset:]
n.handle = handle
}
四. 接收到请求后的Handle路由获取
- ServeHTTP()实际就是接收请求,匹配到指定路由后执行的函数,查看Router实现的ServeHTTP(),了解一个接收请求后的执行流程
在ServeHTTP(),中会调用一个getValue()方法
// ServeHTTP makes the router implement the http.Handler interface.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.PanicHandler != nil {
defer r.recv(w, req)
}
//1.通过*http.Request获取到请求url
path := req.URL.Path
//2.通过*http.Request获取到请求方法例如GET
//通过拿到的请求方法在Router的trees中拿到对应的node
//也就是当前项目中使用同一个请求方法的所有url,与url对应的执行函数
if root := r.trees[req.Method]; root != nil {
//3.通过拿到的root也就是node,根据请求的url拿到对应该该url的handle
//也就是该url对应的执行函数(handle中保存了执行函数的地址)
//重点是root.getValue(path),根据请求url找到指定的节点
if handle, ps, tsr := root.getValue(path); handle != nil {
//4.将请求的req,res传递给执行函数开始执行
handle(w, req, ps)
return
} else if req.Method != http.MethodConnect && path != "/" {
code := 301 // Permanent redirect, request with GET method
if req.Method != http.MethodGet {
// Temporary redirect, request with same method
// As of Go 1.3, Go does not support status code 308.
code = 307
}
if tsr && r.RedirectTrailingSlash {
if len(path) > 1 && path[len(path)-1] == '/' {
req.URL.Path = path[:len(path)-1]
} else {
req.URL.Path = path + "/"
}
http.Redirect(w, req, req.URL.String(), code)
return
}
// Try to fix the request path
if r.RedirectFixedPath {
fixedPath, found := root.findCaseInsensitivePath(
CleanPath(path),
r.RedirectTrailingSlash,
)
if found {
req.URL.Path = string(fixedPath)
http.Redirect(w, req, req.URL.String(), code)
return
}
}
}
}
if req.Method == http.MethodOptions && r.HandleOPTIONS {
// Handle OPTIONS requests
if allow := r.allowed(path, http.MethodOptions); allow != "" {
w.Header().Set("Allow", allow)
if r.GlobalOPTIONS != nil {
r.GlobalOPTIONS.ServeHTTP(w, req)
}
return
}
} else if r.HandleMethodNotAllowed { // Handle 405
if allow := r.allowed(path, req.Method); allow != "" {
w.Header().Set("Allow", allow)
if r.MethodNotAllowed != nil {
r.MethodNotAllowed.ServeHTTP(w, req)
} else {
http.Error(w,
http.StatusText(http.StatusMethodNotAllowed),
http.StatusMethodNotAllowed,
)
}
return
}
}
// Handle 404
if r.NotFound != nil {
r.NotFound.ServeHTTP(w, req)
} else {
http.NotFound(w, req)
}
}
- 通过getValue()获取Handle
// 通过请求来的路径返回handle和参数(tsr是否重定向)
func (n *node) getValue(path string) (handle Handle, p Params, tsr bool) {
walk: // 遍历树
for {
if len(path) > len(n.path) {
fmt.Println("n.path:", n.path)
if path[:len(n.path)] == n.path {
path = path[len(n.path):]
// If this node does not have a wildcard (param or catchAll)
// child, we can just look up the next child node and continue
// to walk down the tree
// 如果当前节点不是参数节点,就一直找子节点直到遍历完整颗树
if !n.wildChild {
c := path[0]
for i := 0; i < len(n.indices); i++ {
if c == n.indices[i] {
n = n.children[i]
continue walk
}
}
// 如果没有找到handle,会重定向到尾部没有 / 的path
tsr = (path == "/" && n.handle != nil)
return
}
// 处理参数节点
n = n.children[0]
switch n.nType {
case param:
// 找到参数或者 / 或者 路径结束
end := 0
for end < len(path) && path[end] != '/' {
end++
}
// 保存参数
if p == nil {
// 懒惰分配
p = make(Params, 0, n.maxParams)
}
i := len(p)
p = p[:i+1] // 在预分配容量内展开切片
p[i].Key = n.path[1:]
p[i].Value = path[:end]
// 更进一步查找
if end < len(path) {
if len(n.children) > 0 {
path = path[end:]
n = n.children[0]
continue walk
}
// 还是未找到
tsr = (len(path) == end+1)
return
}
if handle = n.handle; handle != nil {
return
} else if len(n.children) == 1 {
// 重定向
n = n.children[0]
tsr = (n.path == "/" && n.handle != nil)
}
return
case catchAll:
// 保存参数值
if p == nil {
// 懒惰分配
p = make(Params, 0, n.maxParams)
}
i := len(p)
p = p[:i+1] // 在预分配容量内展开切片
p[i].Key = n.path[2:]
p[i].Value = path
handle = n.handle
return
default:
panic("invalid node type")
}
}
} else if path == n.path {
// 应该找到handle了
if handle = n.handle; handle != nil {
return
}
if path == "/" && n.wildChild && n.nType != root {
tsr = true
return
}
// 重定向
for i := 0; i < len(n.indices); i++ {
if n.indices[i] == '/' {
n = n.children[i]
tsr = (len(n.path) == 1 && n.handle != nil) ||
(n.nType == catchAll && n.children[0].handle != nil)
return
}
}
return
}
// 重定向
tsr = (path == "/") ||
(len(n.path) == len(path)+1 && n.path[len(path)] == '/' &&
path == n.path[:len(n.path)-1] && n.handle != nil)
return
}
}
五. 总结
- golang net/http标准库中存在一些问题,比如:
- 不能单独的对请求方法(POST,GET等)注册特定的处理函数
- 不支持Path变量参数
- 不能自动对Path进行校准
- 性能一般并且扩展性不足
- 解决这些问题,有一个三方库/httprouter是一个高性能可扩展 HTTP 路由,
- 首先调用httprouter下的New()函数进行路由初始化,会拿到一个Router结构体变量
- 调用Router下的GET或POST方法注册路由接口
- 在指定端口开启监听,等待客户端请求
- Router 实现net/http下ServeHTTP(w http.ResponseWriter, req *http.Request) 这个方法,ServeHTTP()实际就是接收请求,匹配到指定路由后执行的函数,是 httprouter 的一个核心的部分,定义了路由的一些初始配置,在Router中存在一个 trees 属性,用来保存注册的路由,可以这样说了解httprouter实际就是了解Router中通过trees 保存路由与路由发现的过程
- 路由的注册trees 树的构建过程,可以追一下router.GET()或POST()等方法的源码
- 内部会调用Router的Handle()方法,Handle方法会接收三个入参:1请求方法method,2接口路径path,3处理器函数handle
- 在注册时首先判断Router上的trees,是否为nil,如果是,先创建trees, trees是一个map,
- 如果不为nil,会根据请求方法method,判断当前trees中是否存在该类型的接口,如果不存在,会将当前请求方法method作为根节点存储到trees中
- 然后会将当前接口路径,接口处理器,封装为node,调用node上的addRoute()方法进行路由注册
- node内部有一个path属性,存储的就是接口的url,handle就是当前节点对应的处理器,trees树是一种压缩的动态字典树结构
- 每个节点可以有多个子节点,每个子节点对应一个路径段。
- 每个节点可以存储一个路由模式和一个处理函数,如果没有则为空。
- 每个节点可以有一个通配符子节点,用来匹配任意路径段。
- 路由模式中的参数用冒号(:)开头,通配符用星号(*)开头。
- 路由模式中不能有重复的参数或通配符。
- 接收到请求后会执行 Router上的ServeHTTP()方法,查看该方法源码
- 获取到Router的trees属性,通过method请求方法先获取到根节点,然后调用node的getValue()进行路由查找,
- 在node的getValue()会遍历前缀树,一直找到与请求路径匹配成功的最后一个节点,返回对应的handle,然后执行handle
- trees树把相同前缀的路由模式合并在一起,减少了节点的数量和匹配的时间。当有一个请求路径到来时,trees树会从根节点开始,按照路径段逐层向下查找,直到找到一个完全匹配的节点或者没有匹配的节点。如果找到了一个完全匹配的节点,就调用它存储的处理函数,并把路由模式中的参数和通配符对应的值传递给它。如果没有找到匹配的节点,就返回404错误
- 假设当前服务中有一下接口
/user/:name
/user/:name/profile
/user/:name/friends
/user/:name/friends/:friend
/user/*
/static/*
/favicon.ico
//在trees树中最终会形成如下格式
+--------+
| root |
+---+----+
|
| /user/
v
+-----+-----+
| :name |
| /user/:name|
+-----+-----+
| | |
| | /profile
| v
| +---+---+
| |profile|
| |/user/:name/profile
| +-------+
| |
| | /friends
| v
| +---+----+
| |friends |
| |/user/:name/friends
| +---+----+
| | |
| | /:friend
| v
| +---+-------+
| |:friend |
| |/user/:name/friends/:friend
| +-----------+
|
| /*
v
+---+---+
| * | |
|/user/*|
+---+---+
|
| /static/
v
+---+----+
| * |
|/static/*|
+--------+
|
| /favicon.ico
v
+----------+
|favicon.ico|
|/favicon.ico|
+----------+