This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new a7b2982  fix:registry timeout not pars (#1392)
a7b2982 is described below

commit a7b2982756f58cd19fcc81d6194fc31d8ac69ad7
Author: zhaoyunxing <[email protected]>
AuthorDate: Fri Aug 20 08:38:34 2021 +0800

    fix:registry timeout not pars (#1392)
    
    * fix:registry timeout not pars
    
    * up:fmt
    
    * up:统一超时时间key
    
    * fix:修改测试顺序问题
    
    * up:fmt
---
 common/constant/key.go                  |  1 +
 common/url.go                           | 10 ++++++++
 config/registry_config.go               |  2 +-
 config/remote_config.go                 |  7 ++---
 metadata/report/etcd/report.go          |  3 +--
 registry/etcdv3/registry.go             |  8 +-----
 registry/zookeeper/service_discovery.go |  2 +-
 remoting/nacos/builder.go               |  5 +---
 remoting/nacos/builder_test.go          | 45 ++++++++++++++++++++++++++++++---
 remoting/zookeeper/client.go            |  9 ++-----
 10 files changed, 62 insertions(+), 30 deletions(-)

diff --git a/common/constant/key.go b/common/constant/key.go
index ae27581..38db23d 100644
--- a/common/constant/key.go
+++ b/common/constant/key.go
@@ -125,6 +125,7 @@ const (
        REGISTRY_PROTOCOL    = "registry"
        ROLE_KEY             = "registry.role"
        REGISTRY_DEFAULT_KEY = "registry.default"
+       // Deprecated use CONFIG_TIMEOUT_KEY key
        REGISTRY_TIMEOUT_KEY = "registry.timeout"
        REGISTRY_LABEL_KEY   = "label"
        PREFERRED_KEY        = "preferred"
diff --git a/common/url.go b/common/url.go
index b279eca..5b2885c 100644
--- a/common/url.go
+++ b/common/url.go
@@ -27,6 +27,7 @@ import (
        "strconv"
        "strings"
        "sync"
+       "time"
 )
 
 import (
@@ -860,3 +861,12 @@ func SetCompareURLEqualFunc(f CompareURLEqualFunc) {
 func GetCompareURLEqualFunc() CompareURLEqualFunc {
        return compareURLEqualFunc
 }
+
+//GetParamDuration get duration if param is invalid or missing will return 3s
+func (c *URL) GetParamDuration(s string, d string) time.Duration {
+
+       if t, err := time.ParseDuration(c.GetParam(s, d)); err == nil {
+               return t
+       }
+       return 3 * time.Second
+}
diff --git a/config/registry_config.go b/config/registry_config.go
index 92d92a6..2b22733 100644
--- a/config/registry_config.go
+++ b/config/registry_config.go
@@ -121,7 +121,7 @@ func (c *RegistryConfig) getUrlMap(roleType 
common.RoleType) url.Values {
        urlMap.Set(constant.GROUP_KEY, c.Group)
        urlMap.Set(constant.ROLE_KEY, strconv.Itoa(int(roleType)))
        urlMap.Set(constant.REGISTRY_KEY, c.Protocol)
-       urlMap.Set(constant.REGISTRY_TIMEOUT_KEY, c.TimeoutStr)
+       urlMap.Set(constant.CONFIG_TIMEOUT_KEY, c.TimeoutStr)
        // multi registry invoker weight label for load balance
        urlMap.Set(constant.REGISTRY_KEY+"."+constant.REGISTRY_LABEL_KEY, 
strconv.FormatBool(true))
        urlMap.Set(constant.REGISTRY_KEY+"."+constant.PREFERRED_KEY, 
strconv.FormatBool(c.Preferred))
diff --git a/config/remote_config.go b/config/remote_config.go
index 292a89e..820f744 100644
--- a/config/remote_config.go
+++ b/config/remote_config.go
@@ -29,7 +29,6 @@ import (
 import (
        "dubbo.apache.org/dubbo-go/v3/common"
        "dubbo.apache.org/dubbo-go/v3/common/constant"
-       "dubbo.apache.org/dubbo-go/v3/common/logger"
 )
 
 // RemoteConfig: usually we need some middleware, including nacos, zookeeper
@@ -45,8 +44,8 @@ type RemoteConfig struct {
        Params     map[string]string `yaml:"params" json:"params,omitempty"`
 }
 
-// Prefix
-func (c *RemoteConfig) Prefix() string {
+// Prefix dubbo.remote.
+func (rc *RemoteConfig) Prefix() string {
        return constant.RemotePrefix
 }
 
@@ -56,8 +55,6 @@ func (rc *RemoteConfig) Timeout() time.Duration {
        if res, err := time.ParseDuration(rc.TimeoutStr); err == nil {
                return res
        }
-       logger.Errorf("Could not parse the timeout string to Duration: %s, the 
default value will be returned",
-               rc.TimeoutStr)
        return 5 * time.Second
 }
 
diff --git a/metadata/report/etcd/report.go b/metadata/report/etcd/report.go
index 836b43b..2d83fa3 100644
--- a/metadata/report/etcd/report.go
+++ b/metadata/report/etcd/report.go
@@ -20,7 +20,6 @@ package etcd
 import (
        "encoding/json"
        "strings"
-       "time"
 )
 
 import (
@@ -146,7 +145,7 @@ type etcdMetadataReportFactory struct{}
 
 // CreateMetadataReport get the MetadataReport instance of etcd
 func (e *etcdMetadataReportFactory) CreateMetadataReport(url *common.URL) 
report.MetadataReport {
-       timeout, _ := 
time.ParseDuration(url.GetParam(constant.REGISTRY_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT))
+       timeout := url.GetParamDuration(constant.CONFIG_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT)
        addresses := strings.Split(url.Location, ",")
        client, err := gxetcd.NewClient(gxetcd.MetadataETCDV3Client, addresses, 
timeout, 1)
        if err != nil {
diff --git a/registry/etcdv3/registry.go b/registry/etcdv3/registry.go
index 3ab9a2e..c777154 100644
--- a/registry/etcdv3/registry.go
+++ b/registry/etcdv3/registry.go
@@ -22,7 +22,6 @@ import (
        "path"
        "strings"
        "sync"
-       "time"
 )
 
 import (
@@ -75,12 +74,7 @@ func (r *etcdV3Registry) ClientLock() *sync.Mutex {
 }
 
 func newETCDV3Registry(url *common.URL) (registry.Registry, error) {
-       timeout, err := 
time.ParseDuration(url.GetParam(constant.REGISTRY_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT))
-       if err != nil {
-               logger.Errorf("timeout config %v is invalid ,err is %v",
-                       url.GetParam(constant.REGISTRY_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT), err.Error())
-               return nil, perrors.WithMessagef(err, "new etcd 
registry(address:%+v)", url.Location)
-       }
+       timeout := url.GetParamDuration(constant.CONFIG_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT)
 
        logger.Infof("etcd address is: %v, timeout is: %s", url.Location, 
timeout.String())
 
diff --git a/registry/zookeeper/service_discovery.go 
b/registry/zookeeper/service_discovery.go
index b2a966d..15cf134 100644
--- a/registry/zookeeper/service_discovery.go
+++ b/registry/zookeeper/service_discovery.go
@@ -102,7 +102,7 @@ func newZookeeperServiceDiscovery(name string) 
(registry.ServiceDiscovery, error
                common.WithParams(make(url.Values)),
                common.WithPassword(remoteConfig.Password),
                common.WithUsername(remoteConfig.Username),
-               common.WithParamsValue(constant.REGISTRY_TIMEOUT_KEY, 
remoteConfig.TimeoutStr))
+               common.WithParamsValue(constant.CONFIG_TIMEOUT_KEY, 
remoteConfig.TimeoutStr))
        url.Location = remoteConfig.Address
        zksd := &zookeeperServiceDiscovery{
                url:      url,
diff --git a/remoting/nacos/builder.go b/remoting/nacos/builder.go
index c42cc93..037169f 100644
--- a/remoting/nacos/builder.go
+++ b/remoting/nacos/builder.go
@@ -68,10 +68,7 @@ func GetNacosConfig(url *common.URL) 
([]nacosConstant.ServerConfig, nacosConstan
                serverConfigs = append(serverConfigs, 
nacosConstant.ServerConfig{IpAddr: ip, Port: uint64(port)})
        }
 
-       timeout, err := 
time.ParseDuration(url.GetParam(constant.REGISTRY_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT))
-       if err != nil {
-               return []nacosConstant.ServerConfig{}, 
nacosConstant.ClientConfig{}, err
-       }
+       timeout := url.GetParamDuration(constant.CONFIG_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT)
 
        clientConfig := nacosConstant.ClientConfig{
                TimeoutMs:           uint64(int32(timeout / time.Millisecond)),
diff --git a/remoting/nacos/builder_test.go b/remoting/nacos/builder_test.go
index 959d27e..89992d6 100644
--- a/remoting/nacos/builder_test.go
+++ b/remoting/nacos/builder_test.go
@@ -19,8 +19,8 @@ package nacos
 
 import (
        "net/url"
-       "strconv"
        "testing"
+       "time"
 )
 
 import (
@@ -83,15 +83,54 @@ func TestNewNacosClientByUrl(t *testing.T) {
        assert.NotNil(t, client)
 }
 
+func TestTimeoutConfig(t *testing.T) {
+       regurlMap := url.Values{}
+       regurlMap.Set(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "true")
+       // regurlMap.Set(constant.NACOS_USERNAME, "nacos")
+       // regurlMap.Set(constant.NACOS_PASSWORD, "nacos")
+       regurlMap.Set(constant.NACOS_NAMESPACE_ID, "nacos")
+
+       t.Run("default timeout", func(t *testing.T) {
+               newURL, _ := common.NewURL("registry://console.nacos.io:80", 
common.WithParams(regurlMap))
+
+               _, cc, err := GetNacosConfig(newURL)
+               assert.Nil(t, err)
+
+               assert.Equal(t, cc.TimeoutMs, 
uint64(int32(10*time.Second/time.Millisecond)))
+       })
+
+       t.Run("right timeout", func(t *testing.T) {
+
+               regurlMap.Set(constant.CONFIG_TIMEOUT_KEY, "5s")
+
+               newURL, _ := common.NewURL("registry://console.nacos.io:80", 
common.WithParams(regurlMap))
+
+               _, cc, err := GetNacosConfig(newURL)
+               assert.Nil(t, err)
+
+               assert.Equal(t, cc.TimeoutMs, 
uint64(int32(5*time.Second/time.Millisecond)))
+       })
+
+       t.Run("invalid timeout", func(t *testing.T) {
+               regurlMap.Set(constant.CONFIG_TIMEOUT_KEY, "5ab")
+
+               newURL, _ := common.NewURL("registry://console.nacos.io:80", 
common.WithParams(regurlMap))
+               _, cc, err := GetNacosConfig(newURL)
+               assert.Nil(t, err)
+
+               assert.Equal(t, cc.TimeoutMs, 
uint64(int32(3*time.Second/time.Millisecond)))
+       })
+
+}
+
 func getRegUrl() *common.URL {
 
        regurlMap := url.Values{}
-       regurlMap.Set(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER))
        regurlMap.Set(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "true")
        // regurlMap.Set(constant.NACOS_USERNAME, "nacos")
        // regurlMap.Set(constant.NACOS_PASSWORD, "nacos")
        regurlMap.Set(constant.NACOS_NAMESPACE_ID, "nacos")
-       regurlMap.Set(constant.REGISTRY_TIMEOUT_KEY, "5s")
+       regurlMap.Set(constant.CONFIG_TIMEOUT_KEY, "5s")
 
        regurl, _ := common.NewURL("registry://console.nacos.io:80", 
common.WithParams(regurlMap))
 
diff --git a/remoting/zookeeper/client.go b/remoting/zookeeper/client.go
index 1ed183f..a324fc7 100644
--- a/remoting/zookeeper/client.go
+++ b/remoting/zookeeper/client.go
@@ -19,7 +19,6 @@ package zookeeper
 
 import (
        "strings"
-       "time"
 )
 
 import (
@@ -55,12 +54,8 @@ func ValidateZookeeperClient(container ZkClientFacade, 
zkName string) error {
 
        if container.ZkClient() == nil {
                // in dubbo, every registry only connect one node, so this is 
[]string{r.Address}
-               timeout, paramErr := 
time.ParseDuration(url.GetParam(constant.REGISTRY_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT))
-               if paramErr != nil {
-                       logger.Errorf("timeout config %v is invalid, err is %v",
-                               url.GetParam(constant.REGISTRY_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT), paramErr.Error())
-                       return perrors.WithMessagef(paramErr, 
"newZookeeperClient(address:%+v)", url.Location)
-               }
+               timeout := url.GetParamDuration(constant.CONFIG_TIMEOUT_KEY, 
constant.DEFAULT_REG_TIMEOUT)
+
                zkAddresses := strings.Split(url.Location, ",")
                newClient, cltErr := gxzookeeper.NewZookeeperClient(zkName, 
zkAddresses, true, gxzookeeper.WithZkTimeOut(timeout))
                if cltErr != nil {

Reply via email to