diff --git a/app/vmauth/main.go b/app/vmauth/main.go index c4f0154f31..88c75c97b3 100644 --- a/app/vmauth/main.go +++ b/app/vmauth/main.go @@ -7,6 +7,7 @@ import ( "net/http/httputil" "net/url" "os" + "sync" "time" "github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo" @@ -108,7 +109,7 @@ func proxyRequest(w http.ResponseWriter, r *http.Request) { // Forward other panics to the caller. panic(err) }() - reverseProxy.ServeHTTP(w, r) + getReverseProxy().ServeHTTP(w, r) } var ( @@ -117,29 +118,42 @@ var ( missingRouteRequests = metrics.NewCounter(`vmauth_http_request_errors_total{reason="missing_route"}`) ) -var reverseProxy = &httputil.ReverseProxy{ - Director: func(r *http.Request) { - targetURL := r.Header.Get("vm-target-url") - target, err := url.Parse(targetURL) - if err != nil { - logger.Panicf("BUG: unexpected error when parsing targetURL=%q: %s", targetURL, err) - } - r.URL = target - }, - Transport: func() *http.Transport { - tr := http.DefaultTransport.(*http.Transport).Clone() - // Automatic compression must be disabled in order to fix https://github.com/VictoriaMetrics/VictoriaMetrics/issues/535 - tr.DisableCompression = true - // Disable HTTP/2.0, since VictoriaMetrics components don't support HTTP/2.0 (because there is no sense in this). - tr.ForceAttemptHTTP2 = false - tr.MaxIdleConnsPerHost = *maxIdleConnsPerBackend - if tr.MaxIdleConns != 0 && tr.MaxIdleConns < tr.MaxIdleConnsPerHost { - tr.MaxIdleConns = tr.MaxIdleConnsPerHost - } - return tr - }(), - FlushInterval: time.Second, - ErrorLog: logger.StdErrorLogger(), +var ( + reverseProxy *httputil.ReverseProxy + reverseProxyOnce sync.Once +) + +func getReverseProxy() *httputil.ReverseProxy { + reverseProxyOnce.Do(initReverseProxy) + return reverseProxy +} + +// initReverseProxy must be called after flag.Parse(), since it uses command-line flags. +func initReverseProxy() { + reverseProxy = &httputil.ReverseProxy{ + Director: func(r *http.Request) { + targetURL := r.Header.Get("vm-target-url") + target, err := url.Parse(targetURL) + if err != nil { + logger.Panicf("BUG: unexpected error when parsing targetURL=%q: %s", targetURL, err) + } + r.URL = target + }, + Transport: func() *http.Transport { + tr := http.DefaultTransport.(*http.Transport).Clone() + // Automatic compression must be disabled in order to fix https://github.com/VictoriaMetrics/VictoriaMetrics/issues/535 + tr.DisableCompression = true + // Disable HTTP/2.0, since VictoriaMetrics components don't support HTTP/2.0 (because there is no sense in this). + tr.ForceAttemptHTTP2 = false + tr.MaxIdleConnsPerHost = *maxIdleConnsPerBackend + if tr.MaxIdleConns != 0 && tr.MaxIdleConns < tr.MaxIdleConnsPerHost { + tr.MaxIdleConns = tr.MaxIdleConnsPerHost + } + return tr + }(), + FlushInterval: time.Second, + ErrorLog: logger.StdErrorLogger(), + } } func usage() { diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d548389c53..bb6b53971f 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,8 @@ sort: 15 ## tip +* BUGFIX: vmauth: properly take into account the value passed to `-maxIdleConnsPerBackend` command-line flag. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1300). + ## [v1.69.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.69.0)