Skip to content

Commit 8ad0064

Browse files
authored
refactor(client): new http.Client init function (#242)
1 parent d54aed9 commit 8ad0064

5 files changed

Lines changed: 33 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
### Added
9+
- client functions `NewDefaultHTTPClient` and `NewDefaultHTTPTransport` to provide HTTP client default properties
10+
811
## [6.3.2]
912

1013
### Added

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.18
55
require (
66
github.com/davecgh/go-spew v1.1.1
77
github.com/dnaeon/go-vcr v1.2.0
8-
github.com/hashicorp/go-cleanhttp v0.5.1
98
github.com/stretchr/testify v1.7.2
109
)
1110

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
55
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
6-
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
7-
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
86
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
97
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
108
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

upcloud/client/client.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"crypto/tls"
77
"fmt"
88
"io"
9+
"net"
910
"net/http"
1011
"net/url"
1112
"os"
1213
"strings"
1314
"time"
14-
15-
"github.com/hashicorp/go-cleanhttp"
1615
)
1716

1817
const (
@@ -189,7 +188,7 @@ func New(username, password string, c ...ConfigFn) *Client {
189188
username: username,
190189
password: password,
191190
baseURL: clientBaseURL(os.Getenv(EnvDebugAPIBaseURL)),
192-
httpClient: cleanhttp.DefaultClient(),
191+
httpClient: NewDefaultHTTPClient(),
193192
}
194193

195194
// If set, replace http client transport with one skipping tls verification
@@ -243,3 +242,30 @@ func handleResponse(response *http.Response) ([]byte, error) {
243242

244243
return responseBody, err
245244
}
245+
246+
// NewDefaultHTTPClient returns new default http.Client.
247+
func NewDefaultHTTPClient() *http.Client {
248+
transport := NewDefaultHTTPTransport()
249+
return &http.Client{
250+
Transport: transport,
251+
}
252+
}
253+
254+
// NewDefaultHTTPTransport return new HTTP client transport round tripper.
255+
func NewDefaultHTTPTransport() http.RoundTripper {
256+
return &http.Transport{
257+
Proxy: http.ProxyFromEnvironment,
258+
DialContext: (&net.Dialer{
259+
Timeout: 30 * time.Second,
260+
KeepAlive: 30 * time.Second,
261+
DualStack: true,
262+
}).DialContext,
263+
ForceAttemptHTTP2: true,
264+
MaxIdleConns: 100,
265+
IdleConnTimeout: 90 * time.Second,
266+
TLSHandshakeTimeout: 10 * time.Second,
267+
ExpectContinueTimeout: 1 * time.Second,
268+
DisableKeepAlives: true,
269+
MaxIdleConnsPerHost: -1,
270+
}
271+
}

upcloud/service/utils_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
1616
"github.com/dnaeon/go-vcr/cassette"
1717
"github.com/dnaeon/go-vcr/recorder"
18-
"github.com/hashicorp/go-cleanhttp"
1918
"github.com/stretchr/testify/require"
2019
)
2120

@@ -77,7 +76,7 @@ func record(t *testing.T, fixture string, f func(context.Context, *testing.T, *r
7776

7877
user, password := getCredentials()
7978

80-
httpClient := cleanhttp.DefaultClient()
79+
httpClient := client.NewDefaultHTTPClient()
8180
origTransport := httpClient.Transport
8281
r.SetTransport(origTransport)
8382
httpClient.Transport = r

0 commit comments

Comments
 (0)