From 52cc824882061bd85c17ca14f4946e21cb1e3e2e Mon Sep 17 00:00:00 2001 From: barnettZQG Date: Wed, 4 Dec 2019 19:28:05 +0800 Subject: [PATCH] fix endpoint value check failure bug --- node/nodem/client/cluster_client.go | 19 +++++++++++++------ node/nodem/client/cluster_client_test.go | 2 ++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/node/nodem/client/cluster_client.go b/node/nodem/client/cluster_client.go index a1c772301..26e17f070 100644 --- a/node/nodem/client/cluster_client.go +++ b/node/nodem/client/cluster_client.go @@ -136,16 +136,24 @@ func (e *etcdClusterClient) GetEndpoints(key string) (result []string) { } //Return data check for _, v := range res { - endpointURL, err := url.Parse(v) - if err != nil || endpointURL.Host == "" || endpointURL.Path != "" { - continue + if checkURL(v) { + result = append(result, v) } - result = append(result, v) } } logrus.Infof("Get endpoints %s => %v", key, result) return } +func checkURL(source string) bool { + endpointURL, err := url.Parse(source) + if err != nil && strings.Contains(err.Error(), "first path segment in URL cannot contain colon") { + endpointURL, err = url.Parse(fmt.Sprintf("tcp://%s", source)) + } + if err != nil || endpointURL.Host == "" || endpointURL.Path != "" { + return false + } + return true +} //SetEndpoints service name and hostip must set func (e *etcdClusterClient) SetEndpoints(serviceName, hostIP string, value []string) { @@ -156,8 +164,7 @@ func (e *etcdClusterClient) SetEndpoints(serviceName, hostIP string, value []str return } for _, v := range value { - endpointURL, err := url.Parse(v) - if err != nil || endpointURL.Host == "" || endpointURL.Path != "" { + if !checkURL(v) { logrus.Warningf("%s service host %s endpoint value %s invalid", serviceName, hostIP, v) continue } diff --git a/node/nodem/client/cluster_client_test.go b/node/nodem/client/cluster_client_test.go index 7595f3a71..71a5b5d5d 100644 --- a/node/nodem/client/cluster_client_test.go +++ b/node/nodem/client/cluster_client_test.go @@ -119,6 +119,8 @@ func TestSetEndpoints(t *testing.T) { c.SetEndpoints("etcd", "DSASD", []string{"http://:8080"}) c.SetEndpoints("etcd", "192.168.1.1", []string{"http://:8080"}) c.SetEndpoints("etcd", "192.168.1.1", []string{"http://192.168.1.1:8080"}) + c.SetEndpoints("node", "192.168.2.137", []string{"192.168.2.137:10252"}) + t.Logf("check: %v", checkURL("192.168.2.137:10252")) } func TestGetEndpoints(t *testing.T) {