Skip to content

Commit 7227de3

Browse files
committed
feat: better default client
1 parent 26f06b3 commit 7227de3

File tree

3 files changed

+80
-36
lines changed

3 files changed

+80
-36
lines changed

pkg/courierhttp/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *Client) Do(ctx context.Context, req any, metas ...courier.Metadata) cou
6161

6262
httpClient := HttpClientFromContext(ctx)
6363
if httpClient == nil {
64-
httpClient = GetShortConnClientContext(ctx, c.HttpTransports...)
64+
httpClient = GetReasonableClientContext(ctx, c.HttpTransports...)
6565
}
6666

6767
resp, err := httpClient.Do(httpReq)

pkg/courierhttp/client/http.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package client
22

33
import (
44
"context"
5-
"net"
6-
"net/http"
7-
"time"
8-
95
contextx "github.com/octohelm/x/context"
10-
"golang.org/x/net/http2"
6+
"net/http"
117
)
128

139
type contextKeyClient struct{}
@@ -23,19 +19,6 @@ func HttpClientFromContext(ctx context.Context) *http.Client {
2319
return nil
2420
}
2521

26-
func newDefaultRoundTripper() http.RoundTripper {
27-
return &http.Transport{
28-
DialContext: (&net.Dialer{
29-
Timeout: 5 * time.Second,
30-
KeepAlive: 0,
31-
}).DialContext,
32-
DisableKeepAlives: true,
33-
TLSHandshakeTimeout: 5 * time.Second,
34-
ResponseHeaderTimeout: 5 * time.Second,
35-
ExpectContinueTimeout: 1 * time.Second,
36-
}
37-
}
38-
3922
type RoundTripperCreateFunc = func() http.RoundTripper
4023

4124
type contextRoundTripperCreator struct{}
@@ -44,23 +27,9 @@ func ContextWithRoundTripperCreator(ctx context.Context, newRoundTripper RoundTr
4427
return contextx.WithValue(ctx, contextRoundTripperCreator{}, newRoundTripper)
4528
}
4629

47-
func RoundTripperCreatorFromContext(ctx context.Context) func() http.RoundTripper {
30+
func RoundTripperCreatorFromContext(ctx context.Context) (func() http.RoundTripper, bool) {
4831
if t, ok := ctx.Value(contextRoundTripperCreator{}).(func() http.RoundTripper); ok {
49-
return t
32+
return t, true
5033
}
51-
return newDefaultRoundTripper
52-
}
53-
54-
func GetShortConnClientContext(ctx context.Context, httpTransports ...HttpTransport) *http.Client {
55-
t := RoundTripperCreatorFromContext(ctx)()
56-
57-
if ht, ok := t.(*http.Transport); ok {
58-
_ = http2.ConfigureTransport(ht)
59-
}
60-
61-
for i := range httpTransports {
62-
t = httpTransports[i](t)
63-
}
64-
65-
return &http.Client{Transport: t}
34+
return nil, false
6635
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"net"
6+
"net/http"
7+
"time"
8+
)
9+
10+
var reasonableRoundTripper http.RoundTripper = &http.Transport{
11+
Proxy: http.ProxyFromEnvironment,
12+
13+
DialContext: (&net.Dialer{
14+
Timeout: 30 * time.Second,
15+
KeepAlive: 30 * time.Second,
16+
}).DialContext,
17+
18+
MaxIdleConns: 100,
19+
MaxIdleConnsPerHost: 10,
20+
IdleConnTimeout: 90 * time.Second,
21+
22+
TLSHandshakeTimeout: 10 * time.Second,
23+
ExpectContinueTimeout: 1 * time.Second,
24+
25+
ResponseHeaderTimeout: 60 * time.Second,
26+
27+
ForceAttemptHTTP2: true,
28+
}
29+
30+
func GetReasonableClientContext(ctx context.Context, httpTransports ...HttpTransport) *http.Client {
31+
t := reasonableRoundTripper
32+
33+
tc, ok := RoundTripperCreatorFromContext(ctx)
34+
if ok {
35+
t = tc()
36+
}
37+
38+
for i := range httpTransports {
39+
t = httpTransports[i](t)
40+
}
41+
42+
return &http.Client{Transport: t}
43+
}
44+
45+
func newRoundTripperWithoutKeepAlive() http.RoundTripper {
46+
return &http.Transport{
47+
Proxy: http.ProxyFromEnvironment,
48+
49+
DialContext: (&net.Dialer{
50+
Timeout: 30 * time.Second,
51+
KeepAlive: 0,
52+
}).DialContext,
53+
54+
DisableKeepAlives: true,
55+
56+
TLSHandshakeTimeout: 10 * time.Second,
57+
ExpectContinueTimeout: 1 * time.Second,
58+
ResponseHeaderTimeout: 60 * time.Second,
59+
}
60+
}
61+
62+
func GetShortConnClientContext(ctx context.Context, httpTransports ...HttpTransport) *http.Client {
63+
t := newRoundTripperWithoutKeepAlive()
64+
65+
tc, ok := RoundTripperCreatorFromContext(ctx)
66+
if ok {
67+
t = tc()
68+
}
69+
70+
for i := range httpTransports {
71+
t = httpTransports[i](t)
72+
}
73+
74+
return &http.Client{Transport: t}
75+
}

0 commit comments

Comments
 (0)