Replace usage of http.DefaultClient and http.DefaultTransport (#104135)

Remove usage of http.DefaultClient and http.DefaultTransport

Part of grafana/data-sources#484
This commit is contained in:
beejeebus
2025-05-09 13:26:39 -04:00
committed by GitHub
parent 8f4b2bbece
commit 8f79e4882f
18 changed files with 120 additions and 44 deletions
+20 -1
View File
@@ -2,10 +2,12 @@ package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
@@ -26,6 +28,23 @@ import (
const grafanaAPI = "https://grafana.com/api"
var httpClient = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: func(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
return dialer.DialContext
}(&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}),
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
// GrafanaCom implements the sub-command "grafana-com".
func GrafanaCom(c *cli.Context) error {
bucketStr := c.String("src-bucket")
@@ -330,7 +349,7 @@ func postRequest(cfg packaging.PublishConfig, pth string, body any, descr string
return nil
}
resp, err := http.DefaultClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed posting to %s (%s): %s", u, descr, err)
}