From 7fe41cf33c1263d37139781e7c096402a3a39e8e Mon Sep 17 00:00:00 2001
From: Joe Zou <yixian.zou@gmail.com>
Date: Sun, 23 Feb 2020 01:46:51 +0800
Subject: [PATCH] fix router init bug

---
 cluster/directory/base_directory.go | 16 ++++++++++++++++
 cluster/router/chain/chain.go       |  4 ++--
 registry/directory/directory.go     | 18 +-----------------
 3 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/cluster/directory/base_directory.go b/cluster/directory/base_directory.go
index c64ac3e12..0fadb0983 100644
--- a/cluster/directory/base_directory.go
+++ b/cluster/directory/base_directory.go
@@ -23,6 +23,7 @@ import (
 
 import (
 	"github.com/dubbogo/gost/container/set"
+	perrors "github.com/pkg/errors"
 	"go.uber.org/atomic"
 )
 
@@ -33,6 +34,7 @@ import (
 	"github.com/apache/dubbo-go/common/constant"
 	"github.com/apache/dubbo-go/common/extension"
 	"github.com/apache/dubbo-go/common/logger"
+	"github.com/apache/dubbo-go/protocol"
 )
 
 var routerURLSet = gxset.NewSet()
@@ -131,3 +133,17 @@ func GetRouterURLSet() *gxset.HashSet {
 func AddRouterURLSet(url *common.URL) {
 	routerURLSet.Add(url)
 }
+
+// BuildRouterChain build router chain by invokers
+func (dir *staticDirectory) BuildRouterChain(invokers []protocol.Invoker) error {
+	if len(invokers) == 0 {
+		return perrors.Errorf("invokers == null")
+	}
+	url := invokers[0].GetUrl()
+	routerChain, e := chain.NewRouterChain(&url)
+	if e != nil {
+		return e
+	}
+	dir.SetRouterChain(routerChain)
+	return nil
+}
diff --git a/cluster/router/chain/chain.go b/cluster/router/chain/chain.go
index 26d52ecf5..8358bdffc 100644
--- a/cluster/router/chain/chain.go
+++ b/cluster/router/chain/chain.go
@@ -49,7 +49,7 @@ type RouterChain struct {
 }
 
 // Route Loop routers in RouterChain and call Route method to determine the target invokers list.
-func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
+func (c *RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
 	finalInvokers := invoker
 	l := len(c.routers)
 	rs := make([]router.Router, l, int(math.Ceil(float64(l)*1.2)))
@@ -67,7 +67,7 @@ func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocati
 // New a array add builtinRouters which is not sorted in RouterChain and routers
 // Sort the array
 // Replace router array in RouterChain
-func (c RouterChain) AddRouters(routers []router.Router) {
+func (c *RouterChain) AddRouters(routers []router.Router) {
 	newRouters := make([]router.Router, 0, len(c.builtinRouters)+len(routers))
 	newRouters = append(newRouters, c.builtinRouters...)
 	newRouters = append(newRouters, routers...)
diff --git a/registry/directory/directory.go b/registry/directory/directory.go
index 83c46c427..7a89b9e83 100644
--- a/registry/directory/directory.go
+++ b/registry/directory/directory.go
@@ -29,7 +29,6 @@ import (
 
 import (
 	"github.com/apache/dubbo-go/cluster/directory"
-	"github.com/apache/dubbo-go/cluster/router/chain"
 	"github.com/apache/dubbo-go/common"
 	"github.com/apache/dubbo-go/common/constant"
 	"github.com/apache/dubbo-go/common/extension"
@@ -178,34 +177,19 @@ func (dir *registryDirectory) toGroupInvokers() []protocol.Invoker {
 		//len is 1 it means no group setting ,so do not need cluster again
 		for _, invokers := range groupInvokersMap {
 			groupInvokersList = invokers
-			dir.buildRouterChain(invokers)
 		}
 	} else {
 		for _, invokers := range groupInvokersMap {
 			staticDir := directory.NewStaticDirectory(invokers)
 			cluster := extension.GetCluster(dir.GetUrl().SubURL.GetParam(constant.CLUSTER_KEY, constant.DEFAULT_CLUSTER))
+			staticDir.BuildRouterChain(invokers)
 			groupInvokersList = append(groupInvokersList, cluster.Join(staticDir))
-
-			dir.buildRouterChain(invokers)
 		}
 	}
 
 	return groupInvokersList
 }
 
-func (dir *registryDirectory) buildRouterChain(invokers []protocol.Invoker) error {
-	if len(invokers) == 0 {
-		return perrors.Errorf("invokers == null")
-	}
-	url := invokers[0].GetUrl()
-	routerChain, e := chain.NewRouterChain(&url)
-	if e != nil {
-		return e
-	}
-	dir.SetRouterChain(routerChain)
-	return nil
-}
-
 func (dir *registryDirectory) uncacheInvoker(url *common.URL) {
 	logger.Debugf("service will be deleted in cache invokers: invokers key is  %s!", url.Key())
 	dir.cacheInvokersMap.Delete(url.Key())
-- 
GitLab