Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ type Sessions[I identity.UserIdentity] interface {
}
```

#### package webtools/httpclient

Provides a sharable `http.Client` that sets sensible values for maintaining a
pool of idle connections.

```go
client := httpclient.Get()
```

#### package webtools/middles/identity

Provides a set of generic structs used for marshaling identity. The interfaces
Expand Down
36 changes: 36 additions & 0 deletions httpclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package httpclient

import (
"net"
"net/http"
"time"
)

func pooledTransport() *http.Transport {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 32,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 3 * time.Second,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: 1,
}
return transport
}

var client = &http.Client{
Timeout: 1 * time.Minute,
Transport: pooledTransport(),
}

// Get returns an http.Client tuned for shared / pooled connections, with
// reasonable values for idle connections, timeouts, and http2 usage.
func Get() *http.Client {
return client
}
15 changes: 15 additions & 0 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package httpclient

import (
"testing"
"time"

"github.com/shoenig/test/must"
)

func TestGet(t *testing.T) {
t.Parallel()

c := Get()
must.Eq(t, 1*time.Minute, c.Timeout)
}