222twotwotwo commented on code in PR #922:
URL: https://github.com/apache/dubbo-go-pixiu/pull/922#discussion_r3193036286
##########
pkg/common/router/router.go:
##########
@@ -134,20 +134,38 @@ func (rm *RouterCoordinator) route(req *stdHttp.Request)
(*model.RouteAction, er
return &hr.Action, nil
}
}
- // Trie
- t := s.MethodTries[req.Method]
- if t == nil {
- return nil, errors.Errorf("route failed for %s, no rules
matched", stringutil.GetTrieKey(req.Method, req.URL.Path))
+ key := stringutil.GetTrieKey(req.Method, req.URL.Path)
+ // Method-specific trie first, then fall back to the wildcard ("*") trie
+ // so routes declared with methods: ["*"] continue to match any method.
+ if act, err := matchInTrie(s.MethodTries[req.Method], key); act != nil
|| err != nil {
+ return act, err
+ }
+ if req.Method != "*" {
+ if act, err := matchInTrie(s.MethodTries["*"], key); act != nil
|| err != nil {
+ return act, err
+ }
}
+ return nil, errors.Errorf("route failed for %s, no rules matched", key)
+}
- node, _, ok := t.Match(stringutil.GetTrieKey(req.Method, req.URL.Path))
+// matchInTrie looks up key in t. The tri-state return lets callers chain a
+// fallback trie:
+//
+// (action, nil) -> match found, return immediately
+// (nil, error) -> match found but bizInfo has wrong type, do NOT fall
back
+// (nil, nil) -> miss, caller may try the next trie
+func matchInTrie(t *trie.Trie, key string) (*model.RouteAction, error) {
+ if t == nil {
+ return nil, nil
+ }
+ node, _, ok := t.Match(key)
if !ok || node == nil || node.GetBizInfo() == nil {
- return nil, errors.Errorf("route failed for %s, no rules
matched", stringutil.GetTrieKey(req.Method, req.URL.Path))
+ return nil, nil
Review Comment:
如果这里返回error就直接失败结束匹配了,后面还需要继续匹配 *
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]