Skip to content

Commit

Permalink
use timeout insteadof read/writetimeout when timeout lower than read/… (
Browse files Browse the repository at this point in the history
valyala#1336)

* use timeout insteadof read/writetimeout when timeout lower than read/writetimeout

* use deadtime; fix test timeout;

Co-authored-by: 徐焱 <[email protected]>
  • Loading branch information
2 people authored and bbenzikry committed Sep 11, 2022
1 parent dbc2733 commit 090b39f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
25 changes: 21 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,11 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
return err == nil, err
}

var deadline time.Time
if req.timeout > 0 {
deadline = time.Now().Add(req.timeout)
}

cc, err := c.acquireConn(req.timeout, req.ConnectionClose())
if err != nil {
return false, err
Expand All @@ -1426,11 +1431,17 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)

resp.parseNetConn(conn)

writeDeadline := deadline
if c.WriteTimeout > 0 {
tmpWriteDeadline := time.Now().Add(c.WriteTimeout)
if writeDeadline.IsZero() || tmpWriteDeadline.Before(writeDeadline) {
writeDeadline = tmpWriteDeadline
}
}
if !writeDeadline.IsZero() {
// Set Deadline every time, since golang has fixed the performance issue
// See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details
currentTime := time.Now()
if err = conn.SetWriteDeadline(currentTime.Add(c.WriteTimeout)); err != nil {
if err = conn.SetWriteDeadline(writeDeadline); err != nil {
c.closeConn(cc)
return true, err
}
Expand Down Expand Up @@ -1459,11 +1470,17 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
return true, err
}

readDeadline := deadline
if c.ReadTimeout > 0 {
tmpReadDeadline := time.Now().Add(c.ReadTimeout)
if readDeadline.IsZero() || tmpReadDeadline.Before(readDeadline) {
readDeadline = tmpReadDeadline
}
}
if !readDeadline.IsZero() {
// Set Deadline every time, since golang has fixed the performance issue
// See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details
currentTime := time.Now()
if err = conn.SetReadDeadline(currentTime.Add(c.ReadTimeout)); err != nil {
if err = conn.SetReadDeadline(readDeadline); err != nil {
c.closeConn(cc)
return true, err
}
Expand Down
24 changes: 20 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1635,9 +1635,13 @@ func TestClientDoTimeoutSuccessConcurrent(t *testing.T) {
func TestClientGetTimeoutError(t *testing.T) {
t.Parallel()

s := startEchoServer(t, "tcp", "127.0.0.1:")
defer s.Stop()

testConn, _ := net.Dial("tcp", s.ln.Addr().String())
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return &readTimeoutConn{t: time.Second}, nil
return &readTimeoutConn{Conn: testConn, t: time.Second}, nil
},
}

Expand All @@ -1647,9 +1651,13 @@ func TestClientGetTimeoutError(t *testing.T) {
func TestClientGetTimeoutErrorConcurrent(t *testing.T) {
t.Parallel()

s := startEchoServer(t, "tcp", "127.0.0.1:")
defer s.Stop()

testConn, _ := net.Dial("tcp", s.ln.Addr().String())
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return &readTimeoutConn{t: time.Second}, nil
return &readTimeoutConn{Conn: testConn, t: time.Second}, nil
},
MaxConnsPerHost: 1000,
}
Expand All @@ -1668,9 +1676,13 @@ func TestClientGetTimeoutErrorConcurrent(t *testing.T) {
func TestClientDoTimeoutError(t *testing.T) {
t.Parallel()

s := startEchoServer(t, "tcp", "127.0.0.1:")
defer s.Stop()

testConn, _ := net.Dial("tcp", s.ln.Addr().String())
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return &readTimeoutConn{t: time.Second}, nil
return &readTimeoutConn{Conn: testConn, t: time.Second}, nil
},
}

Expand All @@ -1680,9 +1692,13 @@ func TestClientDoTimeoutError(t *testing.T) {
func TestClientDoTimeoutErrorConcurrent(t *testing.T) {
t.Parallel()

s := startEchoServer(t, "tcp", "127.0.0.1:")
defer s.Stop()

testConn, _ := net.Dial("tcp", s.ln.Addr().String())
c := &Client{
Dial: func(addr string) (net.Conn, error) {
return &readTimeoutConn{t: time.Second}, nil
return &readTimeoutConn{Conn: testConn, t: time.Second}, nil
},
MaxConnsPerHost: 1000,
}
Expand Down

0 comments on commit 090b39f

Please sign in to comment.