Skip to content

Commit

Permalink
Remove the set read and write deadline optimization since golang has …
Browse files Browse the repository at this point in the history
  • Loading branch information
YanKawaYu authored and erikdubbelboer committed Nov 23, 2018
1 parent 8a7123e commit a26bbd1
Showing 1 changed file with 22 additions and 52 deletions.
74 changes: 22 additions & 52 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,6 @@ type clientConn struct {

createdTime time.Time
lastUseTime time.Time

lastReadDeadlineTime time.Time
lastWriteDeadlineTime time.Time
}

var startTimeUnix = time.Now().Unix()
Expand Down Expand Up @@ -1113,16 +1110,12 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
conn := cc.c

if c.WriteTimeout > 0 {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
// 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 currentTime.Sub(cc.lastWriteDeadlineTime) > (c.WriteTimeout >> 2) {
if err = conn.SetWriteDeadline(currentTime.Add(c.WriteTimeout)); err != nil {
c.closeConn(cc)
return true, err
}
cc.lastWriteDeadlineTime = currentTime
if err = conn.SetWriteDeadline(currentTime.Add(c.WriteTimeout)); err != nil {
c.closeConn(cc)
return true, err
}
}

Expand Down Expand Up @@ -1154,16 +1147,12 @@ func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error)
c.releaseWriter(bw)

if c.ReadTimeout > 0 {
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
// 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 currentTime.Sub(cc.lastReadDeadlineTime) > (c.ReadTimeout >> 2) {
if err = conn.SetReadDeadline(currentTime.Add(c.ReadTimeout)); err != nil {
c.closeConn(cc)
return true, err
}
cc.lastReadDeadlineTime = currentTime
if err = conn.SetReadDeadline(currentTime.Add(c.ReadTimeout)); err != nil {
c.closeConn(cc)
return true, err
}
}

Expand Down Expand Up @@ -1354,13 +1343,6 @@ var clientConnPool sync.Pool

func (c *HostClient) releaseConn(cc *clientConn) {
cc.lastUseTime = time.Now()
//reset read and write deadline for reuse
if c.ReadTimeout > 0 {
cc.c.SetReadDeadline(time.Time{})
}
if c.WriteTimeout > 0 {
cc.c.SetWriteDeadline(time.Time{})
}

c.connsLock.Lock()
c.conns = append(c.conns, cc)
Expand Down Expand Up @@ -2027,8 +2009,6 @@ func (c *pipelineConnClient) writer(conn net.Conn, stopCh <-chan struct{}) error

w *pipelineWork
err error

lastWriteDeadlineTime time.Time
)
close(instantTimerCh)
for {
Expand Down Expand Up @@ -2061,17 +2041,13 @@ func (c *pipelineConnClient) writer(conn net.Conn, stopCh <-chan struct{}) error
}

if writeTimeout > 0 {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
// 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 currentTime.Sub(lastWriteDeadlineTime) > (writeTimeout >> 2) {
if err = conn.SetWriteDeadline(currentTime.Add(writeTimeout)); err != nil {
w.err = err
w.done <- struct{}{}
return err
}
lastWriteDeadlineTime = currentTime
if err = conn.SetWriteDeadline(currentTime.Add(writeTimeout)); err != nil {
w.err = err
w.done <- struct{}{}
return err
}
}
if err = w.req.Write(bw); err != nil {
Expand Down Expand Up @@ -2125,8 +2101,6 @@ func (c *pipelineConnClient) reader(conn net.Conn, stopCh <-chan struct{}) error
var (
w *pipelineWork
err error

lastReadDeadlineTime time.Time
)
for {
select {
Expand All @@ -2142,17 +2116,13 @@ func (c *pipelineConnClient) reader(conn net.Conn, stopCh <-chan struct{}) error
}

if readTimeout > 0 {
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
// 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 currentTime.Sub(lastReadDeadlineTime) > (readTimeout >> 2) {
if err = conn.SetReadDeadline(currentTime.Add(readTimeout)); err != nil {
w.err = err
w.done <- struct{}{}
return err
}
lastReadDeadlineTime = currentTime
if err = conn.SetReadDeadline(currentTime.Add(readTimeout)); err != nil {
w.err = err
w.done <- struct{}{}
return err
}
}
if err = w.resp.Read(br); err != nil {
Expand Down

0 comments on commit a26bbd1

Please sign in to comment.