Copilot commented on code in PR #777:
URL: https://github.com/apache/dubbo-go-pixiu/pull/777#discussion_r2418335741


##########
pkg/common/router/router.go:
##########
@@ -37,94 +37,148 @@ import (
        "github.com/apache/dubbo-go-pixiu/pkg/server"
 )
 
-type (
-       // RouterCoordinator the router coordinator for http connection manager
-       RouterCoordinator struct {
-               activeConfig *model.RouteConfiguration
-               rw           sync.RWMutex
-       }
-)
+// RouterCoordinator the router coordinator for http connection manager
+type RouterCoordinator struct {
+       active   snapshotHolder // atomic snapshot
+       mu       sync.Mutex
+       store    map[string]*model.Router // temp store for dynamic update, DO 
NOT read directly
+       timer    *time.Timer              // debounce timer
+       debounce time.Duration            // merge window, default 50ms
+}
 
 // CreateRouterCoordinator create coordinator for http connection manager
 func CreateRouterCoordinator(routeConfig *model.RouteConfiguration) 
*RouterCoordinator {
-       rc := &RouterCoordinator{activeConfig: routeConfig}
+       rc := &RouterCoordinator{
+               store:    make(map[string]*model.Router),
+               debounce: 50 * time.Millisecond, // merge window
+       }
        if routeConfig.Dynamic {
                server.GetRouterManager().AddRouterListener(rc)
        }
-       rc.initTrie()
-       rc.initRegex()
+       // build initial config and store snapshot
+       first := buildConfig(routeConfig.Routes)
+       rc.active.store(model.ToSnapshot(first))
+       // copy initial routes to store
+       for _, r := range routeConfig.Routes {
+               rc.store[r.ID] = r
+       }
        return rc
 }
 
-// Route find routeAction for request
 func (rm *RouterCoordinator) Route(hc *http.HttpContext) (*model.RouteAction, 
error) {
-       rm.rw.RLock()
-       defer rm.rw.RUnlock()
-
        return rm.route(hc.Request)
 }
 
 func (rm *RouterCoordinator) RouteByPathAndName(path, method string) 
(*model.RouteAction, error) {
-       rm.rw.RLock()
-       defer rm.rw.RUnlock()
-
-       return rm.activeConfig.RouteByPathAndMethod(path, method)
+       s := rm.active.load()
+       if s == nil {
+               return nil, errors.New("router configuration is empty")
+       }
+       t := s.MethodTries[method]
+       if t == nil {
+               return nil, errors.Errorf("route failed for %s, no rules 
matched.", stringutil.GetTrieKey(method, path))
+       }
+       node, _, ok := t.Match(stringutil.GetTrieKey(method, path))
+       if !ok || node == nil || node.GetBizInfo() == nil {
+               return nil, errors.Errorf("route failed for %s, no rules 
matched.", stringutil.GetTrieKey(method, path))
+       }
+       act := node.GetBizInfo().(model.RouteAction)
+       return &act, nil
 }
 
 func (rm *RouterCoordinator) route(req *stdHttp.Request) (*model.RouteAction, 
error) {
-       // match those route that only contains headers first
-       var matched []*model.Router
-       for _, route := range rm.activeConfig.Routes {
-               if len(route.Match.Prefix) > 0 {
+       s := rm.active.load()
+       if s == nil {
+               return nil, errors.New("router configuration is empty")
+       }
+
+       // header-only first
+       for _, hr := range s.HeaderOnly {
+               if !model.MethodAllowed(hr.Methods, req.Method) {
                        continue
                }
-               if route.Match.MatchHeader(req) {
-                       matched = append(matched, route)
+               if matchHeaders(hr.Headers, req) {
+                       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))

Review Comment:
   Missing return statement after the error check. The function continues to 
execute the next block when `t == nil`.



##########
pkg/common/util/stringutil/stringutil.go:
##########
@@ -67,24 +67,43 @@ func IsMatchAll(key string) bool {
 }
 
 func GetTrieKey(method string, path string) string {
+       // "http://localhost:8882/api/v1/test-dubbo/user?name=tc/";

Review Comment:
   The comment shows a trailing slash but the actual example URL in the 
original comment didn't have one. Consider updating for consistency.
   ```suggestion
        // "http://localhost:8882/api/v1/test-dubbo/user?name=tc";
   ```



##########
pkg/common/router/router_bench_test.go:
##########
@@ -0,0 +1,429 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package router
+
+import (
+       "math/rand"
+       stdHttp "net/http"
+       "strconv"
+       "testing"
+)
+
+import (
+       oldrouter "github.com/apache/dubbo-go-pixiu/pkg/common/router/mock"
+       "github.com/apache/dubbo-go-pixiu/pkg/context/http"
+       "github.com/apache/dubbo-go-pixiu/pkg/model"
+)
+
+/*
+           ==============================
+               this is the benchmark for router
+               contrast oldrouter and newrouter
+               oldrouter: 
"github.com/apache/dubbo-go-pixiu/pkg/common/router/mock"
+               newrouter: "github.com/apache/dubbo-go-pixiu/pkg/common/router"
+               ==============================

Review Comment:
   The comment block formatting is inconsistent. The opening line uses spaces 
while subsequent lines use tabs for indentation.
   ```suggestion
    *     ==============================
    *     this is the benchmark for router
    *     contrast oldrouter and newrouter
    *     oldrouter: "github.com/apache/dubbo-go-pixiu/pkg/common/router/mock"
    *     newrouter: "github.com/apache/dubbo-go-pixiu/pkg/common/router"
    *     ==============================
   ```



-- 
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]

Reply via email to