github.com/nguyentm83/docker@v1.5.0/utils/timeoutconn.go (about)

     1  package utils
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  )
     7  
     8  func NewTimeoutConn(conn net.Conn, timeout time.Duration) net.Conn {
     9  	return &TimeoutConn{conn, timeout}
    10  }
    11  
    12  // A net.Conn that sets a deadline for every Read or Write operation
    13  type TimeoutConn struct {
    14  	net.Conn
    15  	timeout time.Duration
    16  }
    17  
    18  func (c *TimeoutConn) 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  }