Skip to content

Commit 339a5a6

Browse files
feat:add keepalive config and pass through to grpc dial option (#87)
* feat:add keepalive config to pass through to GRPC client config in dubbogo * feat:add keepalive config to pass through to GRPC client
1 parent df20316 commit 339a5a6

5 files changed

Lines changed: 30 additions & 3 deletions

File tree

pkg/common/constant/constant.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package constant
1919

20-
import "time"
20+
import (
21+
"time"
22+
)
2123

2224
// transfer
2325
const (

pkg/config/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package config
1919

2020
import (
2121
"time"
22+
)
2223

24+
import (
2325
"github.com/dubbogo/triple/pkg/common/constant"
2426
loggerInterface "github.com/dubbogo/triple/pkg/common/logger"
2527
"github.com/dubbogo/triple/pkg/common/logger/default_logger"
@@ -47,6 +49,8 @@ type Option struct {
4749
GRPCMaxServerSendMsgSize int
4850
GRPCMaxCallRecvMsgSize int
4951
GRPCMaxServerRecvMsgSize int
52+
GRPCKeepAliveTime time.Duration
53+
GRPCKeepAliveTimeout time.Duration
5054

5155
// tracing
5256
JaegerAddress string
@@ -207,6 +211,18 @@ func WithGRPCMaxServerRecvMessageSize(maxServerRecvMsgSize int) OptionFunction {
207211
}
208212
}
209213

214+
func WithGRPCKeepAliveTimeInterval(keepAliveInterval time.Duration) OptionFunction {
215+
return func(o *Option) {
216+
o.GRPCKeepAliveTime = keepAliveInterval
217+
}
218+
}
219+
220+
func WithGRPCKeepAliveTimeout(keepAliveTimeout time.Duration) OptionFunction {
221+
return func(o *Option) {
222+
o.GRPCKeepAliveTimeout = keepAliveTimeout
223+
}
224+
}
225+
210226
func WithJaegerConfig(address, serviceName string, useAgent bool) OptionFunction {
211227
return func(o *Option) {
212228
o.JaegerAddress = address

pkg/config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222
"time"
2323
)
24+
2425
import (
2526
"github.com/stretchr/testify/assert"
2627
)

pkg/triple/dubbo3_client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/dubbogo/grpc-go/encoding/msgpack"
3737
"github.com/dubbogo/grpc-go/encoding/raw_proto"
3838
"github.com/dubbogo/grpc-go/encoding/tools"
39+
"github.com/dubbogo/grpc-go/keepalive"
3940
"github.com/dubbogo/grpc-go/status"
4041

4142
"github.com/opentracing/opentracing-go"
@@ -110,6 +111,14 @@ func NewTripleClient(impl interface{}, opt *config.Option) (*TripleClient, error
110111
defaultCallOpts = append(defaultCallOpts, grpc.MaxCallRecvMsgSize(opt.GRPCMaxCallRecvMsgSize))
111112
}
112113
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(defaultCallOpts...))
114+
//keepalive
115+
if opt.GRPCKeepAliveTime != 0 && opt.GRPCKeepAliveTimeout != 0 {
116+
dialOpts = append(dialOpts, grpc.WithKeepaliveParams(keepalive.ClientParameters{
117+
Time: opt.GRPCKeepAliveTime,
118+
Timeout: opt.GRPCKeepAliveTimeout,
119+
PermitWithoutStream: true,
120+
}))
121+
}
113122

114123
// codec
115124
if opt.CodecType == constant.PBCodecName {

pkg/triple/dubbo3_server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
hessian "github.com/apache/dubbo-go-hessian2"
3333

3434
"github.com/dubbogo/grpc-go"
35+
"github.com/dubbogo/grpc-go/credentials"
3536
"github.com/dubbogo/grpc-go/credentials/insecure"
3637
"github.com/dubbogo/grpc-go/encoding"
3738
hessianGRPCCodec "github.com/dubbogo/grpc-go/encoding/hessian"
@@ -40,8 +41,6 @@ import (
4041
"github.com/dubbogo/grpc-go/encoding/raw_proto"
4142

4243
perrors "github.com/pkg/errors"
43-
44-
"github.com/dubbogo/grpc-go/credentials"
4544
)
4645

4746
import (

0 commit comments

Comments
 (0)