Skip to content

Commit

Permalink
Recommend creating http.Transport with default settings (#20)
Browse files Browse the repository at this point in the history
* Recommend creating http.Transport with default settings

This is now the recommended way to create a transport in golang 1.13, to ensure that default settings (such as to close idle connections) are inherited.

* specify go1.12 way too
  • Loading branch information
tmm1 authored and mattrobenolt committed Oct 21, 2019
1 parent 1723c4b commit 0944d24
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can use the `gocertifi` package as follows:
```go
import "github.com/certifi/gocertifi"

cert_pool, err := gocertifi.CACerts()
certPool, err := gocertifi.CACerts()
```

You can use the returned `*x509.CertPool` as part of an HTTP transport, for example:
Expand All @@ -29,8 +29,22 @@ import (

// Setup an HTTP client with a custom transport
transport := &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: cert_pool},
Proxy: ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// or, starting with go1.13 simply use:
// transport := http.DefaultTransport.(*http.Transport).Clone()

transport.TLSClientConfig = &tls.Config{RootCAs: certPool}
client := &http.Client{Transport: transport}

// Make an HTTP request using our custom transport
Expand Down

0 comments on commit 0944d24

Please sign in to comment.