fix(discov): move etcd hosts from URI authority to path for Go 1.26 compatibility (#5548)

This commit is contained in:
Kevin Wan
2026-04-25 10:48:28 +08:00
committed by GitHub
parent 22bdae0787
commit 4a67261b7b
6 changed files with 120 additions and 6 deletions

View File

@@ -17,3 +17,29 @@ func GetAuthority(target resolver.Target) string {
func GetEndpoints(target resolver.Target) string {
return strings.Trim(target.URL.Path, slashSeparator)
}
// GetHosts returns the comma-separated etcd hosts from the target URL.
// It supports two formats:
// - New format (etcd:///h1:port,h2:port?key=k): hosts are in the URL path (empty authority)
// - Legacy format (etcd://h1:port/key): host is in the URL authority
func GetHosts(target resolver.Target) string {
if target.URL.Host == "" {
// New format: hosts encoded in URL path to avoid RFC 3986 authority issues
return GetEndpoints(target)
}
// Legacy format: single host in authority
return target.URL.Host
}
// GetKey returns the etcd key from the target URL.
// It supports two formats:
// - New format (etcd:///h1:port,h2:port?key=k): key is in the "key" query parameter
// - Legacy format (etcd://h1:port/key): key is in the URL path
func GetKey(target resolver.Target) string {
if target.URL.Host == "" {
// New format: key is in the query parameter
return target.URL.Query().Get("key")
}
// Legacy format: key is in the path
return strings.Trim(target.URL.Path, slashSeparator)
}

View File

@@ -87,3 +87,83 @@ func TestGetEndpoints(t *testing.T) {
})
}
}
func TestGetHosts(t *testing.T) {
tests := []struct {
name string
url string
want string
}{
{
name: "single host",
url: "etcd:///localhost:2379?key=foo",
want: "localhost:2379",
},
{
name: "multiple hosts",
url: "etcd:///host1:2379,host2:2379,host3:2379?key=foo",
want: "host1:2379,host2:2379,host3:2379",
},
{
name: "legacy single host in authority",
url: "etcd://localhost:2379/my-service",
want: "localhost:2379",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uri, err := url.Parse(test.url)
assert.Nil(t, err)
target := resolver.Target{
URL: *uri,
}
assert.Equal(t, test.want, GetHosts(target))
})
}
}
func TestGetKey(t *testing.T) {
tests := []struct {
name string
url string
want string
}{
{
name: "simple key",
url: "etcd:///localhost:2379?key=my-service",
want: "my-service",
},
{
name: "key with slashes",
url: "etcd:///localhost:2379?key=%2Fgrpc%2Fmy-service",
want: "/grpc/my-service",
},
{
name: "no key",
url: "etcd:///localhost:2379",
want: "",
},
{
name: "legacy key in path",
url: "etcd://localhost:2379/my-service",
want: "my-service",
},
{
name: "legacy key with leading slash",
url: "etcd://localhost:2379/grpc/my-service",
want: "grpc/my-service",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uri, err := url.Parse(test.url)
assert.Nil(t, err)
target := resolver.Target{
URL: *uri,
}
assert.Equal(t, test.want, GetKey(target))
})
}
}