Skip to content

Commit

Permalink
feat(transport/grpc): grpc client supports health check option (#3067)
Browse files Browse the repository at this point in the history
  • Loading branch information
youzhixiaomutou authored Nov 2, 2023
1 parent 6cdd818 commit 3fc8fb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion transport/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ func WithNodeFilter(filters ...selector.NodeFilter) ClientOption {
}
}

// WithHealthCheck with health check
func WithHealthCheck(healthCheck bool) ClientOption {
return func(o *clientOptions) {
if !healthCheck {
o.healthCheckConfig = ""
}
}
}

// WithLogger with logger
// Deprecated: use global logger instead.
func WithLogger(_ log.Logger) ClientOption {
Expand All @@ -128,6 +137,7 @@ type clientOptions struct {
grpcOpts []grpc.DialOption
balancerName string
filters []selector.NodeFilter
healthCheckConfig string
printDiscoveryDebugLog bool
}

Expand All @@ -147,6 +157,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
balancerName: balancerName,
subsetSize: 25,
printDiscoveryDebugLog: true,
healthCheckConfig: `,"healthCheckConfig":{"serviceName":""}`,
}
for _, o := range opts {
o(&options)
Expand All @@ -165,7 +176,8 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
sints = append(sints, options.streamInts...)
}
grpcOpts := []grpc.DialOption{
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingConfig": [{"%s":{}}],"healthCheckConfig":{"serviceName":""}}`, options.balancerName)),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingConfig": [{"%s":{}}]%s}`,
options.balancerName, options.healthCheckConfig)),
grpc.WithChainUnaryInterceptor(ints...),
grpc.WithChainStreamInterceptor(sints...),
}
Expand Down
10 changes: 10 additions & 0 deletions transport/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func TestWithOptions(t *testing.T) {
}
}

func TestWithHealthCheck(t *testing.T) {
o := &clientOptions{
healthCheckConfig: `,"healthCheckConfig":{"serviceName":""}`,
}
WithHealthCheck(false)(o)
if !reflect.DeepEqual("", o.healthCheckConfig) {
t.Errorf("expect %v but got %v", "", o.healthCheckConfig)
}
}

func TestDial(t *testing.T) {
o := &clientOptions{}
v := []grpc.DialOption{
Expand Down

0 comments on commit 3fc8fb7

Please sign in to comment.