Skip to content

Commit

Permalink
add ability to configure Timeout for http.Client (#48)
Browse files Browse the repository at this point in the history
* centralize all http.Client creation

* set the timeout from the clientCreator

* add the configuration method

* add example and readme

* lint

* Update client_creator.go
  • Loading branch information
jmcampanini authored Jul 16, 2020
1 parent f2f66d4 commit 5da3030
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ distinct clients:
These are provided when calling `githubapp.NewClientCreator`:

- `githubapp.WithClientUserAgent` sets a `User-Agent` string for all clients
- `githubapp.WithClientTimeout` sets a timeout for requests made by all clients
- `githubapp.WithClientCaching` enables response caching for all v3 (REST) clients.
The cache can be configured to always validate responses or to respect
the cache headers returned by GitHub. Re-validation is useful if data
Expand Down
2 changes: 2 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"os"
"time"

"github.com/gregjones/httpcache"
"github.com/palantir/go-baseapp/baseapp"
Expand Down Expand Up @@ -43,6 +44,7 @@ func main() {
cc, err := githubapp.NewDefaultCachingClientCreator(
config.Github,
githubapp.WithClientUserAgent("example-app/1.0.0"),
githubapp.WithClientTimeout(3*time.Second),
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
githubapp.WithClientMiddleware(
githubapp.ClientMetrics(server.Registry()),
Expand Down
24 changes: 20 additions & 4 deletions githubapp/client_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"regexp"
"strings"
"time"

"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v32/github"
Expand Down Expand Up @@ -127,6 +128,7 @@ type clientCreator struct {
middleware []ClientMiddleware
cacheFunc func() httpcache.Cache
alwaysValidate bool
timeout time.Duration
}

var _ ClientCreator = &clientCreator{}
Expand Down Expand Up @@ -156,6 +158,13 @@ func WithClientCaching(alwaysValidate bool, cache func() httpcache.Cache) Client
}
}

// WithClientTimeout sets a request timeout for requests made by all created clients.
func WithClientTimeout(timeout time.Duration) ClientOption {
return func(c *clientCreator) {
c.timeout = timeout
}
}

// WithClientMiddleware adds middleware that is applied to all created clients.
func WithClientMiddleware(middleware ...ClientMiddleware) ClientOption {
return func(c *clientCreator) {
Expand All @@ -164,7 +173,7 @@ func WithClientMiddleware(middleware ...ClientMiddleware) ClientOption {
}

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

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

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

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

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

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

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

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

func (c *clientCreator) newHTTPClient() *http.Client {
return &http.Client{
Transport: http.DefaultTransport,
Timeout: c.timeout,
}
}

func (c *clientCreator) newClient(base *http.Client, middleware []ClientMiddleware, details string, installID int64) (*github.Client, error) {
applyMiddleware(base, [][]ClientMiddleware{
{setInstallationID(installID)},
Expand Down

0 comments on commit 5da3030

Please sign in to comment.