From 3ee8c7c5de70938b962b0c1ffe9ab77ac6ada440 Mon Sep 17 00:00:00 2001
From: flycash <flycash@apache.org>
Date: Sat, 22 Feb 2020 20:36:40 +0800
Subject: [PATCH] Fix review and test

---
 config_center/nacos/client.go |  2 +-
 config_center/nacos/facade.go |  2 +-
 registry/base_registry.go     | 34 ++++++++++++++++++++++------------
 3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/config_center/nacos/client.go b/config_center/nacos/client.go
index d87ee74dd..06db101d8 100644
--- a/config_center/nacos/client.go
+++ b/config_center/nacos/client.go
@@ -140,7 +140,7 @@ func newNacosClient(name string, nacosAddrs []string, timeout time.Duration) (*N
 		},
 	}
 
-	svrConfList := []nacosconst.ServerConfig{}
+	svrConfList := make([]nacosconst.ServerConfig, 0, len(n.NacosAddrs))
 	for _, nacosAddr := range n.NacosAddrs {
 		split := strings.Split(nacosAddr, ":")
 		port, err := strconv.ParseUint(split[1], 10, 64)
diff --git a/config_center/nacos/facade.go b/config_center/nacos/facade.go
index 7fe4af8d0..77a79ed09 100644
--- a/config_center/nacos/facade.go
+++ b/config_center/nacos/facade.go
@@ -49,7 +49,7 @@ type nacosClientFacade interface {
 // HandleClientRestart Restart client handler
 func HandleClientRestart(r nacosClientFacade) {
 	var (
-		err error
+		err       error
 		failTimes int
 	)
 
diff --git a/registry/base_registry.go b/registry/base_registry.go
index 2a6db51ad..530fedb47 100644
--- a/registry/base_registry.go
+++ b/registry/base_registry.go
@@ -162,7 +162,7 @@ func (r *BaseRegistry) service(c common.URL) string {
 func (r *BaseRegistry) RestartCallBack() bool {
 
 	// copy r.services
-	services := []common.URL{}
+	services := make([]common.URL, 0, len(r.services))
 	for _, confIf := range r.services {
 		services = append(services, confIf)
 	}
@@ -236,9 +236,11 @@ func (r *BaseRegistry) providerRegistry(c common.URL, params url.Values) (string
 		return "", "", perrors.Errorf("conf{Path:%s, Methods:%s}", c.Path, c.Methods)
 	}
 	dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), common.DubboNodes[common.PROVIDER])
-	r.cltLock.Lock()
-	err = r.facadeBasedRegistry.CreatePath(dubboPath)
-	r.cltLock.Unlock()
+	func() {
+		r.cltLock.Lock()
+		defer r.cltLock.Unlock()
+		err = r.facadeBasedRegistry.CreatePath(dubboPath)
+	}()
 	if err != nil {
 		logger.Errorf("facadeBasedRegistry.CreatePath(path{%s}) = error{%#v}", dubboPath, perrors.WithStack(err))
 		return "", "", perrors.WithMessagef(err, "facadeBasedRegistry.CreatePath(path:%s)", dubboPath)
@@ -260,10 +262,11 @@ func (r *BaseRegistry) providerRegistry(c common.URL, params url.Values) (string
 	logger.Debugf("provider url params:%#v", params)
 	var host string
 	if c.Ip == "" {
-		host = localIP + ":" + c.Port
+		host = localIP
 	} else {
-		host = c.Ip + ":" + c.Port
+		host = c.Ip
 	}
+	host += ":" + c.Port
 
 	rawURL = fmt.Sprintf("%s://%s%s?%s", c.Protocol, host, c.Path, params.Encode())
 	// Print your own registration service providers.
@@ -280,17 +283,24 @@ func (r *BaseRegistry) consumerRegistry(c common.URL, params url.Values) (string
 		err       error
 	)
 	dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), common.DubboNodes[common.CONSUMER])
-	r.cltLock.Lock()
-	err = r.facadeBasedRegistry.CreatePath(dubboPath)
-	r.cltLock.Unlock()
+
+	func() {
+		r.cltLock.Lock()
+		err = r.facadeBasedRegistry.CreatePath(dubboPath)
+		r.cltLock.Unlock()
+	}()
 	if err != nil {
 		logger.Errorf("facadeBasedRegistry.CreatePath(path{%s}) = error{%v}", dubboPath, perrors.WithStack(err))
 		return "", "", perrors.WithStack(err)
 	}
 	dubboPath = fmt.Sprintf("/dubbo/%s/%s", r.service(c), common.DubboNodes[common.PROVIDER])
-	r.cltLock.Lock()
-	err = r.facadeBasedRegistry.CreatePath(dubboPath)
-	r.cltLock.Unlock()
+
+	func() {
+		r.cltLock.Lock()
+		defer r.cltLock.Unlock()
+		err = r.facadeBasedRegistry.CreatePath(dubboPath)
+	}()
+
 	if err != nil {
 		logger.Errorf("facadeBasedRegistry.CreatePath(path{%s}) = error{%v}", dubboPath, perrors.WithStack(err))
 		return "", "", perrors.WithStack(err)
-- 
GitLab