From ddec1114a9e8f6bca5c8258750e537898769165b Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Fri, 26 Apr 2024 13:36:19 -0700 Subject: [PATCH] fix(transport/http): use ipv4 explicitly for dialers if ipv6 is not supported This is a workaround for https://github.com/golang/go/issues/25321, and is related to https://github.com/hashicorp/terraform-provider-google/issues/6782 --- transport/http/dial.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/transport/http/dial.go b/transport/http/dial.go index d1cd83b62d9..2c51fd478ed 100644 --- a/transport/http/dial.go +++ b/transport/http/dial.go @@ -22,6 +22,7 @@ import ( "go.opencensus.io/plugin/ochttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "golang.org/x/net/http2" + "golang.org/x/net/nettest" "golang.org/x/oauth2" "google.golang.org/api/googleapi/transport" "google.golang.org/api/internal" @@ -266,6 +267,14 @@ func defaultBaseTransport(ctx context.Context, clientCertSource cert.Source, dia if trans == nil { trans = fallbackBaseTransport() } + trans.DialContext = func(ctx context.Context, network string, addr string) (net.Conn, error) { + // Don't try IPv6 if it's not supported. + // https://github.com/golang/go/issues/25321 + if !nettest.SupportsIPv6() { + return baseDialContext(ctx, "tcp4", addr) + } + return baseDialContext(ctx, network, addr) + } trans.MaxIdleConnsPerHost = 100 if clientCertSource != nil { @@ -275,7 +284,14 @@ func defaultBaseTransport(ctx context.Context, clientCertSource cert.Source, dia } if dialTLSContext != nil { // If DialTLSContext is set, TLSClientConfig wil be ignored - trans.DialTLSContext = dialTLSContext + trans.DialTLSContext = func(ctx context.Context, network string, addr string) (net.Conn, error) { + // Don't try IPv6 if it's not supported. + // https://github.com/golang/go/issues/25321 + if !nettest.SupportsIPv6() { + return dialTLSContext(ctx, "tcp4", addr) + } + return dialTLSContext(ctx, network, addr) + } } configureHTTP2(trans)