fix endpoint value check failure bug

This commit is contained in:
barnettZQG 2019-12-04 19:28:05 +08:00
parent 43c3decd58
commit 52cc824882
2 changed files with 15 additions and 6 deletions

View File

@ -136,16 +136,24 @@ func (e *etcdClusterClient) GetEndpoints(key string) (result []string) {
} }
//Return data check //Return data check
for _, v := range res { for _, v := range res {
endpointURL, err := url.Parse(v) if checkURL(v) {
if err != nil || endpointURL.Host == "" || endpointURL.Path != "" { result = append(result, v)
continue
} }
result = append(result, v)
} }
} }
logrus.Infof("Get endpoints %s => %v", key, result) logrus.Infof("Get endpoints %s => %v", key, result)
return 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 //SetEndpoints service name and hostip must set
func (e *etcdClusterClient) SetEndpoints(serviceName, hostIP string, value []string) { func (e *etcdClusterClient) SetEndpoints(serviceName, hostIP string, value []string) {
@ -156,8 +164,7 @@ func (e *etcdClusterClient) SetEndpoints(serviceName, hostIP string, value []str
return return
} }
for _, v := range value { for _, v := range value {
endpointURL, err := url.Parse(v) if !checkURL(v) {
if err != nil || endpointURL.Host == "" || endpointURL.Path != "" {
logrus.Warningf("%s service host %s endpoint value %s invalid", serviceName, hostIP, v) logrus.Warningf("%s service host %s endpoint value %s invalid", serviceName, hostIP, v)
continue continue
} }

View File

@ -119,6 +119,8 @@ func TestSetEndpoints(t *testing.T) {
c.SetEndpoints("etcd", "DSASD", []string{"http://:8080"}) 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://:8080"})
c.SetEndpoints("etcd", "192.168.1.1", []string{"http://192.168.1.1: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) { func TestGetEndpoints(t *testing.T) {