Move TimeoutConn to seperate pkg dir.
Fixes #10965 Signed-off-by: Rik Nijessen <riknijessen@gmail.com>
This commit is contained in:
parent
309eec2378
commit
690a85797e
3 changed files with 10 additions and 10 deletions
|
@ -1,21 +1,21 @@
|
||||||
package utils
|
package timeout
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewTimeoutConn(conn net.Conn, timeout time.Duration) net.Conn {
|
func New(netConn net.Conn, timeout time.Duration) net.Conn {
|
||||||
return &TimeoutConn{conn, timeout}
|
return &conn{netConn, timeout}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A net.Conn that sets a deadline for every Read or Write operation
|
// A net.Conn that sets a deadline for every Read or Write operation
|
||||||
type TimeoutConn struct {
|
type conn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TimeoutConn) Read(b []byte) (int, error) {
|
func (c *conn) Read(b []byte) (int, error) {
|
||||||
if c.timeout > 0 {
|
if c.timeout > 0 {
|
||||||
err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout))
|
err := c.Conn.SetReadDeadline(time.Now().Add(c.timeout))
|
||||||
if err != nil {
|
if err != nil {
|
|
@ -1,4 +1,4 @@
|
||||||
package utils
|
package timeout
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
@ -10,7 +10,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTimeoutConnRead(t *testing.T) {
|
func TestRead(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, "hello")
|
fmt.Fprintln(w, "hello")
|
||||||
}))
|
}))
|
||||||
|
@ -19,7 +19,7 @@ func TestTimeoutConnRead(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create connection to %q: %v", ts.URL, err)
|
t.Fatalf("failed to create connection to %q: %v", ts.URL, err)
|
||||||
}
|
}
|
||||||
tconn := NewTimeoutConn(conn, 1*time.Second)
|
tconn := New(conn, 1*time.Second)
|
||||||
|
|
||||||
if _, err = bufio.NewReader(tconn).ReadString('\n'); err == nil {
|
if _, err = bufio.NewReader(tconn).ReadString('\n'); err == nil {
|
||||||
t.Fatalf("expected timeout error, got none")
|
t.Fatalf("expected timeout error, got none")
|
|
@ -14,7 +14,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/utils"
|
"github.com/docker/docker/pkg/timeout"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -71,7 +71,7 @@ func newClient(jar http.CookieJar, roots *x509.CertPool, certs []tls.Certificate
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn = utils.NewTimeoutConn(conn, 1*time.Minute)
|
conn = timeout.New(conn, 1*time.Minute)
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue