-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
|
@@ -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" | ||
|
@@ -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 { | ||
|
@@ -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) { | ||
trans := http.DefaultTransport.(*http.Transport).Clone() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We switched to using the goapis transport since it has the capability of auto discovering the client certificate apart of for mtls handshake. https://docs.google.com/document/d/10tFeBhvTgtKl9XwMXbuKOPzq9mGdV-RSyLuXOQtRZos/edit?tab=t.0 The clientCertSource contains the auto loaded mtls credentials. |
||
// Increase MaxIdleConnsPerHost due to reported performance issues under load in the | ||
// GCS client. | ||
trans.MaxIdleConnsPerHost = 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm understanding correctly, these tweaks are from here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
} | ||
|
@@ -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. | ||
|
@@ -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() | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.