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

Do not override the provided HTTP client in the fetcher #302

Merged
merged 4 commits into from
Mar 3, 2021

Conversation

septerr
Copy link
Contributor

@septerr septerr commented Mar 3, 2021

Motivation

When creating a fetcher instance, we always override the Transport of the http.Client present inside the APIClient object. This leads to unexpected behavior when callers provide their own http.Client via the WithClient option.

Solution

This change makes it so that we do not override the Transport when it is already provided . Note we still update the Transport's TLSClientConfig field when insecureTLS is set to true.

Open questions

@septerr septerr requested a review from alex-stone March 3, 2021 01:18
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
defaultTransport.IdleConnTimeout = DefaultIdleConnTimeout
defaultTransport.MaxIdleConns = DefaultMaxConnections
defaultTransport.MaxIdleConnsPerHost = DefaultMaxConnections
Copy link

@alex-stone alex-stone Mar 3, 2021

Choose a reason for hiding this comment

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

The one behavioral change here for clients who are not passing their own rosetta API client is that we're not leveraging the specified max connections

We could potentially change this to occur after applying the options and check if a client is initialized, e.g.

for _, opt := range options {
	opt(f)
}

if f.rosettaClient == nil {
    f.rosettaClient = newDefaultAPIClient(c.maxConnections)
}

Also, that way we're also not building out that client, if it is already being passed in with the WithClient option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch! And great suggestion! There is no need for us to init the fetcher with a client at all. We can delay ituntil after the opts and that gives us much more sensible code!

@septerr septerr requested a review from alex-stone March 3, 2021 01:51
@coveralls
Copy link

coveralls commented Mar 3, 2021

Pull Request Test Coverage Report for Build 14509

  • 22 of 25 (88.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 77.861%

Changes Missing Coverage Covered Lines Changed/Added Lines %
fetcher/fetcher.go 22 25 88.0%
Totals Coverage Status
Change from base Build 14477: 0.04%
Covered Lines: 8388
Relevant Lines: 10773

💛 - Coveralls

)
fetcher := New("https://serveraddress", WithClient(apiClient))
var assert = assert.New(t)
assert.Same(httpClient, fetcher.rosettaClient.GetConfig().HTTPClient)
Copy link
Contributor Author

@septerr septerr Mar 3, 2021

Choose a reason for hiding this comment

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

lmk if you think we should assert that the Transport field of the httpClient remains the same as well.

@septerr septerr requested a review from yfl92 March 3, 2021 02:01
yfl92
yfl92 previously approved these changes Mar 3, 2021
Copy link

@yfl92 yfl92 left a comment

Choose a reason for hiding this comment

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

LGTM - left a nit but it's trivial

Comment on lines 119 to 120
client := client.NewAPIClient(clientCfg)
f.rosettaClient = client
Copy link

Choose a reason for hiding this comment

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

nit: seems like we can consolidate them into (but it's trivial)

Suggested change
client := client.NewAPIClient(clientCfg)
f.rosettaClient = client
f.rosettaClient = client.NewAPIClient(clientCfg)

@heimdall-asguard
Copy link

Review Error for alex-stone @ 2021-03-03 02:25:14 UTC
User failed mfa authentication, public email is not set on your github profile. see go/mfa-help

@septerr septerr merged commit 7c2169d into master Mar 3, 2021
@septerr septerr deleted the sgupta/do-not-override-httpclient branch March 3, 2021 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants