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.
This commit is contained in:
Chris S. Kim 2022-09-08 11:02:05 -04:00 committed by GitHub
parent 6cd25728e5
commit 331c756471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 2 deletions

3
.changelog/14521.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
ui: Reuse connections for requests to /v1/internal/ui/metrics-proxy/
```

View File

@ -941,6 +941,7 @@ func (a *Agent) listenHTTP() ([]apiServer, error) {
srv := &HTTPHandlers{ srv := &HTTPHandlers{
agent: a, agent: a,
denylist: NewDenylist(a.config.HTTPBlockEndpoints), denylist: NewDenylist(a.config.HTTPBlockEndpoints),
proxyTransport: http.DefaultTransport,
} }
a.configReloaders = append(a.configReloaders, srv.ReloadConfig) a.configReloaders = append(a.configReloaders, srv.ReloadConfig)
a.httpHandlers = srv a.httpHandlers = srv

View File

@ -81,6 +81,10 @@ type HTTPHandlers struct {
configReloaders []ConfigReloader configReloaders []ConfigReloader
h http.Handler h http.Handler
metricsProxyCfg atomic.Value 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 // endpoint is a Consul-specific HTTP handler that takes the usual arguments in

View File

@ -771,6 +771,7 @@ func (s *HTTPHandlers) UIMetricsProxy(resp http.ResponseWriter, req *http.Reques
Director: func(r *http.Request) { Director: func(r *http.Request) {
r.URL = u r.URL = u
}, },
Transport: s.proxyTransport,
ErrorLog: log.StandardLogger(&hclog.StandardLoggerOptions{ ErrorLog: log.StandardLogger(&hclog.StandardLoggerOptions{
InferLevels: true, InferLevels: true,
}), }),