Skip to content

Commit aa45d8f

Browse files
fix(go): avoid panic when http.DefaultTransport is wrapped
defaultHTTPClient performed an unchecked type assertion on http.DefaultTransport, which panicked for any caller that had wrapped the global transport (e.g. otelhttp.NewTransport for distributed tracing). When the assertion fails, fall back to the wrapped RoundTripper as-is — preserving the caller's wrapping at the cost of ResponseHeaderTimeout, which is strictly better than panicking.
1 parent 91f6f4a commit aa45d8f

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

default_http_client.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import (
1414
const defaultResponseHeaderTimeout = 10 * time.Minute
1515

1616
// defaultHTTPClient returns an [*http.Client] used when the caller does not
17-
// supply one via [option.WithHTTPClient]. It clones [http.DefaultTransport]
18-
// and adds a [http.Transport.ResponseHeaderTimeout] so stuck connections
19-
// fail fast instead of compounding across retries.
17+
// supply one via [option.WithHTTPClient]. When [http.DefaultTransport] is the
18+
// stdlib [*http.Transport], it is cloned and a [http.Transport.ResponseHeaderTimeout]
19+
// is set so stuck connections fail fast instead of compounding across retries.
20+
// If [http.DefaultTransport] has been wrapped (for example by otelhttp for
21+
// distributed tracing), the wrapping is preserved and the header timeout is
22+
// skipped.
2023
func defaultHTTPClient() *http.Client {
21-
transport := http.DefaultTransport.(*http.Transport).Clone()
22-
transport.ResponseHeaderTimeout = defaultResponseHeaderTimeout
23-
return &http.Client{Transport: transport}
24+
if t, ok := http.DefaultTransport.(*http.Transport); ok {
25+
t = t.Clone()
26+
t.ResponseHeaderTimeout = defaultResponseHeaderTimeout
27+
return &http.Client{Transport: t}
28+
}
29+
return &http.Client{Transport: http.DefaultTransport}
2430
}

0 commit comments

Comments
 (0)