Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc: Canonicalize string returned by ClientConn.Target() and resolver.Address.String() #6923

Merged
merged 4 commits into from Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions clientconn.go
Expand Up @@ -883,14 +883,14 @@ func (cc *ClientConn) channelzMetric() *channelz.ChannelInternalMetric {
}
}

// Target returns the target string of the ClientConn.
// Target returns the canonicalized target string of the ClientConn.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use "canonical" since "canonicalize" isn't a word.

Target returns the canonical target string...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done haha.

//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func (cc *ClientConn) Target() string {
return cc.target
return cc.parsedTarget.String()
}

func (cc *ClientConn) incrCallsStarted() {
Expand Down Expand Up @@ -1744,7 +1744,6 @@ func (cc *ClientConn) parseTargetAndFindResolver() error {
defScheme := resolver.GetDefaultScheme()
channelz.Infof(logger, cc.channelzID, "fallback to scheme %q", defScheme)
canonicalTarget := defScheme + ":///" + cc.target

parsedTarget, err = parseTarget(canonicalTarget)
if err != nil {
channelz.Infof(logger, cc.channelzID, "dial target %q parse failed: %v", canonicalTarget, err)
Expand Down
40 changes: 32 additions & 8 deletions clientconn_test.go
Expand Up @@ -808,15 +808,39 @@ func (s) TestMethodConfigDefaultService(t *testing.T) {
}
}

func (s) TestGetClientConnTarget(t *testing.T) {
addr := "nonexist:///non.existent"
cc, err := Dial(addr, WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", addr, err)
func (s) TestClientConn_Target(t *testing.T) {
tests := []struct {
name string
addr string
targetWant string
}{
{
name: "normal-case",
addr: "dns://a.server.com/google.com",
targetWant: "dns://a.server.com/google.com",
},
{
name: "canonicalized-target-not-specified",
addr: "no.scheme",
targetWant: "passthrough:///no.scheme",
},
{
name: "canonicalized-target-nonexistent",
addr: "nonexist:///non.existent",
targetWant: "passthrough:///nonexist:///non.existent",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test dns:hostname:port->dns:///hostname:port.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
defer cc.Close()
if cc.Target() != addr {
t.Fatalf("Target() = %s, want %s", cc.Target(), addr)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cc, err := Dial(test.addr, WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", test.addr, err)
}
defer cc.Close()
if cc.Target() != test.targetWant {
t.Fatalf("Target() = %s, want %s", cc.Target(), test.targetWant)
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion resolver/resolver.go
Expand Up @@ -283,7 +283,7 @@ func (t Target) Endpoint() string {

// String returns a string representation of Target.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add "canonical".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

func (t Target) String() string {
return t.URL.String()
return t.URL.Scheme + "://" + t.URL.Host + "/" + t.Endpoint()
}

// Builder creates a resolver that will be used to watch name resolution updates.
Expand Down