Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made base transport layer force ipv4 usage if ipv6 is not supported #10485

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions mmv1/third_party/terraform/transport/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"regexp"
"strconv"
Expand All @@ -28,6 +29,8 @@ import (

"github.com/hashicorp/terraform-provider-google/google/verify"

"golang.org/x/net/http2"
"golang.org/x/net/nettest"
"golang.org/x/oauth2"
"google.golang.org/grpc"
googleoauth "golang.org/x/oauth2/google"
Expand Down Expand Up @@ -82,6 +85,7 @@ import (
"google.golang.org/api/storage/v1"
"google.golang.org/api/storagetransfer/v1"
"google.golang.org/api/transport"
apihttp "google.golang.org/api/transport/http"
)

type ProviderMeta struct {
Expand Down Expand Up @@ -429,6 +433,39 @@ func SetEndpointDefaults(d *schema.ResourceData) error {
return nil
}

// baseTransport returns the base HTTP transport. It starts with the
// default transport and makes some tweaks to match best practices
// from google-api-go-client, as well as ensuring that IPv6 does
// not get used in environments that don't support it.
func baseTransport() (http.RoundTripper, error) {
Copy link
Member

@c2thorn c2thorn Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing something here, but why can't we reuse the google-api-go-client's NewClient method and override the given transport's dial context after?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.Client's Transport is a RoundTripper rather than a Transport, so we would need to cast it back to a Transport in order to do that. I don't see a reason that wouldn't work, and that would've been a lot easier to implement.

I think, having dug through the code, I would slightly prefer this way because we know exactly what we're getting and don't have to worry about the intricacies of NewClient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's reasonable now that we've gone through the code. My only concerns are about missing something, but I've gone through it myself now and everything seems in order.

trans := http.DefaultTransport.(*http.Transport).Clone()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Increase MaxIdleConnsPerHost due to reported performance issues under load in the
// GCS client.
trans.MaxIdleConnsPerHost = 100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding correctly, these tweaks are from here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, you did mention it in the comment, but I'll leave the link here just for posterity


// Configure the ReadIdleTimeout HTTP/2 option for the
// transport. This allows broken idle connections to be pruned more quickly,
// preventing the client from attempting to re-use connections that will no
// longer work. http2Trans is discarded after configuration.
http2Trans, err := http2.ConfigureTransports(trans)
if err != nil {
return trans, err
}
http2Trans.ReadIdleTimeout = time.Second * 31

// Override dialer so that we don't try IPv6 if it's not supported.
// https://github.com/golang/go/issues/25321
trans.DialContext = func(ctx context.Context, network string, addr string) (net.Conn, error) {
d := &net.Dialer{}
if !nettest.SupportsIPv6() {
return d.DialContext(ctx, "tcp4", addr)
}
return d.DialContext(ctx, network, addr)
}

return trans, nil
}

func (c *Config) LoadAndValidate(ctx context.Context) error {
if len(c.Scopes) == 0 {
c.Scopes = DefaultClientScopes
Expand All @@ -445,8 +482,13 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {

cleanCtx := context.WithValue(ctx, oauth2.HTTPClient, cleanhttp.DefaultClient())

// 1. MTLS TRANSPORT/CLIENT - sets up proper auth headers
client, _, err := transport.NewHTTPClient(cleanCtx, option.WithTokenSource(tokenSource))
// 1. Set up base HTTP transport and configure token auth / API communication
base, err := baseTransport()
if err != nil {
return err
}

transport, err := apihttp.NewTransport(cleanCtx, base, option.WithTokenSource(tokenSource))
if err != nil {
return err
}
Expand All @@ -458,7 +500,7 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
}

// 2. Logging Transport - ensure we log HTTP requests to GCP APIs.
loggingTransport := logging.NewTransport("Google", client.Transport)
loggingTransport := logging.NewTransport("Google", transport)

// 3. Retry Transport - retries common temporary errors
// Keep order for wrapping logging so we log each retried request as well.
Expand All @@ -479,8 +521,8 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
headerTransport.Set("X-Goog-User-Project", c.BillingProject)
}

// Set final transport value.
client.Transport = headerTransport
// Create http client
client := &http.Client{Transport: headerTransport}

// This timeout is a timeout per HTTP request, not per logical operation.
client.Timeout = c.synchronousTimeout()
Expand Down
Loading