10BC0 c8d/httpfallback: Handle connection errors · moby/moby@ddf384b · GitHub
[go: up one dir, main page]

Skip to content

Commit ddf384b

Browse files
committed
c8d/httpfallback: Handle connection errors
Adjust the httpFallback implementation to also handle non-TLS related errors which can also happen when issuing a HTTPS requested to HTTP-only registries. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 08674fb commit ddf384b

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

daemon/containerd/resolver.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import (
44
"context"
55
"crypto/tls"
66
"errors"
7+
"net"
78
"net/http"
9+
"os"
10+
"strings"
11+
"syscall"
812

913
"github.com/containerd/containerd/remotes"
1014
"github.com/containerd/containerd/remotes/docker"
@@ -118,9 +122,7 @@ type httpFallback struct {
118122

119123
func (f httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
120124
resp, err := f.super.RoundTrip(r)
121-
var tlsErr tls.RecordHeaderError
122-
if errors.As(err, &tlsErr) && string(tlsErr.RecordHeader[:]) == "HTTP/" {
123-
// server gave HTTP response to HTTPS client
125+
if err != nil && (isTLSError(err) || isPortError(err, r.URL.Host)) {
124126
plainHttpUrl := *r.URL
125127
plainHttpUrl.Scheme = "http"
126128

@@ -132,3 +134,27 @@ func (f httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
132134

133135
return resp, err
134136
}
137+
138+
func isTLSError(err error) bool {
139+
var tlsErr tls.RecordHeaderError
140+
if errors.As(err, &tlsErr) && string(tlsErr.RecordHeader[:]) == "HTTP/" {
141+
return true
142+
}
143+
if strings.Contains(err.Error(), "TLS handshake timeout") {
144+
return true
145+
}
146+
147+
return false
148+
}
149+
150+
func isPortError(err error, host string) bool {
151+
if errors.Is(err, syscall.ECONNREFUSED) || os.IsTimeout(err) {
152+
if _, port, _ := net.SplitHostPort(host); port != "" {
153+
// Port is specified, will not retry on different port with scheme change
154+
return false
155+
}
156+
return true
157+
}
158+
159+
return false
160+
}

0 commit comments

Comments
 (0)
0