feat: support grpc client keepalive config (#2950)
This commit is contained in:
@@ -8,8 +8,11 @@ import (
|
||||
"github.com/zeromicro/go-zero/zrpc/internal/auth"
|
||||
"github.com/zeromicro/go-zero/zrpc/internal/clientinterceptors"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
const defaultClientKeepaliveTime = 20 * time.Second
|
||||
|
||||
var (
|
||||
// WithDialOption is an alias of internal.WithDialOption.
|
||||
WithDialOption = internal.WithDialOption
|
||||
@@ -62,6 +65,11 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
|
||||
if c.Timeout > 0 {
|
||||
opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
|
||||
}
|
||||
if c.KeepaliveTime > 0 {
|
||||
opts = append(opts, WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: c.KeepaliveTime,
|
||||
})))
|
||||
}
|
||||
|
||||
opts = append(opts, options...)
|
||||
|
||||
@@ -90,6 +98,12 @@ func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
|
||||
Timeout: true,
|
||||
}
|
||||
|
||||
opts = append([]ClientOption{
|
||||
WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: defaultClientKeepaliveTime,
|
||||
})),
|
||||
}, opts...)
|
||||
|
||||
return internal.NewClient(target, middlewares, opts...)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user