From 331c756471dc307575a3446535e4d97d4789e422 Mon Sep 17 00:00:00 2001 From: "Chris S. Kim" Date: Thu, 8 Sep 2022 11:02:05 -0400 Subject: [PATCH] Reuse http.DefaultTransport in UIMetricsProxy (#14521) http.Transport keeps a pool of connections and should be reused when possible. We instantiate a new http.DefaultTransport for every metrics request, making large numbers of concurrent requests inefficiently spin up new connections instead of reusing open ones. --- .changelog/14521.txt | 3 +++ agent/agent.go | 5 +++-- agent/http.go | 4 ++++ agent/ui_endpoint.go | 1 + 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .changelog/14521.txt diff --git a/.changelog/14521.txt b/.changelog/14521.txt new file mode 100644 index 000000000..8dc0de40f --- /dev/null +++ b/.changelog/14521.txt @@ -0,0 +1,3 @@ +```release-note:improvement +ui: Reuse connections for requests to /v1/internal/ui/metrics-proxy/ +``` diff --git a/agent/agent.go b/agent/agent.go index b52c480c2..1bcb6c148 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -939,8 +939,9 @@ func (a *Agent) listenHTTP() ([]apiServer, error) { } srv := &HTTPHandlers{ - agent: a, - denylist: NewDenylist(a.config.HTTPBlockEndpoints), + agent: a, + denylist: NewDenylist(a.config.HTTPBlockEndpoints), + proxyTransport: http.DefaultTransport, } a.configReloaders = append(a.configReloaders, srv.ReloadConfig) a.httpHandlers = srv diff --git a/agent/http.go b/agent/http.go index 98beb6feb..ff7764f0d 100644 --- a/agent/http.go +++ b/agent/http.go @@ -81,6 +81,10 @@ type HTTPHandlers struct { configReloaders []ConfigReloader h http.Handler metricsProxyCfg atomic.Value + + // proxyTransport is used by UIMetricsProxy to keep + // a managed pool of connections. + proxyTransport http.RoundTripper } // endpoint is a Consul-specific HTTP handler that takes the usual arguments in diff --git a/agent/ui_endpoint.go b/agent/ui_endpoint.go index df6f359de..a418a4017 100644 --- a/agent/ui_endpoint.go +++ b/agent/ui_endpoint.go @@ -771,6 +771,7 @@ func (s *HTTPHandlers) UIMetricsProxy(resp http.ResponseWriter, req *http.Reques Director: func(r *http.Request) { r.URL = u }, + Transport: s.proxyTransport, ErrorLog: log.StandardLogger(&hclog.StandardLoggerOptions{ InferLevels: true, }),