client: add an option to disable global dial options (#5990)

This commit is contained in:
Zach Reyes 2023-01-27 17:06:29 -05:00 committed by GitHub
parent c813c17a33
commit 6a707eb1bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 19 deletions

View File

@ -133,6 +133,10 @@ func (dcs *defaultConfigSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*ires
// https://github.com/grpc/grpc/blob/master/doc/naming.md. // https://github.com/grpc/grpc/blob/master/doc/naming.md.
// e.g. to use dns resolver, a "dns:///" prefix should be applied to the target. // e.g. to use dns resolver, a "dns:///" prefix should be applied to the target.
func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) { func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) {
return dialWithGlobalOptions(ctx, target, false, opts...)
}
func dialWithGlobalOptions(ctx context.Context, target string, disableGlobalOptions bool, opts ...DialOption) (conn *ClientConn, err error) {
cc := &ClientConn{ cc := &ClientConn{
target: target, target: target,
csMgr: &connectivityStateManager{}, csMgr: &connectivityStateManager{},
@ -146,8 +150,10 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil}) cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil})
cc.ctx, cc.cancel = context.WithCancel(context.Background()) cc.ctx, cc.cancel = context.WithCancel(context.Background())
for _, opt := range extraDialOptions { if !disableGlobalOptions {
opt.apply(&cc.dopts) for _, opt := range globalDialOptions {
opt.apply(&cc.dopts)
}
} }
for _, opt := range opts { for _, opt := range opts {

View File

@ -19,6 +19,7 @@
package grpc package grpc
import ( import (
"context"
"strings" "strings"
"testing" "testing"
@ -26,7 +27,7 @@ import (
"google.golang.org/grpc/internal" "google.golang.org/grpc/internal"
) )
func (s) TestAddExtraDialOptions(t *testing.T) { func (s) TestAddGlobalDialOptions(t *testing.T) {
// Ensure the Dial fails without credentials // Ensure the Dial fails without credentials
if _, err := Dial("fake"); err == nil { if _, err := Dial("fake"); err == nil {
t.Fatalf("Dialing without a credential did not fail") t.Fatalf("Dialing without a credential did not fail")
@ -40,8 +41,8 @@ func (s) TestAddExtraDialOptions(t *testing.T) {
opts := []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials())} opts := []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials())}
internal.AddGlobalDialOptions.(func(opt ...DialOption))(opts...) internal.AddGlobalDialOptions.(func(opt ...DialOption))(opts...)
for i, opt := range opts { for i, opt := range opts {
if extraDialOptions[i] != opt { if globalDialOptions[i] != opt {
t.Fatalf("Unexpected extra dial option at index %d: %v != %v", i, extraDialOptions[i], opt) t.Fatalf("Unexpected global dial option at index %d: %v != %v", i, globalDialOptions[i], opt)
} }
} }
@ -53,19 +54,37 @@ func (s) TestAddExtraDialOptions(t *testing.T) {
} }
internal.ClearGlobalDialOptions() internal.ClearGlobalDialOptions()
if len(extraDialOptions) != 0 { if len(globalDialOptions) != 0 {
t.Fatalf("Unexpected len of extraDialOptions: %d != 0", len(extraDialOptions)) t.Fatalf("Unexpected len of globalDialOptions: %d != 0", len(globalDialOptions))
} }
} }
func (s) TestAddExtraServerOptions(t *testing.T) { // TestDisableGlobalOptions tests dialing with a bit that disables global
// options. Dialing with this bit set should not pick up global options.
func (s) TestDisableGlobalOptions(t *testing.T) {
// Set transport credentials as a global option.
internal.AddGlobalDialOptions.(func(opt ...DialOption))(WithTransportCredentials(insecure.NewCredentials()))
// Dial with disable global options set to true. This Dial should fail due
// to the global dial options with credentials not being picked up due to it
// being disabled.
if _, err := internal.DialWithGlobalOptions.(func(context.Context, string, bool, ...DialOption) (*ClientConn, error))(context.Background(), "fake", true); err == nil {
t.Fatalf("Dialing without a credential did not fail")
} else {
if !strings.Contains(err.Error(), "no transport security set") {
t.Fatalf("Dialing failed with unexpected error: %v", err)
}
}
internal.ClearGlobalDialOptions()
}
func (s) TestAddGlobalServerOptions(t *testing.T) {
const maxRecvSize = 998765 const maxRecvSize = 998765
// Set and check the ServerOptions // Set and check the ServerOptions
opts := []ServerOption{Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize)} opts := []ServerOption{Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize)}
internal.AddGlobalServerOptions.(func(opt ...ServerOption))(opts...) internal.AddGlobalServerOptions.(func(opt ...ServerOption))(opts...)
for i, opt := range opts { for i, opt := range opts {
if extraServerOptions[i] != opt { if globalServerOptions[i] != opt {
t.Fatalf("Unexpected extra server option at index %d: %v != %v", i, extraServerOptions[i], opt) t.Fatalf("Unexpected global server option at index %d: %v != %v", i, globalServerOptions[i], opt)
} }
} }
@ -76,8 +95,8 @@ func (s) TestAddExtraServerOptions(t *testing.T) {
} }
internal.ClearGlobalServerOptions() internal.ClearGlobalServerOptions()
if len(extraServerOptions) != 0 { if len(globalServerOptions) != 0 {
t.Fatalf("Unexpected len of extraServerOptions: %d != 0", len(extraServerOptions)) t.Fatalf("Unexpected len of globalServerOptions: %d != 0", len(globalServerOptions))
} }
} }

