Merge pull request #10342 from dmcgowan/add-mutex-fallback-host

Adds a mutex to protect fallback host
This commit is contained in:
Fu Wei 2024-06-17 13:13:57 +00:00 committed by GitHub
commit 0975ec0908
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,6 +28,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"sync"
"github.com/containerd/errdefs" "github.com/containerd/errdefs"
"github.com/containerd/log" "github.com/containerd/log"
@ -728,11 +729,16 @@ func NewHTTPFallback(transport http.RoundTripper) http.RoundTripper {
type httpFallback struct { type httpFallback struct {
super http.RoundTripper super http.RoundTripper
host string host string
mu sync.Mutex
} }
func (f *httpFallback) RoundTrip(r *http.Request) (*http.Response, error) { func (f *httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
f.mu.Lock()
fallback := f.host == r.URL.Host
f.mu.Unlock()
// only fall back if the same host had previously fell back // only fall back if the same host had previously fell back
if f.host != r.URL.Host { if !fallback {
resp, err := f.super.RoundTrip(r) resp, err := f.super.RoundTrip(r)
if !isTLSError(err) && !isPortError(err, r.URL.Host) { if !isTLSError(err) && !isPortError(err, r.URL.Host) {
return resp, err return resp, err
@ -745,8 +751,12 @@ func (f *httpFallback) RoundTrip(r *http.Request) (*http.Response, error) {
plainHTTPRequest := *r plainHTTPRequest := *r
plainHTTPRequest.URL = &plainHTTPUrl plainHTTPRequest.URL = &plainHTTPUrl
if f.host != r.URL.Host { if !fallback {
f.host = r.URL.Host f.mu.Lock()
if f.host != r.URL.Host {
f.host = r.URL.Host
}
f.mu.Unlock()
// update body on the second attempt // update body on the second attempt
if r.Body != nil && r.GetBody != nil { if r.Body != nil && r.GetBody != nil {