Skip to content

Commit

Permalink
add time out support
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjwxc committed Jul 16, 2021
1 parent 15091f6 commit a33099b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 5 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var (
// DefaultPoolTTL sets the connection pool ttl
DefaultPoolTTL = time.Minute

// DefaultPoolTimeout sets the connection pool ttl
DefaultPoolTimeout = 5 * time.Second

// NewClient returns a new client
NewClient func(...Option) Client = newNamingClient
// NewIPAddrClient returns a new client
Expand All @@ -67,7 +70,7 @@ func newNamingClient(opts ...Option) Client {
// wg: wait(options.Context),
}
// rc.once.Store(false)
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams(), false)
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams(), false, options.TimeOut)

return rc
}
Expand All @@ -84,7 +87,7 @@ func newIPAddrClient(opts ...Option) Client {
// wg: wait(options.Context),
}
// rc.once.Store(false)
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams(), true)
rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams(), true, options.TimeOut)

return rc
}
17 changes: 12 additions & 5 deletions client/grpc_pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"context"
"fmt"
"sync"
"time"

Expand All @@ -9,8 +11,9 @@ import (
)

type pool struct {
size int
ttl int64
timeOut time.Duration
size int
ttl int64

// max streams on a *poolConn
maxStreams int
Expand Down Expand Up @@ -50,7 +53,7 @@ type poolConn struct {
in bool
}

func newPool(size int, ttl time.Duration, idle int, ms int, isIPAddr bool) *pool {
func newPool(size int, ttl time.Duration, idle int, ms int, isIPAddr bool, timeOut time.Duration) *pool {
if ms <= 0 {
ms = 1
}
Expand All @@ -60,6 +63,7 @@ func newPool(size int, ttl time.Duration, idle int, ms int, isIPAddr bool) *pool
return &pool{
size: size,
ttl: int64(ttl.Seconds()),
timeOut: timeOut,
maxStreams: ms,
maxIdle: idle,
conns: make(map[string]*streamsPool),
Expand Down Expand Up @@ -135,9 +139,12 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
p.Unlock()

// create new conn
cc, err := grpc.Dial(addr, opts...)
ctx, cel := context.WithTimeout(context.Background(), p.timeOut)
defer cel()
cc, err := grpc.DialContext(ctx, addr, opts...)
if err != nil {
return nil, err
//if err == context.DeadlineExceeded { // 超时
return nil, fmt.Errorf("[%v]:%v", addr, err.Error())
}
conn = &poolConn{cc, nil, addr, p, sp, 1, time.Now().Unix(), nil, nil, false}

Expand Down
2 changes: 1 addition & 1 deletion client/grpc_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func testPool(t *testing.T, size int, ttl time.Duration, idle int, ms int) {
defer s.Stop()

// zero pool
p := newPool(size, ttl, idle, ms, false)
p := newPool(size, ttl, idle, ms, false, 3*time.Second)

for i := 0; i < 10; i++ {
// get a conn
Expand Down
2 changes: 2 additions & 0 deletions client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options struct {
// Connection Pool
PoolSize int
PoolTTL time.Duration
TimeOut time.Duration

Registry *registry.Registry
// registry
Expand All @@ -37,6 +38,7 @@ func newOptions(options ...Option) Options {
PoolSize: DefaultPoolSize,
PoolTTL: DefaultPoolTTL,
RegisterTTL: time.Millisecond * 100,
TimeOut: DefaultPoolTimeout,
}

for _, o := range options {
Expand Down

0 comments on commit a33099b

Please sign in to comment.