From cdf9a2b1a434d66ab37db6baaec61621ada3f262 Mon Sep 17 00:00:00 2001 From: FengZhang Date: Wed, 2 Nov 2022 12:47:01 +0000 Subject: [PATCH] timeout config optimize --- pkg/common/constant/constant.go | 4 +++- pkg/config/config.go | 10 ++++++---- pkg/config/config_test.go | 10 +++++----- pkg/triple/dubbo3_client.go | 4 ++-- pkg/triple/dubbo3_conn.go | 8 ++++---- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 37ef770..1c1f1c6 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -17,6 +17,8 @@ package constant +import "time" + // transfer const ( // TRIPLE is triple protocol name @@ -26,7 +28,7 @@ const ( DefaultHttp2ControllerReadBufferSize = 4096 // DefaultTimeout is default timeout seconds of triple client - DefaultTimeout = 15 + DefaultTimeout = time.Second * 15 // DefaultListeningAddress is default listening address DefaultListeningAddress = "127.0.0.1:20001" diff --git a/pkg/config/config.go b/pkg/config/config.go index fab4bd1..2a41a0c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,8 @@ package config import ( + "time" + "github.com/dubbogo/triple/pkg/common/constant" loggerInterface "github.com/dubbogo/triple/pkg/common/logger" "github.com/dubbogo/triple/pkg/common/logger/default_logger" @@ -26,7 +28,7 @@ import ( // triple option type Option struct { // network opts - Timeout uint32 + Timeout time.Duration BufferSize uint32 // service opts @@ -63,8 +65,8 @@ type Option struct { // Validate sets empty field to default config func (o *Option) Validate() { - if o.Timeout == uint32(0) { - o.Timeout = uint32(constant.DefaultTimeout) + if o.Timeout == time.Duration(0) { + o.Timeout = constant.DefaultTimeout } if o.BufferSize == uint32(0) { @@ -106,7 +108,7 @@ func NewTripleOption(fs ...OptionFunction) *Option { } // WithClientTimeout return OptionFunction with timeout of @timeout -func WithClientTimeout(timeout uint32) OptionFunction { +func WithClientTimeout(timeout time.Duration) OptionFunction { return func(o *Option) { o.Timeout = timeout } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 435fc18..3334ab6 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -19,8 +19,8 @@ package config import ( "testing" + "time" ) - import ( "github.com/stretchr/testify/assert" ) @@ -40,7 +40,7 @@ func TestWithClientTimeout(t *testing.T) { WithClientTimeout(120), ) assert.NotNil(t, opt) - assert.Equal(t, opt.Timeout, uint32(120)) + assert.Equal(t, opt.Timeout, time.Duration(120)) } func TestWithSerializerType(t *testing.T) { @@ -71,11 +71,11 @@ func TestOption_SetEmptyFieldDefaultConfig(t *testing.T) { ) assert.NotNil(t, opt) assert.Equal(t, uint32(100000), opt.BufferSize) - assert.Equal(t, uint32(constant.DefaultTimeout), opt.Timeout) + assert.Equal(t, constant.DefaultTimeout, opt.Timeout) opt = NewTripleOption() assert.Equal(t, uint32(constant.DefaultHttp2ControllerReadBufferSize), opt.BufferSize) - assert.Equal(t, uint32(constant.DefaultTimeout), opt.Timeout) + assert.Equal(t, constant.DefaultTimeout, opt.Timeout) } func TestWithHeaderAppVersion(t *testing.T) { @@ -105,7 +105,7 @@ func TestWithLogger(t *testing.T) { func TestValidate(t *testing.T) { opt := NewTripleOption() opt.Validate() - assert.Equal(t, uint32(constant.DefaultTimeout), opt.Timeout) + assert.Equal(t, constant.DefaultTimeout, opt.Timeout) assert.Equal(t, uint32(constant.DefaultHttp2ControllerReadBufferSize), opt.BufferSize) assert.Equal(t, constant.DefaultListeningAddress, opt.Location) assert.Equal(t, constant.TRIPLE, opt.Protocol) diff --git a/pkg/triple/dubbo3_client.go b/pkg/triple/dubbo3_client.go index be42756..46cf53b 100644 --- a/pkg/triple/dubbo3_client.go +++ b/pkg/triple/dubbo3_client.go @@ -100,9 +100,9 @@ func NewTripleClient(impl interface{}, opt *config.Option) (*TripleClient, error // codec if opt.CodecType == constant.PBCodecName { // put dubbo3 network logic to tripleConn, creat pb stub invoker - tripleClient.stubInvoker = reflect.ValueOf(getInvoker(impl, newTripleConn(int(opt.Timeout), opt.Location, dialOpts...))) + tripleClient.stubInvoker = reflect.ValueOf(getInvoker(impl, newTripleConn(opt.Timeout, opt.Location, dialOpts...))) } else { - tripleClient.triplConn = newTripleConn(int(opt.Timeout), opt.Location, dialOpts...) + tripleClient.triplConn = newTripleConn(opt.Timeout, opt.Location, dialOpts...) } return tripleClient, nil } diff --git a/pkg/triple/dubbo3_conn.go b/pkg/triple/dubbo3_conn.go index a4e324b..1c821df 100644 --- a/pkg/triple/dubbo3_conn.go +++ b/pkg/triple/dubbo3_conn.go @@ -34,7 +34,7 @@ import ( // TripleConn is the struct that called in pb.go file // Its client field contains all net logic of dubbo3 type TripleConn struct { - timeout int + timeout time.Duration grpcConn *grpc.ClientConn } @@ -42,7 +42,7 @@ type TripleConn struct { // @method is /interfaceKey/functionName e.g. /com.apache.dubbo.sample.basic.IGreeter/BigUnaryTest // @arg is request body, must be proto.Message type func (t *TripleConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...grpc.CallOption) common.ErrorWithAttachment { - ctx, cancel := context.WithTimeout(ctx, time.Duration(t.timeout)*time.Second) + ctx, cancel := context.WithTimeout(ctx, t.timeout) defer cancel() trailer, err := t.grpcConn.Invoke(ctx, method, args, reply, opts...) //return t.client.Request(ctx, method, args, reply) @@ -60,9 +60,9 @@ func (t *TripleConn) NewStream(ctx context.Context, method string, opts ...grpc. } // newTripleConn new a triple conn with given @tripleclient, which contains all net logic -func newTripleConn(timeout int, address string, opts ...grpc.DialOption) *TripleConn { +func newTripleConn(timeout time.Duration, address string, opts ...grpc.DialOption) *TripleConn { //grpcConn, _ := grpc.Dial(address,grpc.WithInsecure()) - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() opts = append(opts, grpc.WithInsecure()) grpcConn, _ := grpc.DialContext(ctx, address, opts...)