Skip to content

Commit b3c566c

Browse files
committed
Add clean HTTP client and improve token source
Introduces a cleanHTTPClient function for better HTTP client defaults and connection pooling. Updates NewInstallationTokenSource to use the new HTTP client and wraps the installationTokenSource with oauth2.ReuseTokenSource for improved token reuse.
1 parent 6597bf3 commit b3c566c

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

auth.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,24 @@ type installationTokenSource struct {
187187
func NewInstallationTokenSource(id int64, src oauth2.TokenSource, opts ...InstallationTokenSourceOpt) oauth2.TokenSource {
188188
ctx := context.Background()
189189

190+
httpClient := cleanHTTPClient()
191+
httpClient.Transport = &oauth2.Transport{
192+
Source: oauth2.ReuseTokenSource(nil, src),
193+
Base: httpClient.Transport,
194+
}
195+
190196
i := &installationTokenSource{
191197
id: id,
192198
ctx: ctx,
193199
src: src,
194-
client: github.NewClient(oauth2.NewClient(ctx, src)),
200+
client: github.NewClient(httpClient),
195201
}
196202

197203
for _, opt := range opts {
198204
opt(i)
199205
}
200206

201-
return i
207+
return oauth2.ReuseTokenSource(nil, i)
202208
}
203209

204210
// Token generates a new GitHub App installation token for authenticating as a GitHub App installation.

http.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package githubauth
2+
3+
import (
4+
"net"
5+
"net/http"
6+
"runtime"
7+
"time"
8+
)
9+
10+
// cleanHTTPClient returns a new http.Client with clean defaults and connection pooling.
11+
// Implementation based on github.com/hashicorp/go-cleanhttp
12+
// Licensed under MPL-2.0: https://github.com/hashicorp/go-cleanhttp/blob/master/LICENSE
13+
func cleanHTTPClient() *http.Client {
14+
return &http.Client{
15+
Transport: &http.Transport{
16+
Proxy: http.ProxyFromEnvironment,
17+
DialContext: (&net.Dialer{
18+
Timeout: 30 * time.Second,
19+
KeepAlive: 30 * time.Second,
20+
DualStack: true,
21+
}).DialContext,
22+
MaxIdleConns: 100,
23+
IdleConnTimeout: 90 * time.Second,
24+
TLSHandshakeTimeout: 10 * time.Second,
25+
ExpectContinueTimeout: 1 * time.Second,
26+
ForceAttemptHTTP2: true,
27+
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
28+
},
29+
}
30+
}

0 commit comments

Comments
 (0)