聊聊dubbo-go-proxy的Route

codecraft · · 496 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要研究一下dubbo-go-proxy的Route

Route

dubbo-go-proxy/pkg/router/route.go

// Route defines the tree of router APIs
type Route struct {
    lock         sync.RWMutex
    tree         *avltree.Tree
    wildcardTree *avltree.Tree
}

// NewRoute returns an empty router tree
func NewRoute() *Route {
    return &Route{
        tree:         avltree.NewWithStringComparator(),
        wildcardTree: avltree.NewWithStringComparator(),
    }
}
Route定义了lock、tree、wildcardTree属性,其NewRoute方法用于创建Route

PutAPI

dubbo-go-proxy/pkg/router/route.go

// PutAPI puts an api into the resource
func (rt *Route) PutAPI(api router.API) error {
    lowerCasePath := strings.ToLower(api.URLPattern)
    node, ok := rt.findNode(lowerCasePath)
    rt.lock.Lock()
    defer rt.lock.Unlock()
    if !ok {
        wildcard := strings.Contains(lowerCasePath, constant.PathParamIdentifier)
        rn := &Node{
            fullPath: lowerCasePath,
            methods:  map[config.HTTPVerb]*config.Method{api.Method.HTTPVerb: &api.Method},
            wildcard: wildcard,
            headers:  api.Headers,
        }
        if wildcard {
            rt.wildcardTree.Put(lowerCasePath, rn)
        }
        rt.tree.Put(lowerCasePath, rn)
        return nil
    }
    return node.putMethod(api.Method, api.Headers)
}
PutAPI方法根据lowerCasePath去查找node,若没有找到则加锁创建Node,然后放到tree中,若是path是wildcard的则也会加入到wildcardTree中;如果有找到则执行node.putMethod(api.Method, api.Headers)

UpdateAPI

dubbo-go-proxy/pkg/router/route.go

// UpdateAPI update the api method in the existing router node
func (rt *Route) UpdateAPI(api router.API) error {
    node, found := rt.findNode(api.URLPattern)
    if found {
        if _, ok := node.methods[api.Method.HTTPVerb]; ok {
            rt.lock.Lock()
            defer rt.lock.Unlock()
            node.methods[api.Method.HTTPVerb] = &api.Method
        }
    }
    return nil
}
UpdateAPI方法先根据api.URLPattern查找node,若找不到返回nil,找到的话再去找node.methods[api.Method.HTTPVerb],若找到则将api.Method赋值给node.methods[api.Method.HTTPVerb]

FindAPI

dubbo-go-proxy/pkg/router/route.go

// FindAPI returns the api that meets the
func (rt *Route) FindAPI(fullPath string, httpverb config.HTTPVerb) (*router.API, bool) {
    if n, found := rt.findNode(fullPath); found {
        rt.lock.RLock()
        defer rt.lock.RUnlock()
        if method, ok := n.methods[httpverb]; ok {
            return &router.API{
                URLPattern: n.fullPath,
                Method:     *method,
                Headers:    n.headers,
            }, ok
        }
    }
    return nil, false
}
FindAPI方法先通过findNode找node,再通过node.methods[httpverb]找method

findNode

dubbo-go-proxy/pkg/router/route.go

func (rt *Route) findNode(fullPath string) (*Node, bool) {
    lowerPath := strings.ToLower(fullPath)
    var n interface{}
    var found bool
    if n, found = rt.searchWildcard(lowerPath); !found {
        rt.lock.RLock()
        defer rt.lock.RUnlock()
        if n, found = rt.tree.Get(lowerPath); !found {
            return nil, false
        }
    }
    return n.(*Node), found
}
findNode方法通过searchWildcard来查找node,找不到则从node的tree.Get方法查找

searchWildcard

dubbo-go-proxy/pkg/router/route.go

func (rt *Route) searchWildcard(fullPath string) (*Node, bool) {
    rt.lock.RLock()
    defer rt.lock.RUnlock()
    wildcardPaths := rt.wildcardTree.Keys()
    for _, p := range wildcardPaths {
        if wildcardMatch(p.(string), fullPath) != nil {
            n, ok := rt.wildcardTree.Get(p)
            return n.(*Node), ok
        }
    }
    return nil, false
}
searchWildcard方法遍历wildcardTree.Keys(),挨个执行wildcardMatch,若匹配到则通过wildcardTree.Get(p)来获取node

小结

Route定义了lock、tree、wildcardTree属性,其NewRoute方法用于创建Route;它提供了PutAPI、UpdateAPI、FindAPI等方法;里头实现使用的是avltree.Tree。

doc


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊dubbo-go-proxy的Route

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

496 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传