Skip to content

Don't update httpClient passed to NewClient - #3011

Merged
gmlewis merged 1 commit into
google:masterfrom
WillAbides:reuse-client
Dec 16, 2023
Merged

Don't update httpClient passed to NewClient#3011
gmlewis merged 1 commit into
google:masterfrom
WillAbides:reuse-client

Conversation

@WillAbides

@WillAbidesWillAbides commented Dec 1, 2023

Copy link
Copy Markdown
Contributor

Users can accidentally leak credentials by doing something like this:

pat:=os.Getenv("GITHUB_TOKEN")
httpClient:=&http.Client{
Timeout: time.Second*30,
}
ghClient:=github.NewClient(httpClient).WithAuthToken(pat)
// ... do something with ghClienthttpClient.Get("https://some-sketchy-site")

This PR fixes that by updating NewClient to make a copy of httpClient so that when its transport is updated by WithAuthToken, the original httpClient won't have its transport updated as well.

It also updates TestWithAuthToken to check for leaks

@codecov

codecovBot commented Dec 1, 2023

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (062b611) 97.91% compared to head (593c130) 97.89%.

Additional details and impacted files
@@ Coverage Diff @@## master #3011 +/- ##
==========================================
- Coverage 97.91% 97.89% -0.03% 
==========================================
Files 150 150 Lines 12980 12984 +4 ==========================================
+ Hits 12710 12711 +1 - Misses 192 194 +2 - Partials 78 79 +1 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @WillAbides !
LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

@gmlewisgmlewis added the NeedsReview PR is awaiting a review before merging. label Dec 1, 2023

@valbeatvalbeat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@gmlewisgmlewis removed the NeedsReview PR is awaiting a review before merging. label Dec 16, 2023
@gmlewis

Copy link
Copy Markdown
Collaborator

Thank you, @valbeat !
Merging.

@gmlewis
gmlewis merged commit 6d3dfc6 into google:masterDec 16, 2023
gmlewis pushed a commit to WillAbides/go-github that referenced this pull request Dec 18, 2023
gmlewis pushed a commit to o-sama/go-github that referenced this pull request Dec 19, 2023
fkorotkov added a commit to cirruslabs/cirrus-cli that referenced this pull request Feb 15, 2024
Use a single instance with tweaked defaults across all the invocations. Plus use newer API of `go-github` (including google/go-github#3011 fix).
Previously a default client was used which doesn't have nor request timeout nor idle connection timeout. This resulted in poor `p99` performance since sometimes connections were hanging blocking others to access `github.com`.
fkorotkov added a commit to cirruslabs/cirrus-cli that referenced this pull request Feb 16, 2024
* Optimize HTTP client used for GitHub
Use a single instance with tweaked defaults across all the invocations. Plus use newer API of `go-github` (including google/go-github#3011 fix).
Previously a default client was used which doesn't have nor request timeout nor idle connection timeout. This resulted in poor `p99` performance since sometimes connections were hanging blocking others to access `github.com`.
* Renamed to `defaultGitHubClient`
jlaportebot added a commit to jlaportebot/go-github that referenced this pull request Jun 28, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@WillAbides@gmlewis@valbeat