140d563d98
Fixes #10965 Signed-off-by: Rik Nijessen <riknijessen@gmail.com>
26 lines
455 B
Go
26 lines
455 B
Go
package timeout
|
|
|
|
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)
|
|
}
|