feat(transport/grpc): gRPC client discovery supports incoming logger and timeout (#1702)

pull/1706/head
包子 3 years ago committed by GitHub
parent 72f9cc3518
commit 5aeb14d381
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      transport/grpc/client.go
  2. 23
      transport/grpc/client_test.go

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/selector" "github.com/go-kratos/kratos/v2/selector"
@ -87,6 +88,13 @@ func WithFilter(filters ...selector.Filter) ClientOption {
} }
} }
// WithLogger with logger
func WithLogger(log log.Logger) ClientOption {
return func(o *clientOptions) {
o.logger = log
}
}
// clientOptions is gRPC Client // clientOptions is gRPC Client
type clientOptions struct { type clientOptions struct {
endpoint string endpoint string
@ -98,6 +106,7 @@ type clientOptions struct {
grpcOpts []grpc.DialOption grpcOpts []grpc.DialOption
balancerName string balancerName string
filters []selector.Filter filters []selector.Filter
logger log.Logger
} }
// Dial returns a GRPC connection. // Dial returns a GRPC connection.
@ -114,6 +123,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
options := clientOptions{ options := clientOptions{
timeout: 2000 * time.Millisecond, timeout: 2000 * time.Millisecond,
balancerName: wrr.Name, balancerName: wrr.Name,
logger: log.DefaultLogger,
} }
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
@ -129,7 +139,14 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
grpc.WithChainUnaryInterceptor(ints...), grpc.WithChainUnaryInterceptor(ints...),
} }
if options.discovery != nil { if options.discovery != nil {
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discovery, discovery.WithInsecure(insecure)))) grpcOpts = append(grpcOpts,
grpc.WithResolvers(
discovery.NewBuilder(
options.discovery,
discovery.WithInsecure(insecure),
discovery.WithLogger(options.logger),
discovery.WithTimeout(options.timeout),
)))
} }
if insecure { if insecure {
grpcOpts = append(grpcOpts, grpc.WithInsecure()) grpcOpts = append(grpcOpts, grpc.WithInsecure())

@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -59,6 +60,13 @@ func TestWithTLSConfig(t *testing.T) {
assert.Equal(t, v, o.tlsConf) assert.Equal(t, v, o.tlsConf)
} }
func TestWithLogger(t *testing.T) {
o := &clientOptions{}
v := log.DefaultLogger
WithLogger(v)(o)
assert.Equal(t, v, o.logger)
}
func EmptyMiddleware() middleware.Middleware { func EmptyMiddleware() middleware.Middleware {
return func(handler middleware.Handler) middleware.Handler { return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
@ -112,3 +120,18 @@ func TestDial(t *testing.T) {
WithOptions(v...)(o) WithOptions(v...)(o)
assert.Equal(t, v, o.grpcOpts) assert.Equal(t, v, o.grpcOpts)
} }
func TestDialConn(t *testing.T) {
_, err := dial(
context.Background(),
true,
WithDiscovery(&mockRegistry{}),
WithTimeout(10*time.Second),
WithLogger(log.DefaultLogger),
WithEndpoint("abc"),
WithMiddleware(EmptyMiddleware()),
)
if err != nil {
t.Error(err)
}
}

Loading…
Cancel
Save