Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update grpc client connection #4415

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions zrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
Token: c.Token,
})))
}
if c.NonBlock {
opts = append(opts, WithNonBlock())
}
if c.Timeout > 0 {
opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
}
Expand Down
40 changes: 12 additions & 28 deletions zrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,8 @@ func TestDepositServer_Deposit(t *testing.T) {
},
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
nonBlockClient := MustNewClient(
RpcClientConf{
Endpoints: []string{"foo"},
App: "foo",
Token: "bar",
Timeout: 1000,
NonBlock: true,
Middlewares: ClientMiddlewaresConf{
Trace: true,
Duration: true,
Prometheus: true,
Breaker: true,
Timeout: true,
},
},
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
Expand All @@ -134,21 +114,22 @@ func TestDepositServer_Deposit(t *testing.T) {
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
targetClient, err := NewClientWithTarget("foo",
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())), WithUnaryClientInterceptor(
func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn,
invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
return invoker(ctx, method, req, reply, cc, opts...)
}), WithTimeout(1000*time.Millisecond))
assert.Nil(t, err)
clients := []Client{
directClient,
nonBlockClient,
tarConfClient,
targetClient,
}
Expand Down Expand Up @@ -208,7 +189,8 @@ func TestNewClientWithError(t *testing.T) {
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
Expand All @@ -227,7 +209,8 @@ func TestNewClientWithError(t *testing.T) {
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
Expand All @@ -239,7 +222,8 @@ func TestNewClientWithTarget(t *testing.T) {
WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
WithDialOption(grpc.WithContextDialer(dialer())),
WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
return invoker(ctx, method, req, reply, cc, opts...)
}))

Expand Down
17 changes: 9 additions & 8 deletions zrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ type (

// A RpcClientConf is a rpc client config.
RpcClientConf struct {
Etcd discov.EtcdConf `json:",optional,inherit"`
Endpoints []string `json:",optional"`
Target string `json:",optional"`
App string `json:",optional"`
Token string `json:",optional"`
NonBlock bool `json:",optional"`
Timeout int64 `json:",default=2000"`
KeepaliveTime time.Duration `json:",optional"`
Etcd discov.EtcdConf `json:",optional,inherit"`
Endpoints []string `json:",optional"`
Target string `json:",optional"`
App string `json:",optional"`
Token string `json:",optional"`
// Deprecated: remove in the future
NonBlock bool `json:",optional"`
Timeout int64 `json:",default=2000"`
KeepaliveTime time.Duration `json:",optional"`
Middlewares ClientMiddlewaresConf
}

Expand Down
10 changes: 3 additions & 7 deletions zrpc/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (

// A ClientOptions is a client options.
ClientOptions struct {
// Deprecated: remove in the future
NonBlock bool
Timeout time.Duration
Secure bool
Expand Down Expand Up @@ -79,10 +80,6 @@ func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
grpc.WithTransportCredentials(insecure.NewCredentials()))
}

if !cliOpts.NonBlock {
options = append(options, grpc.WithBlock())
}

options = append(options,
grpc.WithChainUnaryInterceptor(c.buildUnaryInterceptors(cliOpts.Timeout)...),
grpc.WithChainStreamInterceptor(c.buildStreamInterceptors()...),
Expand Down Expand Up @@ -125,9 +122,7 @@ func (c *client) buildUnaryInterceptors(timeout time.Duration) []grpc.UnaryClien

func (c *client) dial(server string, opts ...ClientOption) error {
options := c.buildDialOptions(opts...)
timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
defer cancel()
conn, err := grpc.DialContext(timeCtx, server, options...)
conn, err := grpc.NewClient(server, options...)
if err != nil {
service := server
if errors.Is(err, context.DeadlineExceeded) {
Expand All @@ -153,6 +148,7 @@ func WithDialOption(opt grpc.DialOption) ClientOption {
}

// WithNonBlock sets the dialing to be nonblock.
// Deprecated: remove in the future
func WithNonBlock() ClientOption {
return func(options *ClientOptions) {
options.NonBlock = true
Expand Down
Loading