Skip to content

Commit 5da3030

Browse files
authored
add ability to configure Timeout for http.Client (#48)
* centralize all http.Client creation * set the timeout from the clientCreator * add the configuration method * add example and readme * lint * Update client_creator.go
1 parent f2f66d4 commit 5da3030

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ distinct clients:
176176
These are provided when calling `githubapp.NewClientCreator`:
177177

178178
- `githubapp.WithClientUserAgent` sets a `User-Agent` string for all clients
179+
- `githubapp.WithClientTimeout` sets a timeout for requests made by all clients
179180
- `githubapp.WithClientCaching` enables response caching for all v3 (REST) clients.
180181
The cache can be configured to always validate responses or to respect
181182
the cache headers returned by GitHub. Re-validation is useful if data

example/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"os"
19+
"time"
1920

2021
"github.com/gregjones/httpcache"
2122
"github.com/palantir/go-baseapp/baseapp"
@@ -43,6 +44,7 @@ func main() {
4344
cc, err := githubapp.NewDefaultCachingClientCreator(
4445
config.Github,
4546
githubapp.WithClientUserAgent("example-app/1.0.0"),
47+
githubapp.WithClientTimeout(3*time.Second),
4648
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
4749
githubapp.WithClientMiddleware(
4850
githubapp.ClientMetrics(server.Registry()),

githubapp/client_creator.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/url"
2222
"regexp"
2323
"strings"
24+
"time"
2425

2526
"github.com/bradleyfalzon/ghinstallation"
2627
"github.com/google/go-github/v32/github"
@@ -127,6 +128,7 @@ type clientCreator struct {
127128
middleware []ClientMiddleware
128129
cacheFunc func() httpcache.Cache
129130
alwaysValidate bool
131+
timeout time.Duration
130132
}
131133

132134
var _ ClientCreator = &clientCreator{}
@@ -156,6 +158,13 @@ func WithClientCaching(alwaysValidate bool, cache func() httpcache.Cache) Client
156158
}
157159
}
158160

161+
// WithClientTimeout sets a request timeout for requests made by all created clients.
162+
func WithClientTimeout(timeout time.Duration) ClientOption {
163+
return func(c *clientCreator) {
164+
c.timeout = timeout
165+
}
166+
}
167+
159168
// WithClientMiddleware adds middleware that is applied to all created clients.
160169
func WithClientMiddleware(middleware ...ClientMiddleware) ClientOption {
161170
return func(c *clientCreator) {
@@ -164,7 +173,7 @@ func WithClientMiddleware(middleware ...ClientMiddleware) ClientOption {
164173
}
165174

166175
func (c *clientCreator) NewAppClient() (*github.Client, error) {
167-
base := &http.Client{Transport: http.DefaultTransport}
176+
base := c.newHTTPClient()
168177
installation, transportError := newAppInstallation(c.integrationID, c.privKeyBytes, c.v3BaseURL)
169178

170179
middleware := []ClientMiddleware{installation}
@@ -183,7 +192,7 @@ func (c *clientCreator) NewAppClient() (*github.Client, error) {
183192
}
184193

185194
func (c *clientCreator) NewAppV4Client() (*githubv4.Client, error) {
186-
base := &http.Client{Transport: http.DefaultTransport}
195+
base := c.newHTTPClient()
187196
installation, transportError := newAppInstallation(c.integrationID, c.privKeyBytes, c.v3BaseURL)
188197

189198
// The v4 API primarily uses POST requests (except for introspection queries)
@@ -201,7 +210,7 @@ func (c *clientCreator) NewAppV4Client() (*githubv4.Client, error) {
201210
}
202211

203212
func (c *clientCreator) NewInstallationClient(installationID int64) (*github.Client, error) {
204-
base := &http.Client{Transport: http.DefaultTransport}
213+
base := c.newHTTPClient()
205214
installation, transportError := newInstallation(c.integrationID, installationID, c.privKeyBytes, c.v3BaseURL)
206215

207216
middleware := []ClientMiddleware{installation}
@@ -220,7 +229,7 @@ func (c *clientCreator) NewInstallationClient(installationID int64) (*github.Cli
220229
}
221230

222231
func (c *clientCreator) NewInstallationV4Client(installationID int64) (*githubv4.Client, error) {
223-
base := &http.Client{Transport: http.DefaultTransport}
232+
base := c.newHTTPClient()
224233
installation, transportError := newInstallation(c.integrationID, installationID, c.privKeyBytes, c.v3BaseURL)
225234

226235
// The v4 API primarily uses POST requests (except for introspection queries)
@@ -249,6 +258,13 @@ func (c *clientCreator) NewTokenV4Client(token string) (*githubv4.Client, error)
249258
return c.newV4Client(tc, nil, "oauth token")
250259
}
251260

261+
func (c *clientCreator) newHTTPClient() *http.Client {
262+
return &http.Client{
263+
Transport: http.DefaultTransport,
264+
Timeout: c.timeout,
265+
}
266+
}
267+
252268
func (c *clientCreator) newClient(base *http.Client, middleware []ClientMiddleware, details string, installID int64) (*github.Client, error) {
253269
applyMiddleware(base, [][]ClientMiddleware{
254270
{setInstallationID(installID)},

0 commit comments

Comments
 (0)