View File

@ -38,11 +38,12 @@ import (
func init() { func init() {
internal.AddGlobalDialOptions = func(opt ...DialOption) { internal.AddGlobalDialOptions = func(opt ...DialOption) {
extraDialOptions = append(extraDialOptions, opt...) globalDialOptions = append(globalDialOptions, opt...)
} }
internal.ClearGlobalDialOptions = func() { internal.ClearGlobalDialOptions = func() {
extraDialOptions = nil globalDialOptions = nil
} }
internal.DialWithGlobalOptions = dialWithGlobalOptions
internal.WithBinaryLogger = withBinaryLogger internal.WithBinaryLogger = withBinaryLogger
internal.JoinDialOptions = newJoinDialOption internal.JoinDialOptions = newJoinDialOption
} }
@ -83,7 +84,7 @@ type DialOption interface {
apply(*dialOptions) apply(*dialOptions)
} }
var extraDialOptions []DialOption var globalDialOptions []DialOption
// EmptyDialOption does not alter the dial configuration. It can be embedded in // EmptyDialOption does not alter the dial configuration. It can be embedded in
// another structure to build custom dial options. // another structure to build custom dial options.

View File

@ -29,6 +29,9 @@ import (
) )
var ( var (
// DialWithGlobalOptions dials with a knob on whether to disable global dial
// options (set via AddGlobalDialOptions).
DialWithGlobalOptions interface{} // func (context.Context, string, bool, ...DialOption) (*ClientConn, error)
// WithHealthCheckFunc is set by dialoptions.go // WithHealthCheckFunc is set by dialoptions.go
WithHealthCheckFunc interface{} // func (HealthChecker) DialOption WithHealthCheckFunc interface{} // func (HealthChecker) DialOption
// HealthCheckFunc is used to provide client-side LB channel health checking // HealthCheckFunc is used to provide client-side LB channel health checking

View File

@ -74,10 +74,10 @@ func init() {
srv.drainServerTransports(addr) srv.drainServerTransports(addr)
} }
internal.AddGlobalServerOptions = func(opt ...ServerOption) { internal.AddGlobalServerOptions = func(opt ...ServerOption) {
extraServerOptions = append(extraServerOptions, opt...) globalServerOptions = append(globalServerOptions, opt...)
} }
internal.ClearGlobalServerOptions = func() { internal.ClearGlobalServerOptions = func() {
extraServerOptions = nil globalServerOptions = nil
} }
internal.BinaryLogger = binaryLogger internal.BinaryLogger = binaryLogger
internal.JoinServerOptions = newJoinServerOption internal.JoinServerOptions = newJoinServerOption
@ -183,7 +183,7 @@ var defaultServerOptions = serverOptions{
writeBufferSize: defaultWriteBufSize, writeBufferSize: defaultWriteBufSize,
readBufferSize: defaultReadBufSize, readBufferSize: defaultReadBufSize,
} }
var extraServerOptions []ServerOption var globalServerOptions []ServerOption
// A ServerOption sets options such as credentials, codec and keepalive parameters, etc. // A ServerOption sets options such as credentials, codec and keepalive parameters, etc.
type ServerOption interface { type ServerOption interface {
@ -600,7 +600,7 @@ func (s *Server) stopServerWorkers() {
// started to accept requests yet. // started to accept requests yet.
func NewServer(opt ...ServerOption) *Server { func NewServer(opt ...ServerOption) *Server {
opts := defaultServerOptions opts := defaultServerOptions
for _, o := range extraServerOptions { for _, o := range globalServerOptions {
o.apply(&opts) o.apply(&opts)
} }
for _, o := range opt { for _, o := range opt {