From f1c1097bbe57f1bb0679d85f284ff363f43c958c Mon Sep 17 00:00:00 2001 From: Robert Fratto Date: Tue, 8 Oct 2019 10:10:17 -0400 Subject: [PATCH] pkg/canary: use default HTTP client when reading from Loki The documentation[1] for net/http.Client says the following: A Client is an HTTP client. Its zero value (DefaultClient) is a usable client that uses DefaultTransport. The Client's Transport typically has internal state (cached TCP connections), so Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines. A Client is higher-level than a RoundTripper (such as Transport) and additionally handles HTTP details such as cookies and redirects. Canary was creating a new http.Client every time a query request was made to Loki, causing a new TCP connection to be established each time. If Loki has an outage, it's possible to get into a situation where all websocket requests are failing and for a high volume of requests to go through the normal query route. This can possibly lead to networking issues like socket starvation. [1]: https://golang.org/pkg/net/http/#Client --- pkg/canary/reader/reader.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/canary/reader/reader.go b/pkg/canary/reader/reader.go index 6693caba813b9..1b1824c4281f0 100644 --- a/pkg/canary/reader/reader.go +++ b/pkg/canary/reader/reader.go @@ -109,8 +109,6 @@ func (r *Reader) Query(start time.Time, end time.Time) ([]time.Time, error) { } _, _ = fmt.Fprintf(r.w, "Querying loki for missing values with query: %v\n", u.String()) - client := &http.Client{} - req, err := http.NewRequest("GET", u.String(), nil) if err != nil { return nil, err @@ -118,7 +116,7 @@ func (r *Reader) Query(start time.Time, end time.Time) ([]time.Time, error) { req.SetBasicAuth(r.user, r.pass) - resp, err := client.Do(req) + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err }