Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lyuxuan committed Mar 20, 2019
1 parent f0112c1 commit 47bb6ca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
16 changes: 8 additions & 8 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func (cc *ClientConn) scWatcher() {
// We may revisit this decision in the future.
cc.sc = sc
s := ""
cc.scRaw = &s
cc.sc.rawJSONString = &s
cc.mu.Unlock()
case <-cc.ctx.Done():
return
Expand Down Expand Up @@ -479,8 +479,8 @@ func (cc *ClientConn) handleResolvedAddrs(addrs []resolver.Address, err error) {
}

// If resolver does not return service config, apply the default service config if available.
if cc.scRaw == nil && cc.dopts.defaultServiceConfig != nil {
cc.applyServiceConfig(cc.dopts.defaultServiceConfig, cc.dopts.defaultServiceConfigRaw)
if cc.sc.rawJSONString == nil && cc.dopts.defaultServiceConfig != nil {
cc.applyServiceConfig(cc.dopts.defaultServiceConfig, *cc.dopts.defaultServiceConfig.rawJSONString)
}

cc.curAddresses = addrs
Expand Down Expand Up @@ -770,11 +770,11 @@ func (cc *ClientConn) handleServiceConfig(js string) error {
cc.mu.Lock()
defer cc.mu.Unlock()
if cc.dopts.disableServiceConfig {
if cc.dopts.defaultServiceConfig == nil {
if cc.dopts.defaultServiceConfig == nil || cc.scRaw != nil {
return nil
}
// apply default service config when there's resolver service config is disabled.
return cc.applyServiceConfig(cc.dopts.defaultServiceConfig, cc.dopts.defaultServiceConfigRaw)
return cc.applyServiceConfig(cc.dopts.defaultServiceConfig, *cc.dopts.defaultServiceConfig.rawJSONString)
}

return cc.applyServiceConfig(nil, js)
Expand All @@ -784,7 +784,7 @@ func (cc *ClientConn) handleServiceConfig(js string) error {
// a parsed service config, we will skip the parsing. It will also skip the whole processing if
// the new service config is the same as the old one.
func (cc *ClientConn) applyServiceConfig(sc *ServiceConfig, js string) error {
if cc.scRaw != nil && *cc.scRaw == js {
if cc.sc.rawJSONString != nil && *cc.sc.rawJSONString == js {
return nil
}

Expand All @@ -794,7 +794,7 @@ func (cc *ClientConn) applyServiceConfig(sc *ServiceConfig, js string) error {
if err != nil {
return err
}
sc = &scfg
sc = scfg
}

if channelz.IsOn() {
Expand All @@ -811,7 +811,7 @@ func (cc *ClientConn) applyServiceConfig(sc *ServiceConfig, js string) error {
if cc.conns == nil {
return nil
}
cc.scRaw = &js
sc.rawJSONString = &js
cc.sc = *sc

if sc.retryThrottling != nil {
Expand Down
22 changes: 11 additions & 11 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ type dialOptions struct {
// balancer, and also by WithBalancerName dial option.
balancerBuilder balancer.Builder
// This is to support grpclb.
resolverBuilder resolver.Builder
reqHandshake envconfig.RequireHandshakeSetting
channelzParentID int64
disableServiceConfig bool
disableRetry bool
disableHealthCheck bool
healthCheckFunc internal.HealthChecker
defaultServiceConfig *ServiceConfig
defaultServiceConfigRaw string
resolverBuilder resolver.Builder
reqHandshake envconfig.RequireHandshakeSetting
channelzParentID int64
disableServiceConfig bool
disableRetry bool
disableHealthCheck bool
healthCheckFunc internal.HealthChecker
defaultServiceConfig *ServiceConfig
}

// DialOption configures how we set up the connection.
Expand Down Expand Up @@ -452,15 +451,16 @@ func WithDisableServiceConfig() DialOption {
// will be used in cases where:
// 1. WithDisableServiceConfig is called.
// 2. Resolver does not return service config or if the resolver gets and invalid config.
// It is strongly recommended that caller of this function verifies the validity of the input string
// by using the grpc.ValidateServiceConfig function.
func WithDefaultServiceConfig(s string) DialOption {
return newFuncDialOption(func(o *dialOptions) {
sc, err := parseServiceConfig(s)
if err != nil {
grpclog.Warningf("the provided service config is invalid, err: %v", err)
return
}
o.defaultServiceConfig = &sc
o.defaultServiceConfigRaw = s
o.defaultServiceConfig = sc
})
}

Expand Down
17 changes: 11 additions & 6 deletions service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ type ServiceConfig struct {
// healthCheckConfig must be set as one of the requirement to enable LB channel
// health check.
healthCheckConfig *healthCheckConfig
// rawJSONString stores the pointer to the original service config json string that get parsed into
// this service config struct. Null pointer means this is a service config struct just defined,
// but no json string has been parsed and initialize the struct.
rawJSONString *string
}

// healthCheckConfig defines the go-native version of the LB channel health check config.
Expand Down Expand Up @@ -238,12 +242,12 @@ type jsonSC struct {
HealthCheckConfig *healthCheckConfig
}

func parseServiceConfig(js string) (ServiceConfig, error) {
func parseServiceConfig(js string) (*ServiceConfig, error) {
var rsc jsonSC
err := json.Unmarshal([]byte(js), &rsc)
if err != nil {
grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
return ServiceConfig{}, err
return &ServiceConfig{}, err
}
sc := ServiceConfig{
LB: rsc.LoadBalancingPolicy,
Expand All @@ -252,7 +256,7 @@ func parseServiceConfig(js string) (ServiceConfig, error) {
healthCheckConfig: rsc.HealthCheckConfig,
}
if rsc.MethodConfig == nil {
return sc, nil
return &sc, nil
}

for _, m := range *rsc.MethodConfig {
Expand All @@ -262,7 +266,7 @@ func parseServiceConfig(js string) (ServiceConfig, error) {
d, err := parseDuration(m.Timeout)
if err != nil {
grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
return ServiceConfig{}, err
return &ServiceConfig{}, err
}

mc := MethodConfig{
Expand All @@ -271,7 +275,7 @@ func parseServiceConfig(js string) (ServiceConfig, error) {
}
if mc.retryPolicy, err = convertRetryPolicy(m.RetryPolicy); err != nil {
grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
return ServiceConfig{}, err
return &ServiceConfig{}, err
}
if m.MaxRequestMessageBytes != nil {
if *m.MaxRequestMessageBytes > int64(maxInt) {
Expand Down Expand Up @@ -302,7 +306,8 @@ func parseServiceConfig(js string) (ServiceConfig, error) {
sc.retryThrottling = nil
}
}
return sc, nil
sc.rawJSONString = &js
return &sc, nil
}

func convertRetryPolicy(jrp *jsonRetryPolicy) (p *retryPolicy, err error) {
Expand Down
16 changes: 11 additions & 5 deletions service_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (s) TestParseLoadBalancer(t *testing.T) {

for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
if c.wantErr != (err != nil) || !scCompareWithRawJSONSkipped(*sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, *sc, err, c.wantSC, c.wantErr)
}
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func (s) TestParseWaitForReady(t *testing.T) {

for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
if c.wantErr != (err != nil) || !scCompareWithRawJSONSkipped(*sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ func (s) TestPraseTimeOut(t *testing.T) {

for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
if c.wantErr != (err != nil) || !scCompareWithRawJSONSkipped(*sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func (s) TestPraseMsgSize(t *testing.T) {

for _, c := range testcases {
sc, err := parseServiceConfig(c.scjs)
if c.wantErr != (err != nil) || !reflect.DeepEqual(sc, c.wantSC) {
if c.wantErr != (err != nil) || !scCompareWithRawJSONSkipped(*sc, c.wantSC) {
t.Fatalf("parseServiceConfig(%s) = %+v, %v, want %+v, %v", c.scjs, sc, err, c.wantSC, c.wantErr)
}
}
Expand Down Expand Up @@ -384,3 +384,9 @@ func newDuration(b time.Duration) *time.Duration {
func newString(b string) *string {
return &b
}

func scCompareWithRawJSONSkipped(s1, s2 ServiceConfig) bool {
s1.rawJSONString = nil
s2.rawJSONString = nil
return reflect.DeepEqual(s1, s2)
}

0 comments on commit 47bb6ca

Please sign in to comment.