Skip to content

Commit 50f2e9b

Browse files
committed
fix: remove all user visisble changes aside from new NewClient func
1 parent fec57e6 commit 50f2e9b

File tree

6 files changed

+28
-44
lines changed

6 files changed

+28
-44
lines changed

balancer/rls/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ func parseRLSProto(rlsProto *rlspb.RouteLookupConfig) (*lbConfig, error) {
195195
parsedTarget, err := url.Parse(lookupService)
196196
if err != nil {
197197
// url.Parse() fails if scheme is missing. Retry with default scheme.
198-
parsedTarget, err = url.Parse(resolver.GetDefaultSchemeOrPassthrough() + ":///" + lookupService)
198+
parsedTarget, err = url.Parse(resolver.GetDefaultScheme() + ":///" + lookupService)
199199
if err != nil {
200200
return nil, fmt.Errorf("rls: invalid target URI in lookup_service %s", lookupService)
201201
}
202202
}
203203
if parsedTarget.Scheme == "" {
204-
parsedTarget.Scheme = resolver.GetDefaultSchemeOrPassthrough()
204+
parsedTarget.Scheme = resolver.GetDefaultScheme()
205205
}
206206
if resolver.Get(parsedTarget.Scheme) == nil {
207207
return nil, fmt.Errorf("rls: unregistered scheme in lookup_service %s", lookupService)

clientconn.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,11 @@ func (dcs *defaultConfigSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*ires
117117
}, nil
118118
}
119119

120-
// NewClient returns a new client in idle mode.
121-
func NewClient(target string, opts ...DialOption) (conn *ClientConn, err error) {
120+
func newClient(target, defaultScheme string, opts ...DialOption) (conn *ClientConn, err error) {
122121
cc := &ClientConn{
123122
target: target,
124123
conns: make(map[*addrConn]struct{}),
125-
dopts: defaultDialOptions(),
124+
dopts: defaultDialOptions(defaultScheme),
126125
czData: new(channelzData),
127126
}
128127

@@ -191,6 +190,11 @@ func NewClient(target string, opts ...DialOption) (conn *ClientConn, err error)
191190
return cc, nil
192191
}
193192

193+
// NewClient returns a new client in idle mode.
194+
func NewClient(target string, opts ...DialOption) (conn *ClientConn, err error) {
195+
return newClient(target, "dns", opts...)
196+
}
197+
194198
// DialContext creates a client connection to the given target. By default, it's
195199
// a non-blocking dial (the function won't wait for connections to be
196200
// established, and connecting happens in the background). To make it a blocking
@@ -211,8 +215,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
211215
// At the end of this method, we kick the channel out of idle, rather than waiting for the first rpc.
212216
// The eagerConnect option is used to tell the ClientChannel that DialContext was called to make
213217
// this channel, and hence tries to connect immediately.
214-
opts = append(opts, WithEagerConnect())
215-
cc, err := NewClient(target, opts...)
218+
cc, err := newClient(target, "passthrough", opts...)
216219
if err != nil {
217220
return nil, err
218221
}
@@ -1746,9 +1749,9 @@ func (cc *ClientConn) parseTargetAndFindResolver() error {
17461749
// scheme, except when a custom dialer is specified in which case, we should
17471750
// always use passthrough scheme. For either case, we need to respect any overridden
17481751
// global defaults set by the user.
1749-
defScheme := resolver.GetDefaultSchemeOrDNS()
1750-
if cc.dopts.eagerConnect {
1751-
defScheme = resolver.GetDefaultSchemeOrPassthrough()
1752+
defScheme := cc.dopts.defScheme
1753+
if defScheme == "" || internal.UserSetDefaultScheme {
1754+
defScheme = resolver.GetDefaultScheme()
17521755
}
17531756

17541757
channelz.Infof(logger, cc.channelzID, "fallback to scheme %q", defScheme)

clientconn_parsed_target_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
func (s) TestParsedTarget_Success_WithoutCustomDialer(t *testing.T) {
37-
defScheme := resolver.GetDefaultSchemeOrPassthrough()
37+
defScheme := resolver.GetDefaultScheme()
3838
tests := []struct {
3939
target string
4040
wantParsed resolver.Target
@@ -93,7 +93,7 @@ func (s) TestParsedTarget_Failure_WithoutCustomDialer(t *testing.T) {
9393
}
9494

9595
func (s) TestParsedTarget_WithCustomDialer(t *testing.T) {
96-
defScheme := resolver.GetDefaultSchemeOrPassthrough()
96+
defScheme := resolver.GetDefaultScheme()
9797
tests := []struct {
9898
target string
9999
wantParsed resolver.Target

dialoptions.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type dialOptions struct {
7979
resolvers []resolver.Builder
8080
idleTimeout time.Duration
8181
recvBufferPool SharedBufferPool
82-
eagerConnect bool
82+
defScheme string
8383
}
8484

8585
// DialOption configures how we set up the connection.
@@ -488,15 +488,6 @@ func WithUserAgent(s string) DialOption {
488488
})
489489
}
490490

491-
// WithEagerConnect tells the ClientConnection that the connection was created directly
492-
// with either Dial or DialContext, meaning that the connection is automatically put into
493-
// a connecting state.
494-
func WithEagerConnect() DialOption {
495-
return newFuncDialOption(func(o *dialOptions) {
496-
o.eagerConnect = true
497-
})
498-
}
499-
500491
// WithKeepaliveParams returns a DialOption that specifies keepalive parameters
501492
// for the client transport.
502493
func WithKeepaliveParams(kp keepalive.ClientParameters) DialOption {
@@ -641,7 +632,7 @@ func withHealthCheckFunc(f internal.HealthChecker) DialOption {
641632
})
642633
}
643634

644-
func defaultDialOptions() dialOptions {
635+
func defaultDialOptions(defScheme string) dialOptions {
645636
return dialOptions{
646637
copts: transport.ConnectOptions{
647638
ReadBufferSize: defaultReadBufSize,
@@ -653,6 +644,7 @@ func defaultDialOptions() dialOptions {
653644
healthCheckFunc: internal.HealthCheckFunc,
654645
idleTimeout: 30 * time.Minute,
655646
recvBufferPool: nopBufferPool{},
647+
defScheme: defScheme,
656648
}
657649
}
658650

internal/internal.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ var (
197197

198198
// FromOutgoingContextRaw returns the un-merged, intermediary contents of metadata.rawMD.
199199
FromOutgoingContextRaw any // func(context.Context) (metadata.MD, [][]string, bool)
200+
201+
// UserSetDefaultScheme is set to true if the user has overridden the default resolver scheme.
202+
UserSetDefaultScheme bool
200203
)
201204

202205
// HealthChecker defines the signature of the client-side LB channel health checking function.

resolver/resolver.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package resolver
2323
import (
2424
"context"
2525
"fmt"
26+
"google.golang.org/grpc/internal"
2627
"net"
2728
"net/url"
2829
"strings"
@@ -36,9 +37,7 @@ var (
3637
// m is a map from scheme to resolver builder.
3738
m = make(map[string]Builder)
3839
// defaultScheme is the default scheme to use.
39-
defaultScheme = ""
40-
passthroughScheme = "passthrough"
41-
dnsScheme = "dns"
40+
defaultScheme = "dns"
4241
)
4342

4443
// TODO(bar) install dns resolver in init(){}.
@@ -65,32 +64,19 @@ func Get(scheme string) Builder {
6564
}
6665

6766
// SetDefaultScheme sets the default scheme that will be used. The default
68-
// default scheme is "passthrough".
67+
// scheme is "dns".
6968
//
7069
// NOTE: this function must only be called during initialization time (i.e. in
7170
// an init() function), and is not thread-safe. The scheme set last overrides
7271
// previously set values.
7372
func SetDefaultScheme(scheme string) {
73+
internal.UserSetDefaultScheme = true
7474
defaultScheme = scheme
7575
}
7676

77-
// GetDefaultSchemeOrPassthrough gets the default scheme that will be used, if no scheme was
78-
// overridden, defaults to the "passthrough" scheme. This scheme is used when directly calling
79-
// the Dial method to create a client connection.
80-
func GetDefaultSchemeOrPassthrough() string {
81-
if defaultScheme == "" {
82-
return passthroughScheme
83-
}
84-
return defaultScheme
85-
}
86-
87-
// GetDefaultSchemeOrDNS gets the default scheme that will be used, if no scheme was overridden,
88-
// defaults to the "dns" scheme. This is the default scheme when calling NewClient to generate a
89-
// new client connection.
90-
func GetDefaultSchemeOrDNS() string {
91-
if defaultScheme == "" {
92-
return dnsScheme
93-
}
77+
// GetDefaultScheme gets the default scheme that will be used, if no scheme was
78+
// overridden, defaults to the "dns" scheme.
79+
func GetDefaultScheme() string {
9480
return defaultScheme
9581
}
9682

0 commit comments

Comments
 (0)