mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
feat: support grpc client keepalive config (#2950)
This commit is contained in:
parent
265a24ac6d
commit
d41e542c92
@ -8,8 +8,11 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/zrpc/internal/auth"
|
"github.com/zeromicro/go-zero/zrpc/internal/auth"
|
||||||
"github.com/zeromicro/go-zero/zrpc/internal/clientinterceptors"
|
"github.com/zeromicro/go-zero/zrpc/internal/clientinterceptors"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/keepalive"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultClientKeepaliveTime = 20 * time.Second
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// WithDialOption is an alias of internal.WithDialOption.
|
// WithDialOption is an alias of internal.WithDialOption.
|
||||||
WithDialOption = internal.WithDialOption
|
WithDialOption = internal.WithDialOption
|
||||||
@ -62,6 +65,11 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
|
|||||||
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))
|
||||||
}
|
}
|
||||||
|
if c.KeepaliveTime > 0 {
|
||||||
|
opts = append(opts, WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||||
|
Time: c.KeepaliveTime,
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
|
||||||
opts = append(opts, options...)
|
opts = append(opts, options...)
|
||||||
|
|
||||||
@ -90,6 +98,12 @@ func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
|
|||||||
Timeout: true,
|
Timeout: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts = append([]ClientOption{
|
||||||
|
WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||||
|
Time: defaultClientKeepaliveTime,
|
||||||
|
})),
|
||||||
|
}, opts...)
|
||||||
|
|
||||||
return internal.NewClient(target, middlewares, opts...)
|
return internal.NewClient(target, middlewares, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,6 +117,7 @@ func TestDepositServer_Deposit(t *testing.T) {
|
|||||||
App: "foo",
|
App: "foo",
|
||||||
Token: "bar",
|
Token: "bar",
|
||||||
Timeout: 1000,
|
Timeout: 1000,
|
||||||
|
KeepaliveTime: time.Second * 15,
|
||||||
Middlewares: ClientMiddlewaresConf{
|
Middlewares: ClientMiddlewaresConf{
|
||||||
Trace: true,
|
Trace: true,
|
||||||
Duration: true,
|
Duration: true,
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package zrpc
|
package zrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/discov"
|
"github.com/zeromicro/go-zero/core/discov"
|
||||||
"github.com/zeromicro/go-zero/core/service"
|
"github.com/zeromicro/go-zero/core/service"
|
||||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||||
@ -14,6 +16,19 @@ type (
|
|||||||
// ServerMiddlewaresConf defines whether to use server middlewares.
|
// ServerMiddlewaresConf defines whether to use server middlewares.
|
||||||
ServerMiddlewaresConf = internal.ServerMiddlewaresConf
|
ServerMiddlewaresConf = internal.ServerMiddlewaresConf
|
||||||
|
|
||||||
|
// 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:",default=20s"`
|
||||||
|
Middlewares ClientMiddlewaresConf
|
||||||
|
}
|
||||||
|
|
||||||
// A RpcServerConf is a rpc server config.
|
// A RpcServerConf is a rpc server config.
|
||||||
RpcServerConf struct {
|
RpcServerConf struct {
|
||||||
service.ServiceConf
|
service.ServiceConf
|
||||||
@ -29,18 +44,6 @@ type (
|
|||||||
Health bool `json:",default=true"`
|
Health bool `json:",default=true"`
|
||||||
Middlewares ServerMiddlewaresConf
|
Middlewares ServerMiddlewaresConf
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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"`
|
|
||||||
Middlewares ClientMiddlewaresConf
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDirectClientConf returns a RpcClientConf.
|
// NewDirectClientConf returns a RpcClientConf.
|
||||||
|
Loading…
Reference in New Issue
Block a user