-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
prevent vtctld from creating tons of S3 connections #15296
Changes from 3 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 |
---|---|---|
|
@@ -83,6 +83,9 @@ var ( | |
|
||
// path component delimiter | ||
delimiter = "/" | ||
|
||
// use a shared transport for all connections | ||
defaultS3Transport *http.Transport | ||
) | ||
|
||
func registerFlags(fs *pflag.FlagSet) { | ||
|
@@ -445,9 +448,7 @@ func (bs *S3BackupStorage) client() (*s3.S3, error) { | |
if bs._client == nil { | ||
logLevel := getLogLevel() | ||
|
||
tlsClientConf := &tls.Config{InsecureSkipVerify: tlsSkipVerifyCert} | ||
httpTransport := &http.Transport{TLSClientConfig: tlsClientConf} | ||
httpClient := &http.Client{Transport: httpTransport} | ||
httpClient := &http.Client{Transport: getS3Transport()} | ||
|
||
session, err := session.NewSession() | ||
if err != nil { | ||
|
@@ -496,6 +497,19 @@ func objName(parts ...string) *string { | |
return &res | ||
} | ||
|
||
// This creates a new transport based off http.DefaultTransport the first time and returns the same | ||
// transport on subsequent calls so connections can be reused as part of the same transport. | ||
func getS3Transport() *http.Transport { | ||
if defaultS3Transport == nil { | ||
tlsClientConf := &tls.Config{InsecureSkipVerify: tlsSkipVerifyCert} | ||
|
||
defaultS3Transport = 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. This seems correct after reading through: golang/go#26013 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. I think that we should get rid of this getter and instead initialize the variable in 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. yes, that makes sense. I will move it to the 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. as mentioned in the other comment, I have moved this to the |
||
defaultS3Transport.TLSClientConfig = tlsClientConf | ||
} | ||
|
||
return defaultS3Transport | ||
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. Is this concurrency safe / does it need to be? Since it doesn't seem like it is. I'm also not a big fan of having a global for this, those tend to bite us later on. Can we move it at least into 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 can then also initialize it already in the 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. yes, it should be, specially considering the default on the SDK already uses I thought the if the above is not possible and setting it up on 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. I see that you added a unit test for this. What's the conclusion? 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. @deepthi I’m back next week but I don’t see a refactor of this or a reason why that couldn’t be done. I strongly prefer not having a global like this if that can reasonably be avoided. Also happy to push up that refactor once I’m back. 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. I have moved this to inside the |
||
} | ||
|
||
func init() { | ||
backupstorage.BackupStorageMap["s3"] = &S3BackupStorage{params: backupstorage.NoParams()} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,3 +276,16 @@ func TestSSECustomerFileBase64Key(t *testing.T) { | |
assert.Nil(t, sseData.customerKey, "customerKey expected to be nil") | ||
assert.Nil(t, sseData.customerMd5, "customerMd5 expected to be nil") | ||
} | ||
|
||
func Test_getS3Transport(t *testing.T) { | ||
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. Nit, but 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. I updated it to |
||
transport := getS3Transport() | ||
|
||
// checking some of the values are present in the returned transport and match the http.DefaultTransport. | ||
assert.Equal(t, http.DefaultTransport.(*http.Transport).IdleConnTimeout, transport.IdleConnTimeout) | ||
assert.Equal(t, http.DefaultTransport.(*http.Transport).MaxIdleConns, transport.MaxIdleConns) | ||
assert.NotNil(t, transport.DialContext) | ||
assert.NotNil(t, transport.Proxy) | ||
|
||
newTransport := getS3Transport() | ||
assert.Same(t, transport, newTransport) // new call should return the same transport | ||
} |
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.
Nit, but s3 is a bit redundant here. We can just call it
defaultTransport
.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.
got rid of the global variable 😌