Skip to content

Commit

Permalink
transport/http: use ipv4 explicitly for dialers if ipv6 is not supported
Browse files Browse the repository at this point in the history
This is a workaround for golang/go#25321, and is related to hashicorp/terraform-provider-google#6782
  • Loading branch information
melinath committed Apr 26, 2024
1 parent 32a5d10 commit 4b8b91e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion transport/http/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 4b8b91e

Please sign in to comment.