httpserver: add http2 option

Currently, the `httpserver` disabled HTTP/2 support by design, because:
```
// Disable http/2, since it doesn't give any advantages for VictoriaMetrics services.
```

As VictoriaLogs and VictoriaTraces rely on `httpserver`, in order to
support gRPC over HTTP/2, an option to support HTTP/2 is required.


Related PR https://github.com/VictoriaMetrics/VictoriaMetrics/pull/9881
This commit is contained in:
Zhu Jiekun
2025-10-24 16:30:27 +08:00
committed by GitHub
parent 11f488d8ff
commit d6bbfaf164

View File

@@ -98,6 +98,8 @@ type ServeOptions struct {
//
// Mostly required by http proxy servers, which performs own authorization and requests routing
DisableBuiltinRoutes bool
// EnableHTTP2 enable HTTP/2 support for the given server.
EnableHTTP2 bool
}
// Serve starts an http server on the given addrs with the given optional rh.
@@ -147,16 +149,30 @@ func serve(addr string, rh RequestHandler, idx int, opts ServeOptions) {
logger.Infof("pprof handlers are exposed at %s://%s/debug/pprof/", scheme, ln.Addr())
}
serveWithListener(addr, ln, rh, opts.DisableBuiltinRoutes)
serveWithListener(addr, ln, rh, opts.DisableBuiltinRoutes, opts.EnableHTTP2)
}
func serveWithListener(addr string, ln net.Listener, rh RequestHandler, disableBuiltinRoutes bool) {
func serveWithListener(addr string, ln net.Listener, rh RequestHandler, disableBuiltinRoutes bool, enableHTTP2 bool) {
var s server
s.s = &http.Server{
// Disable HTTP/2 by default, since it doesn't give any advantages for VictoriaMetrics services.
// But for external projects that import `httpserver` package,
// the `enableHTTP2` arg provides the flexibility to use HTTP/2.
var (
protocols *http.Protocols
tlsNextProto map[string]func(*http.Server, *tls.Conn, http.Handler)
)
if enableHTTP2 {
protocols = &http.Protocols{}
protocols.SetHTTP2(true)
protocols.SetUnencryptedHTTP2(true)
} else {
tlsNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
}
// Disable http/2, since it doesn't give any advantages for VictoriaMetrics services.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
s.s = &http.Server{
Protocols: protocols,
TLSNextProto: tlsNextProto,
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: *idleConnTimeout,