Rename package timeout to timeoutconn.
Signed-off-by: Rik Nijessen <riknijessen@gmail.com>
This commit is contained in:
parent
140d563d98
commit
ee25c29598
2 changed files with 2 additions and 2 deletions
26
timeoutconn/timeoutconn.go
Normal file
26
timeoutconn/timeoutconn.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue