feat(zrpc): change NonBlock default to true following gRPC best practices (#5259)

This commit is contained in:
Kevin Wan
2025-10-26 20:56:34 +08:00
committed by GitHub
parent 98423ca948
commit d6c876860b
4 changed files with 22 additions and 1 deletions

View File

@@ -21,6 +21,9 @@ var (
WithDialOption = internal.WithDialOption WithDialOption = internal.WithDialOption
// WithNonBlock sets the dialing to be nonblock. // WithNonBlock sets the dialing to be nonblock.
WithNonBlock = internal.WithNonBlock WithNonBlock = internal.WithNonBlock
// WithBlock sets the dialing to be blocking.
// Deprecated: blocking dials are not recommended by gRPC.
WithBlock = internal.WithBlock
// WithStreamClientInterceptor is an alias of internal.WithStreamClientInterceptor. // WithStreamClientInterceptor is an alias of internal.WithStreamClientInterceptor.
WithStreamClientInterceptor = internal.WithStreamClientInterceptor WithStreamClientInterceptor = internal.WithStreamClientInterceptor
// WithTimeout is an alias of internal.WithTimeout. // WithTimeout is an alias of internal.WithTimeout.
@@ -61,6 +64,8 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
} }
if c.NonBlock { if c.NonBlock {
opts = append(opts, WithNonBlock()) opts = append(opts, WithNonBlock())
} else {
opts = append(opts, WithBlock())
} }
if c.Timeout > 0 { if c.Timeout > 0 {
opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond)) opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))

View File

@@ -27,7 +27,7 @@ type (
Target string `json:",optional"` Target string `json:",optional"`
App string `json:",optional"` App string `json:",optional"`
Token string `json:",optional"` Token string `json:",optional"`
NonBlock bool `json:",optional"` NonBlock bool `json:",default=true"`
Timeout int64 `json:",default=2000"` Timeout int64 `json:",default=2000"`
KeepaliveTime time.Duration `json:",optional"` KeepaliveTime time.Duration `json:",optional"`
Middlewares ClientMiddlewaresConf Middlewares ClientMiddlewaresConf

View File

@@ -141,6 +141,15 @@ func (c *client) dial(server string, opts ...ClientOption) error {
return nil return nil
} }
// WithBlock sets the dialing to be blocking.
// Deprecated: blocking dials are not recommended by gRPC.
// See https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md
func WithBlock() ClientOption {
return func(options *ClientOptions) {
options.NonBlock = false
}
}
// WithDialOption returns a func to customize a ClientOptions with given dial option. // WithDialOption returns a func to customize a ClientOptions with given dial option.
func WithDialOption(opt grpc.DialOption) ClientOption { func WithDialOption(opt grpc.DialOption) ClientOption {
return func(options *ClientOptions) { return func(options *ClientOptions) {

View File

@@ -34,6 +34,13 @@ func TestWithNonBlock(t *testing.T) {
assert.True(t, options.NonBlock) assert.True(t, options.NonBlock)
} }
func TestWithBlock(t *testing.T) {
var options ClientOptions
opt := WithBlock()
opt(&options)
assert.False(t, options.NonBlock)
}
func TestWithStreamClientInterceptor(t *testing.T) { func TestWithStreamClientInterceptor(t *testing.T) {
var options ClientOptions var options ClientOptions
opt := WithStreamClientInterceptor(func(ctx context.Context, desc *grpc.StreamDesc, opt := WithStreamClientInterceptor(func(ctx context.Context, desc *grpc.StreamDesc,