Skip to content

Commit c267d5b

Browse files
authored
grpc: add clientconn.CanonicalTarget() to return the canonical target string (#7006)
1 parent 51f9cc0 commit c267d5b

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

clientconn.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,13 @@ func (cc *ClientConn) channelzMetric() *channelz.ChannelInternalMetric {
883883
}
884884
}
885885

886-
// Target returns the canonical target string of the ClientConn.
887-
//
888-
// # Experimental
889-
//
890-
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
891-
// later release.
886+
// Target returns the target string of the ClientConn.
892887
func (cc *ClientConn) Target() string {
888+
return cc.target
889+
}
890+
891+
// CanonicalTarget returns the canonical target string of the ClientConn.
892+
func (cc *ClientConn) CanonicalTarget() string {
893893
return cc.parsedTarget.String()
894894
}
895895

@@ -1744,6 +1744,7 @@ func (cc *ClientConn) parseTargetAndFindResolver() error {
17441744
defScheme := resolver.GetDefaultScheme()
17451745
channelz.Infof(logger, cc.channelzID, "fallback to scheme %q", defScheme)
17461746
canonicalTarget := defScheme + ":///" + cc.target
1747+
17471748
parsedTarget, err = parseTarget(canonicalTarget)
17481749
if err != nil {
17491750
channelz.Infof(logger, cc.channelzID, "dial target %q parse failed: %v", canonicalTarget, err)

clientconn_test.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -808,31 +808,31 @@ func (s) TestMethodConfigDefaultService(t *testing.T) {
808808
}
809809
}
810810

811-
func (s) TestClientConn_Target(t *testing.T) {
811+
func (s) TestClientConnCanonicalTarget(t *testing.T) {
812812
tests := []struct {
813-
name string
814-
addr string
815-
targetWant string
813+
name string
814+
addr string
815+
canonicalTargetWant string
816816
}{
817817
{
818-
name: "normal-case",
819-
addr: "dns://a.server.com/google.com",
820-
targetWant: "dns://a.server.com/google.com",
818+
name: "normal-case",
819+
addr: "dns://a.server.com/google.com",
820+
canonicalTargetWant: "dns://a.server.com/google.com",
821821
},
822822
{
823-
name: "canonical-target-not-specified",
824-
addr: "no.scheme",
825-
targetWant: "passthrough:///no.scheme",
823+
name: "canonical-target-not-specified",
824+
addr: "no.scheme",
825+
canonicalTargetWant: "passthrough:///no.scheme",
826826
},
827827
{
828-
name: "canonical-target-nonexistent",
829-
addr: "nonexist:///non.existent",
830-
targetWant: "passthrough:///nonexist:///non.existent",
828+
name: "canonical-target-nonexistent",
829+
addr: "nonexist:///non.existent",
830+
canonicalTargetWant: "passthrough:///nonexist:///non.existent",
831831
},
832832
{
833-
name: "canonical-target-add-colon-slash",
834-
addr: "dns:hostname:port",
835-
targetWant: "dns:///hostname:port",
833+
name: "canonical-target-add-colon-slash",
834+
addr: "dns:hostname:port",
835+
canonicalTargetWant: "dns:///hostname:port",
836836
},
837837
}
838838
for _, test := range tests {
@@ -842,8 +842,11 @@ func (s) TestClientConn_Target(t *testing.T) {
842842
t.Fatalf("Dial(%s, _) = _, %v, want _, <nil>", test.addr, err)
843843
}
844844
defer cc.Close()
845-
if cc.Target() != test.targetWant {
846-
t.Fatalf("Target() = %s, want %s", cc.Target(), test.targetWant)
845+
if cc.Target() != test.addr {
846+
t.Fatalf("Target() = %s, want %s", cc.Target(), test.addr)
847+
}
848+
if cc.CanonicalTarget() != test.canonicalTargetWant {
849+
t.Fatalf("CanonicalTarget() = %s, want %s", cc.CanonicalTarget(), test.canonicalTargetWant)
847850
}
848851
})
849852
}

0 commit comments

Comments
 (0)