1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/timeoutconn/timeoutconn.go
Rik Nijessen 7e7646c28a Rename package timeout to timeoutconn.
Signed-off-by: Rik Nijessen <riknijessen@gmail.com>
2015-02-25 20:52:37 +01:00

26 lines
459 B
Go

package timeoutconn
import (
"net"
"time"
)
func New(netConn net.Conn, timeout time.Duration) net.Conn {
return &conn{netConn, timeout}
}
// A net.Conn that sets a deadline for every Read or Write operation
type conn struct {
net.Conn
timeout time.Duration
}
func (c *conn) Read(b []byte) (int, error) {
if c.timeout > 0 {
err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout))
if err != nil {
return 0, err
}
}
return c.Conn.Read(b)
}