github.com/slava-ustovytski/docker@v1.8.2-rc1/pkg/timeoutconn/timeoutconn.go (about)

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