github.com/lmars/docker@v1.6.0-rc2/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 err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout)) 21 if err != nil { 22 return 0, err 23 } 24 } 25 return c.Conn.Read(b) 26 }