Skip to content

Commit

Permalink
grpc: add clientconn.CanonicalTarget() to return the canonical target…
Browse files Browse the repository at this point in the history
… string (#7006)
  • Loading branch information
arvindbr8 authored Feb 28, 2024
1 parent 51f9cc0 commit c267d5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
13 changes: 7 additions & 6 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,13 +883,13 @@ func (cc *ClientConn) channelzMetric() *channelz.ChannelInternalMetric {
}
}

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

// CanonicalTarget returns the canonical target string of the ClientConn.
func (cc *ClientConn) CanonicalTarget() string {
return cc.parsedTarget.String()
}

Expand Down Expand Up @@ -1744,6 +1744,7 @@ 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
39 changes: 21 additions & 18 deletions clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,31 +808,31 @@ func (s) TestMethodConfigDefaultService(t *testing.T) {
}
}

func (s) TestClientConn_Target(t *testing.T) {
func (s) TestClientConnCanonicalTarget(t *testing.T) {
tests := []struct {
name string
addr string
targetWant string
name string
addr string
canonicalTargetWant string
}{
{
name: "normal-case",
addr: "dns://a.server.com/google.com",
targetWant: "dns://a.server.com/google.com",
name: "normal-case",
addr: "dns://a.server.com/google.com",
canonicalTargetWant: "dns://a.server.com/google.com",
},
{
name: "canonical-target-not-specified",
addr: "no.scheme",
targetWant: "passthrough:///no.scheme",
name: "canonical-target-not-specified",
addr: "no.scheme",
canonicalTargetWant: "passthrough:///no.scheme",
},
{
name: "canonical-target-nonexistent",
addr: "nonexist:///non.existent",
targetWant: "passthrough:///nonexist:///non.existent",
name: "canonical-target-nonexistent",
addr: "nonexist:///non.existent",
canonicalTargetWant: "passthrough:///nonexist:///non.existent",
},
{
name: "canonical-target-add-colon-slash",
addr: "dns:hostname:port",
targetWant: "dns:///hostname:port",
name: "canonical-target-add-colon-slash",
addr: "dns:hostname:port",
canonicalTargetWant: "dns:///hostname:port",
},
}
for _, test := range tests {
Expand All @@ -842,8 +842,11 @@ func (s) TestClientConn_Target(t *testing.T) {
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)
if cc.Target() != test.addr {
t.Fatalf("Target() = %s, want %s", cc.Target(), test.addr)
}
if cc.CanonicalTarget() != test.canonicalTargetWant {
t.Fatalf("CanonicalTarget() = %s, want %s", cc.CanonicalTarget(), test.canonicalTargetWant)
}
})
}
Expand Down

0 comments on commit c267d5b

Please sign in to comment.