本文主要研究一下dubbo-go-proxy的Routenode
dubbo-go-proxy/pkg/router/route.gogit
// 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
dubbo-go-proxy/pkg/router/route.gogithub
// 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)
dubbo-go-proxy/pkg/router/route.goapi
// 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]
dubbo-go-proxy/pkg/router/route.gocode
// 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
dubbo-go-proxy/pkg/router/route.gorouter
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方法查找
dubbo-go-proxy/pkg/router/route.goget
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。string