Skip to content

Commit 0b73047

Browse files
observer: Reduce memory usage (#8649)
Reuse shared HTTP clients and transports instead of initializing a new client for every probe request, and close HTTP response bodies in CRL, HTTP, and TLS probers. These changes line up well with the heap and goroutine profiles, which show `net/http` transport churn and a large number of `net/http.(*persistConn).readLoop` / `writeLoop` goroutines accumulating, consistent with connections and associated transport buffers not being closed promptly. Fixes #8650
1 parent 84c9477 commit 0b73047

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

observer/obsclient/obsclient.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ import (
77
"net/http"
88
)
99

10-
// Client returns an http.Client for use in probers.
10+
var secureClient = newClient(false)
11+
var insecureClient = newClient(true)
12+
13+
// Client returns a shared *http.Client, with the appropriate TLS configuration,
14+
// shared across all probers.
1115
func Client(insecure bool) *http.Client {
16+
if insecure {
17+
return insecureClient
18+
}
19+
return secureClient
20+
}
21+
22+
func newClient(insecure bool) *http.Client {
1223
// Use the default transport, because it comes with useful defaults that are
1324
// not just the http.Transport zero-values.
1425
t := http.DefaultTransport.(*http.Transport).Clone()

observer/probers/crl/crl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (p CRLProbe) Probe(ctx context.Context) error {
4545
if err != nil {
4646
return err
4747
}
48+
defer resp.Body.Close()
4849

4950
body, err := io.ReadAll(resp.Body)
5051
if err != nil {

observer/probers/http/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (p HTTPProbe) Probe(ctx context.Context) error {
4545
if err != nil {
4646
return err
4747
}
48+
defer resp.Body.Close()
4849

4950
if !slices.Contains(p.rcodes, resp.StatusCode) {
5051
return fmt.Errorf("got HTTP status code %d, but want one of %#v", resp.StatusCode, p.rcodes)

observer/probers/tls/tls.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func checkOCSP(ctx context.Context, cert, issuer *x509.Certificate, want int) (b
8383
if err != nil {
8484
return false, err
8585
}
86+
defer res.Body.Close()
8687

8788
output, err := io.ReadAll(res.Body)
8889
if err != nil {

0 commit comments

Comments
 (0)