app/vmauth: initialize reverse proxy only after flag.Parse() is called

This should properly take into accoun the `-maxIdleConnsPerBackend` command-line flag value.
Previously it was hardcoded to 100.

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1300
This commit is contained in:
Aliaksandr Valialkin 2021-11-09 19:18:27 +02:00
parent e5ac9d8e57
commit e5d4c7f4a7
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
2 changed files with 40 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"os" "os"
"sync"
"time" "time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo" "github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
@ -108,7 +109,7 @@ func proxyRequest(w http.ResponseWriter, r *http.Request) {
// Forward other panics to the caller. // Forward other panics to the caller.
panic(err) panic(err)
}() }()
reverseProxy.ServeHTTP(w, r) getReverseProxy().ServeHTTP(w, r)
} }
var ( var (
@ -117,29 +118,42 @@ var (
missingRouteRequests = metrics.NewCounter(`vmauth_http_request_errors_total{reason="missing_route"}`) missingRouteRequests = metrics.NewCounter(`vmauth_http_request_errors_total{reason="missing_route"}`)
) )
var reverseProxy = &httputil.ReverseProxy{ var (
Director: func(r *http.Request) { reverseProxy *httputil.ReverseProxy
targetURL := r.Header.Get("vm-target-url") reverseProxyOnce sync.Once
target, err := url.Parse(targetURL) )
if err != nil {
logger.Panicf("BUG: unexpected error when parsing targetURL=%q: %s", targetURL, err) func getReverseProxy() *httputil.ReverseProxy {
} reverseProxyOnce.Do(initReverseProxy)
r.URL = target return reverseProxy
}, }
Transport: func() *http.Transport {
tr := http.DefaultTransport.(*http.Transport).Clone() // initReverseProxy must be called after flag.Parse(), since it uses command-line flags.
// Automatic compression must be disabled in order to fix https://github.com/VictoriaMetrics/VictoriaMetrics/issues/535 func initReverseProxy() {
tr.DisableCompression = true reverseProxy = &httputil.ReverseProxy{
// Disable HTTP/2.0, since VictoriaMetrics components don't support HTTP/2.0 (because there is no sense in this). Director: func(r *http.Request) {
tr.ForceAttemptHTTP2 = false targetURL := r.Header.Get("vm-target-url")
tr.MaxIdleConnsPerHost = *maxIdleConnsPerBackend target, err := url.Parse(targetURL)
if tr.MaxIdleConns != 0 && tr.MaxIdleConns < tr.MaxIdleConnsPerHost { if err != nil {
tr.MaxIdleConns = tr.MaxIdleConnsPerHost logger.Panicf("BUG: unexpected error when parsing targetURL=%q: %s", targetURL, err)
} }
return tr r.URL = target
}(), },
FlushInterval: time.Second, Transport: func() *http.Transport {
ErrorLog: logger.StdErrorLogger(), 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() { func usage() {

View File

@ -6,6 +6,8 @@ sort: 15
## tip ## 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) ## [v1.69.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.69.0)