Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
  • Loading branch information
CabinfeverB committed Mar 12, 2024
1 parent f3888ca commit 34eedb0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
30 changes: 6 additions & 24 deletions client/pd_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package pd
import (
"context"
"crypto/tls"
"fmt"
"net/url"
"reflect"
"sort"
Expand Down Expand Up @@ -1126,36 +1125,19 @@ func addrsToURLs(addrs []string, tlsCfg *tls.Config) []string {
// Add default schema "http://" to addrs.
urls := make([]string, 0, len(addrs))
for _, addr := range addrs {
urls = append(urls, addrToURL(addr, tlsCfg))
urls = append(urls, modifyURLScheme(addr, tlsCfg))
}
return urls
}

func addrToURL(addr string, tlsCfg *tls.Config) string {
if tlsCfg == nil {
u, err := url.Parse(addr)
if err == nil {
u.Scheme = httpScheme
addr = u.String()
} else if !strings.HasPrefix(addr, httpSchemePrefix) {
addr = fmt.Sprintf("%s%s", httpSchemePrefix, addr)
}
} else {
u, err := url.Parse(addr)
if err == nil {
u.Scheme = httpsScheme
addr = u.String()
} else if !strings.HasPrefix(addr, httpsSchemePrefix) {
addr = fmt.Sprintf("%s%s", httpsSchemePrefix, addr)
}
}
return addr
}

func modifyURLScheme(uStr string, tlsCfg *tls.Config) string {
u, err := url.Parse(uStr)
if err != nil {
return uStr
if tlsCfg != nil {
return httpsSchemePrefix + uStr
} else {
return httpSchemePrefix + uStr
}
}
if tlsCfg != nil {
u.Scheme = httpsScheme
Expand Down
26 changes: 14 additions & 12 deletions client/pd_service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ func (suite *serviceClientTestSuite) SetupSuite() {
followerConn, err2 := grpc.Dial(suite.followerServer.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err1 == nil && err2 == nil {
suite.followerClient = newPDServiceClient(
addrToURL(suite.followerServer.addr, nil),
addrToURL(suite.leaderServer.addr, nil),
modifyURLScheme(suite.followerServer.addr, nil),
modifyURLScheme(suite.leaderServer.addr, nil),
followerConn, false)
suite.leaderClient = newPDServiceClient(
addrToURL(suite.leaderServer.addr, nil),
addrToURL(suite.leaderServer.addr, nil),
modifyURLScheme(suite.leaderServer.addr, nil),
modifyURLScheme(suite.leaderServer.addr, nil),
leaderConn, true)
suite.followerServer.server.leaderConn = suite.leaderClient.GetClientConn()
suite.followerServer.server.leaderAddr = suite.leaderClient.GetURL()
Expand All @@ -172,8 +172,8 @@ func (suite *serviceClientTestSuite) TearDownSuite() {

func (suite *serviceClientTestSuite) TestServiceClient() {
re := suite.Require()
leaderAddress := addrToURL(suite.leaderServer.addr, nil)
followerAddress := addrToURL(suite.followerServer.addr, nil)
leaderAddress := modifyURLScheme(suite.leaderServer.addr, nil)
followerAddress := modifyURLScheme(suite.followerServer.addr, nil)

follower := suite.followerClient
leader := suite.leaderClient
Expand Down Expand Up @@ -307,17 +307,17 @@ func (suite *serviceClientTestSuite) TestServiceClientBalancer() {

func TestServiceClientScheme(t *testing.T) {
re := require.New(t)
cli := newPDServiceClient(addrToURL("127.0.0.1:2379", nil), addrToURL("127.0.0.1:2379", nil), nil, false)
cli := newPDServiceClient(modifyURLScheme("127.0.0.1:2379", nil), modifyURLScheme("127.0.0.1:2379", nil), nil, false)
re.Equal("http://127.0.0.1:2379", cli.GetURL())
cli = newPDServiceClient(addrToURL("https://127.0.0.1:2379", nil), addrToURL("127.0.0.1:2379", nil), nil, false)
cli = newPDServiceClient(modifyURLScheme("https://127.0.0.1:2379", nil), modifyURLScheme("127.0.0.1:2379", nil), nil, false)
re.Equal("http://127.0.0.1:2379", cli.GetURL())
cli = newPDServiceClient(addrToURL("http://127.0.0.1:2379", nil), addrToURL("127.0.0.1:2379", nil), nil, false)
cli = newPDServiceClient(modifyURLScheme("http://127.0.0.1:2379", nil), modifyURLScheme("127.0.0.1:2379", nil), nil, false)
re.Equal("http://127.0.0.1:2379", cli.GetURL())
cli = newPDServiceClient(addrToURL("127.0.0.1:2379", &tls.Config{}), addrToURL("127.0.0.1:2379", &tls.Config{}), nil, false)
cli = newPDServiceClient(modifyURLScheme("127.0.0.1:2379", &tls.Config{}), modifyURLScheme("127.0.0.1:2379", &tls.Config{}), nil, false)
re.Equal("https://127.0.0.1:2379", cli.GetURL())
cli = newPDServiceClient(addrToURL("https://127.0.0.1:2379", &tls.Config{}), addrToURL("127.0.0.1:2379", &tls.Config{}), nil, false)
cli = newPDServiceClient(modifyURLScheme("https://127.0.0.1:2379", &tls.Config{}), modifyURLScheme("127.0.0.1:2379", &tls.Config{}), nil, false)
re.Equal("https://127.0.0.1:2379", cli.GetURL())
cli = newPDServiceClient(addrToURL("http://127.0.0.1:2379", &tls.Config{}), addrToURL("127.0.0.1:2379", &tls.Config{}), nil, false)
cli = newPDServiceClient(modifyURLScheme("http://127.0.0.1:2379", &tls.Config{}), modifyURLScheme("127.0.0.1:2379", &tls.Config{}), nil, false)
re.Equal("https://127.0.0.1:2379", cli.GetURL())
}

Expand All @@ -326,8 +326,10 @@ func TestSchemeFunction(t *testing.T) {
tlsCfg := &tls.Config{}
re.Equal("https://127.0.0.1:2379", modifyURLScheme("https://127.0.0.1:2379", tlsCfg))
re.Equal("https://127.0.0.1:2379", modifyURLScheme("http://127.0.0.1:2379", tlsCfg))
re.Equal("https://127.0.0.1:2379", modifyURLScheme("127.0.0.1:2379", tlsCfg))
re.Equal("http://127.0.0.1:2379", modifyURLScheme("https://127.0.0.1:2379", nil))
re.Equal("http://127.0.0.1:2379", modifyURLScheme("http://127.0.0.1:2379", nil))
re.Equal("http://127.0.0.1:2379", modifyURLScheme("127.0.0.1:2379", nil))

urls := []string{
"http://127.0.0.1:2379",
Expand Down

0 comments on commit 34eedb0

Please sign in to comment.