github.com/akashshinde/docker@v1.9.1/pkg/timeoutconn/timeoutconn.go (about)

     1  // Package timeoutconn provides overridden net.Conn that supports deadline (timeout).
     2  package timeoutconn
     3  
     4  import (
     5  	"net"
     6  	"time"
     7  )
     8  
     9  // New creates a net.Conn with a timeout for every Read operation.
    10  func New(netConn net.Conn, timeout time.Duration) net.Conn {
    11  	return &conn{netConn, timeout}
    12  }
    13  
    14  // A net.Conn that sets a deadline for every Read operation.
    15  // FIXME was documented the deadline was on Write operation too but not implement
    16  type conn struct {
    17  	net.Conn
    18  	timeout time.Duration
    19  }
    20  
    21  func (c *conn) Read(b []byte) (int, error) {
    22  	if c.timeout > 0 {
    23  		if err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout)); err != nil {
    24  			return 0, err
    25  		}
    26  	}
    27  	return c.Conn.Read(b)
    28  }