Skip to content

Commit 6965c31

Browse files
authored
Adds NewHTTPTransport function to httpclient (#1303)
This PR adds `httpclient.NewHTTPTransport`, which returns a new http transport based off the definition in the Golang stdlib `http.DefaultTransport`. It's not a clone, because that would return any mutations of the stdlib `http.DefaultTransport` from other code at the time of the call. Any plugin that needs a default http transport should use this function.
1 parent f8bcc37 commit 6965c31

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

backend/httpclient/http_client.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package httpclient
22

33
import (
4+
"context"
45
"crypto/tls"
56
"crypto/x509"
67
"errors"
78
"fmt"
89
"net"
910
"net/http"
11+
"time"
1012

1113
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
1214
)
@@ -50,7 +52,7 @@ func New(opts ...Options) (*http.Client, error) {
5052
// Note: If more than one Options is provided a panic is raised.
5153
func GetTransport(opts ...Options) (http.RoundTripper, error) {
5254
if opts == nil {
53-
return GetDefaultTransport()
55+
return NewHTTPTransport(), nil
5456
}
5557

5658
clientOpts := createOptions(opts...)
@@ -96,14 +98,30 @@ func GetTransport(opts ...Options) (http.RoundTripper, error) {
9698
return roundTripperFromMiddlewares(clientOpts, clientOpts.Middlewares, transport)
9799
}
98100

99-
// GetDefaultTransport returns a clone of http.DefaultTransport, if it's of the
100-
// correct type, or an error otherwise. There are a number of places where plugin
101-
// code uses http.DefaultTransport; this supports doing so in a safer way.
102-
func GetDefaultTransport() (http.RoundTripper, error) {
103-
if transport, ok := http.DefaultTransport.(*http.Transport); ok {
104-
return transport.Clone(), nil
101+
// NewHTTPTransport returns a new HTTP Transport, based off the definition in
102+
// the stdlib http.DefaultTransport. It's not a clone, because that would return
103+
// any mutations of http.DefaultTransport from other code at the time of the call.
104+
// Any plugin that needs a default http transport should use this function.
105+
func NewHTTPTransport() http.RoundTripper {
106+
return &http.Transport{
107+
Proxy: http.ProxyFromEnvironment,
108+
DialContext: func(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
109+
return dialer.DialContext
110+
}(&net.Dialer{
111+
Timeout: 30 * time.Second,
112+
KeepAlive: 30 * time.Second,
113+
}),
114+
ForceAttemptHTTP2: true,
115+
MaxIdleConns: 100,
116+
IdleConnTimeout: 90 * time.Second,
117+
TLSHandshakeTimeout: 10 * time.Second,
118+
ExpectContinueTimeout: 1 * time.Second,
105119
}
106-
return nil, fmt.Errorf("http.DefaultTransport is not *http.Transport but %T", http.DefaultTransport)
120+
}
121+
122+
// Deprecated - use NewHTTPTransport. Keeping for backwards compatibility.
123+
func GetDefaultTransport() (http.RoundTripper, error) {
124+
return NewHTTPTransport(), nil
107125
}
108126

109127
// GetTLSConfig creates a new tls.Config given provided options.

0 commit comments

Comments
 (0)