Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Optimized connection SetDeadline.
Browse files Browse the repository at this point in the history
Update deadline only if more than 25% of the last deadline pasted.
See golang/go#15133 for details.
  • Loading branch information
ivankorobkov committed Jun 27, 2018
1 parent 348ca7d commit f27febe
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type Connection struct {
node *Node

// timeout
timeout time.Duration
timeout time.Duration
deadline time.Time

// duration after which connection is considered idle
idleTimeout time.Duration
Expand Down Expand Up @@ -245,10 +246,22 @@ func (ctn *Connection) SetTimeout(timeout time.Duration) error {

// important: remove deadline when not needed; connections are pooled
if ctn.conn != nil {
now := time.Now()

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

// Optimization: update deadline only if more than 25% of the last deadline pasted.
// See https://github.com/golang/go/issues/15133 for details.
if !deadline.IsZero() {
if now.Sub(ctn.deadline) < (timeout >> 2) {
return nil
}
}

ctn.deadline = deadline
if err := ctn.conn.SetDeadline(deadline); err != nil {
if ctn.node != nil {
atomic.AddInt64(&ctn.node.stats.ConnectionsFailed, 1)
Expand Down

0 comments on commit f27febe

Please sign in to comment.