Skip to content

Commit 96365ad

Browse files
committed
fix: apply HttpTransports when cluster http client provide
1 parent 7227de3 commit 96365ad

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

pkg/courierhttp/client/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ import (
1818
"github.com/octohelm/courier/pkg/statuserror"
1919
)
2020

21+
type HttpTransport = func(rt http.RoundTripper) http.RoundTripper
22+
23+
func WithHttpTransports(rts ...HttpTransport) func(rt http.RoundTripper) http.RoundTripper {
24+
return func(r http.RoundTripper) http.RoundTripper {
25+
for _, rt := range rts {
26+
r = rt(r)
27+
}
28+
return r
29+
}
30+
}
31+
2132
type RoundTrip = func(request *http.Request) (*http.Response, error)
2233

2334
func HttpTransportFunc(round func(request *http.Request, next RoundTrip) (*http.Response, error)) HttpTransport {
@@ -38,8 +49,6 @@ func (h *httpTransportFunc) RoundTrip(request *http.Request) (*http.Response, er
3849
return h.round(request, h.rt.RoundTrip)
3950
}
4051

41-
type HttpTransport = func(rt http.RoundTripper) http.RoundTripper
42-
4352
type Client struct {
4453
Endpoint string `flag:""`
4554
NewError func() error
@@ -62,6 +71,8 @@ func (c *Client) Do(ctx context.Context, req any, metas ...courier.Metadata) cou
6271
httpClient := HttpClientFromContext(ctx)
6372
if httpClient == nil {
6473
httpClient = GetReasonableClientContext(ctx, c.HttpTransports...)
74+
} else {
75+
httpClient.Transport = WithHttpTransports(c.HttpTransports...)(httpClient.Transport)
6576
}
6677

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

pkg/courierhttp/client/http_default.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ func GetReasonableClientContext(ctx context.Context, httpTransports ...HttpTrans
3535
t = tc()
3636
}
3737

38-
for i := range httpTransports {
39-
t = httpTransports[i](t)
40-
}
41-
42-
return &http.Client{Transport: t}
38+
return &http.Client{Transport: WithHttpTransports(httpTransports...)(t)}
4339
}
4440

4541
func newRoundTripperWithoutKeepAlive() http.RoundTripper {
@@ -67,9 +63,5 @@ func GetShortConnClientContext(ctx context.Context, httpTransports ...HttpTransp
6763
t = tc()
6864
}
6965

70-
for i := range httpTransports {
71-
t = httpTransports[i](t)
72-
}
73-
74-
return &http.Client{Transport: t}
66+
return &http.Client{Transport: WithHttpTransports(httpTransports...)(t)}
7567
}

0 commit comments

Comments
 (0)