diff --git a/config_center/nacos/client.go b/config_center/nacos/client.go index d87ee74dda6b9692a4a31e252ad77d28240f5741..06db101d888701d73c9d2bdf87c3715a73c4ee46 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 7fe4af8d0e6877a857838f84e78158d398faf555..77a79ed091461ea5184cb2531d985c233ccd92e9 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 2a6db51ad77dd23c24fc5d1c27b8ab0dd34c76f8..530fedb4768abc6d96c6b131204223d5b2f8087f 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)