Skip to content

Commit

Permalink
Merge pull request #40 from Code-Fight/ftr/timeout_to_time_duration
Browse files Browse the repository at this point in the history
Ftr:timeout config optimize
  • Loading branch information
zhaoyunxing92 authored Nov 2, 2022
2 parents 6fca87c + 433a65b commit dde4119
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pkg/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package constant

import "time"

// transfer
const (
// TRIPLE is triple protocol name
Expand All @@ -26,7 +28,7 @@ const (
DefaultHttp2ControllerReadBufferSize = 4096

// DefaultTimeout is default timeout seconds of triple client
DefaultTimeout = 15
DefaultTimeout = time.Second * 3

// DefaultListeningAddress is default listening address
DefaultListeningAddress = "127.0.0.1:20001"
Expand Down
10 changes: 6 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,7 +28,7 @@ import (
// triple option
type Option struct {
// network opts
Timeout uint32
Timeout time.Duration
BufferSize uint32

// service opts
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package config

import (
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
)
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/triple/dubbo3_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/triple/dubbo3_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ 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
}

// Invoke called by unary rpc 's pb.go file in dubbo-go 3.0 design
// @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)
Expand All @@ -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...)
Expand Down

0 comments on commit dde4119

Please sign in to comment.