86
86
// errCredentialsConflict indicates that grpc.WithTransportCredentials()
87
87
// and grpc.WithInsecure() are both called for a connection.
88
88
errCredentialsConflict = errors .New ("grpc: transport credentials are set for an insecure connection (grpc.WithTransportCredentials() and grpc.WithInsecure() are both called)" )
89
+ // errInvalidDefaultServiceConfig indicates that grpc.WithDefaultServiceConfig(string) provides
90
+ // an invalid service config string.
91
+ errInvalidDefaultServiceConfig = errors .New ("grpc: the provided default service config is invalid" )
89
92
)
90
93
91
94
const (
@@ -173,6 +176,13 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
173
176
}
174
177
}
175
178
179
+ if cc .dopts .defaultServiceConfigRawJSON != nil {
180
+ sc , err := parseServiceConfig (* cc .dopts .defaultServiceConfigRawJSON )
181
+ if err != nil {
182
+ return nil , errInvalidDefaultServiceConfig
183
+ }
184
+ cc .dopts .defaultServiceConfig = sc
185
+ }
176
186
cc .mkp = cc .dopts .copts .KeepaliveParams
177
187
178
188
if cc .dopts .copts .Dialer == nil {
@@ -457,26 +467,25 @@ func (cc *ClientConn) waitForResolvedAddrs(ctx context.Context) error {
457
467
}
458
468
}
459
469
460
- // Apply default service config when default service config is configured and :
470
+ // gRPC should resort to default service config when :
461
471
// * resolver service config is disabled
462
472
// * or, resolver does not return a service config or returns an invalid one.
463
- func (cc * ClientConn ) shouldApplyDefaultServiceConfig (sc string ) bool {
464
- if cc .dopts .defaultServiceConfig != nil {
465
- if cc .dopts .disableServiceConfig {
466
- return true
467
- }
468
- // The logic below is temporary, will be removed once we change the resolver.State ServiceConfig field type.
469
- // Right now, we assume that empty service config string means resolver does not return a config.
470
- if sc == "" {
471
- return false
472
- }
473
- // TODO: the logic below is temporary. Once we finish the logic to validate service config
474
- // in resolver, we will replace the logic below.
475
- _ , err := parseServiceConfig (sc )
476
- if err != nil {
477
- return true
478
- }
473
+ func (cc * ClientConn ) fallbackToDefaultServiceConfig (sc string ) bool {
474
+ if cc .dopts .disableServiceConfig {
475
+ return true
479
476
}
477
+ // The logic below is temporary, will be removed once we change the resolver.State ServiceConfig field type.
478
+ // Right now, we assume that empty service config string means resolver does not return a config.
479
+ if sc == "" {
480
+ return true
481
+ }
482
+ // TODO: the logic below is temporary. Once we finish the logic to validate service config
483
+ // in resolver, we will replace the logic below.
484
+ _ , err := parseServiceConfig (sc )
485
+ if err != nil {
486
+ return true
487
+ }
488
+
480
489
return false
481
490
}
482
491
@@ -490,14 +499,14 @@ func (cc *ClientConn) updateResolverState(s resolver.State) error {
490
499
return nil
491
500
}
492
501
493
- if cc .shouldApplyDefaultServiceConfig (s .ServiceConfig ) {
494
- if cc .sc .rawJSONString == nil {
502
+ if cc .fallbackToDefaultServiceConfig (s .ServiceConfig ) {
503
+ if cc .dopts . defaultServiceConfig != nil && cc . sc .rawJSONString == nil {
495
504
cc .applyServiceConfig (cc .dopts .defaultServiceConfig )
496
505
}
497
506
} else {
498
507
// TODO: the parsing logic below will be moved inside resolver.
499
508
sc , err := parseServiceConfig (s .ServiceConfig )
500
- if err != nil && s . ServiceConfig != "" { // s.ServiceConfig != "" is a temporary special case.
509
+ if err != nil {
501
510
return err
502
511
}
503
512
if cc .sc .rawJSONString == nil || * cc .sc .rawJSONString != s .ServiceConfig {
@@ -763,11 +772,9 @@ func (cc *ClientConn) getTransport(ctx context.Context, failfast bool, method st
763
772
return t , done , nil
764
773
}
765
774
766
- // Parse and apply the service config. If sc is passed as a non-nil pointer, which indicates we have
767
- // a parsed service config, we will skip the parsing. It will also skip the whole processing if
768
- // the new service config is the same as the old one.
769
775
func (cc * ClientConn ) applyServiceConfig (sc * ServiceConfig ) error {
770
776
if sc == nil {
777
+ // should never reach here.
771
778
return fmt .Errorf ("got nil pointer for service config" )
772
779
}
773
780
cc .sc = * sc
0 commit comments