Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use timeout insteadof read/writetimeout when timeout lower than read/… #1336

Merged
merged 2 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions client.go
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
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