02f4ae6c56
Docker-DCO-1.1-Signed-off-by: Derek <crq@kernel.org> (github: crquan)
26 lines
479 B
Go
26 lines
479 B
Go
package utils
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
func NewTimeoutConn(conn net.Conn, timeout time.Duration) net.Conn {
|
|
return &TimeoutConn{conn, timeout}
|
|
}
|
|
|
|
// A net.Conn that sets a deadline for every Read or Write operation
|
|
type TimeoutConn struct {
|
|
net.Conn
|
|
timeout time.Duration
|
|
}
|
|
|
|
func (c *TimeoutConn) 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)
|
|
}
|