Browse code

Remove timeout shared function.

Handle timeouts when it's necessary based on a Timeout interface.

Signed-off-by: David Calavera <david.calavera@gmail.com>

David Calavera authored on 2015/12/15 04:23:21
Showing 3 changed files
... ...
@@ -9,8 +9,6 @@ import (
9 9
 	"net/http"
10 10
 	"net/url"
11 11
 	"strings"
12
-
13
-	"github.com/docker/docker/utils"
14 12
 )
15 13
 
16 14
 // serverResponse is a wrapper for http API responses.
... ...
@@ -96,7 +94,7 @@ func (cli *Client) sendClientRequest(method, path string, query url.Values, body
96 96
 	}
97 97
 
98 98
 	if err != nil {
99
-		if utils.IsTimeout(err) || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
99
+		if isTimeout(err) || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
100 100
 			return serverResp, ErrConnectionFailed
101 101
 		}
102 102
 
... ...
@@ -163,3 +161,16 @@ func ensureReaderClosed(response *serverResponse) {
163 163
 		response.body.Close()
164 164
 	}
165 165
 }
166
+
167
+func isTimeout(err error) bool {
168
+	type timeout interface {
169
+		Timeout() bool
170
+	}
171
+	e := err
172
+	switch urlErr := err.(type) {
173
+	case *url.Error:
174
+		e = urlErr.Err
175
+	}
176
+	t, ok := e.(timeout)
177
+	return ok && t.Timeout()
178
+}
... ...
@@ -25,7 +25,6 @@ import (
25 25
 	"github.com/docker/docker/pkg/ioutils"
26 26
 	"github.com/docker/docker/pkg/stringid"
27 27
 	"github.com/docker/docker/pkg/tarsum"
28
-	"github.com/docker/docker/utils"
29 28
 )
30 29
 
31 30
 var (
... ...
@@ -420,7 +419,7 @@ func (r *Session) GetRepositoryData(remote reference.Named) (*RepositoryData, er
420 420
 		// and return a non-obtuse error message for users
421 421
 		// "Get https://index.docker.io/v1/repositories/library/busybox/images: i/o timeout"
422 422
 		// was a top search on the docker user forum
423
-		if utils.IsTimeout(err) {
423
+		if isTimeout(err) {
424 424
 			return nil, fmt.Errorf("Network timed out while trying to connect to %s. You may want to check your internet connection or if you are behind a proxy.", repositoryTarget)
425 425
 		}
426 426
 		return nil, fmt.Errorf("Error while pulling image: %v", err)
... ...
@@ -754,3 +753,16 @@ func (r *Session) GetAuthConfig(withPasswd bool) *cliconfig.AuthConfig {
754 754
 		Email:    r.authConfig.Email,
755 755
 	}
756 756
 }
757
+
758
+func isTimeout(err error) bool {
759
+	type timeout interface {
760
+		Timeout() bool
761
+	}
762
+	e := err
763
+	switch urlErr := err.(type) {
764
+	case *url.Error:
765
+		e = urlErr.Err
766
+	}
767
+	t, ok := e.(timeout)
768
+	return ok && t.Timeout()
769
+}
757 770
deleted file mode 100644
... ...
@@ -1,21 +0,0 @@
1
-package utils
2
-
3
-import (
4
-	"net"
5
-	"net/url"
6
-)
7
-
8
-// IsTimeout takes an error returned from (generally) the http package and determines if it is a timeout error.
9
-func IsTimeout(err error) bool {
10
-	switch e := err.(type) {
11
-	case net.Error:
12
-		return e.Timeout()
13
-	case *url.Error:
14
-		if t, ok := e.Err.(net.Error); ok {
15
-			return t.Timeout()
16
-		}
17
-		return false
18
-	default:
19
-		return false
20
-	}
21
-}