feat: use WithBlock() by default, NonBlock can be set in config or WithNonBlock() (#1198)
This commit is contained in:
@@ -14,6 +14,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
// WithDialOption is an alias of internal.WithDialOption.
|
// WithDialOption is an alias of internal.WithDialOption.
|
||||||
WithDialOption = internal.WithDialOption
|
WithDialOption = internal.WithDialOption
|
||||||
|
// WithNonBlock sets the dialing to be nonblock.
|
||||||
|
WithNonBlock = internal.WithNonBlock
|
||||||
// WithTimeout is an alias of internal.WithTimeout.
|
// WithTimeout is an alias of internal.WithTimeout.
|
||||||
WithTimeout = internal.WithTimeout
|
WithTimeout = internal.WithTimeout
|
||||||
// WithRetry is an alias of internal.WithRetry.
|
// WithRetry is an alias of internal.WithRetry.
|
||||||
@@ -57,6 +59,9 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
|
|||||||
Token: c.Token,
|
Token: c.Token,
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
if c.NonBlock {
|
||||||
|
opts = append(opts, WithNonBlock())
|
||||||
|
}
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,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"`
|
||||||
Retry bool `json:",optional"` // grpc auto retry
|
Retry bool `json:",optional"` // grpc auto retry
|
||||||
Timeout int64 `json:",default=2000"`
|
Timeout int64 `json:",default=2000"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ type (
|
|||||||
|
|
||||||
// A ClientOptions is a client options.
|
// A ClientOptions is a client options.
|
||||||
ClientOptions struct {
|
ClientOptions struct {
|
||||||
|
NonBlock bool
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Secure bool
|
Secure bool
|
||||||
Retry bool
|
Retry bool
|
||||||
@@ -75,8 +76,11 @@ func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
|
|||||||
options = append([]grpc.DialOption(nil), grpc.WithInsecure())
|
options = append([]grpc.DialOption(nil), grpc.WithInsecure())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !cliOpts.NonBlock {
|
||||||
|
options = append(options, grpc.WithBlock())
|
||||||
|
}
|
||||||
|
|
||||||
options = append(options,
|
options = append(options,
|
||||||
grpc.WithBlock(),
|
|
||||||
WithUnaryClientInterceptors(
|
WithUnaryClientInterceptors(
|
||||||
clientinterceptors.UnaryTracingInterceptor,
|
clientinterceptors.UnaryTracingInterceptor,
|
||||||
clientinterceptors.DurationInterceptor,
|
clientinterceptors.DurationInterceptor,
|
||||||
@@ -122,6 +126,13 @@ func WithDialOption(opt grpc.DialOption) ClientOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithNonBlock sets the dialing to be nonblock.
|
||||||
|
func WithNonBlock() ClientOption {
|
||||||
|
return func(options *ClientOptions) {
|
||||||
|
options.NonBlock = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithTimeout returns a func to customize a ClientOptions with given timeout.
|
// WithTimeout returns a func to customize a ClientOptions with given timeout.
|
||||||
func WithTimeout(timeout time.Duration) ClientOption {
|
func WithTimeout(timeout time.Duration) ClientOption {
|
||||||
return func(options *ClientOptions) {
|
return func(options *ClientOptions) {
|
||||||
|
|||||||
@@ -31,6 +31,13 @@ func TestWithRetry(t *testing.T) {
|
|||||||
assert.True(t, options.Retry)
|
assert.True(t, options.Retry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithNonBlock(t *testing.T) {
|
||||||
|
var options ClientOptions
|
||||||
|
opt := WithNonBlock()
|
||||||
|
opt(&options)
|
||||||
|
assert.True(t, options.NonBlock)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWithUnaryClientInterceptor(t *testing.T) {
|
func TestWithUnaryClientInterceptor(t *testing.T) {
|
||||||
var options ClientOptions
|
var options ClientOptions
|
||||||
opt := WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
|
opt := WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply interface{},
|
||||||
|
|||||||
Reference in New Issue
Block a user