mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-20 15:16:42 +00:00
lib/httpserver: add -http.disableResponseCompression
flag, which may help saving CPU resources at the cost of higher network bandwidth usage
This commit is contained in:
parent
8e3eb5b39d
commit
a26e774eca
1 changed files with 8 additions and 0 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
@ -22,6 +23,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth")
|
||||||
|
|
||||||
servers = make(map[string]*http.Server)
|
servers = make(map[string]*http.Server)
|
||||||
serversLock sync.Mutex
|
serversLock sync.Mutex
|
||||||
)
|
)
|
||||||
|
@ -39,6 +42,8 @@ type RequestHandler func(w http.ResponseWriter, r *http.Request) bool
|
||||||
// By default all the responses are transparently compressed, since Google
|
// By default all the responses are transparently compressed, since Google
|
||||||
// charges a lot for the egress traffic. The compression may be disabled
|
// charges a lot for the egress traffic. The compression may be disabled
|
||||||
// by calling DisableResponseCompression before writing the first byte to w.
|
// by calling DisableResponseCompression before writing the first byte to w.
|
||||||
|
//
|
||||||
|
// The compression is also disabled if -http.disableResponseCompression flag is set.
|
||||||
func Serve(addr string, rh RequestHandler) {
|
func Serve(addr string, rh RequestHandler) {
|
||||||
logger.Infof("starting http server at http://%s/", addr)
|
logger.Infof("starting http server at http://%s/", addr)
|
||||||
logger.Infof("pprof handlers are exposed at http://%s/debug/pprof/", addr)
|
logger.Infof("pprof handlers are exposed at http://%s/debug/pprof/", addr)
|
||||||
|
@ -157,6 +162,9 @@ func handlerWrapper(w http.ResponseWriter, r *http.Request, rh RequestHandler) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func maybeGzipResponseWriter(w http.ResponseWriter, r *http.Request) http.ResponseWriter {
|
func maybeGzipResponseWriter(w http.ResponseWriter, r *http.Request) http.ResponseWriter {
|
||||||
|
if *disableResponseCompression {
|
||||||
|
return w
|
||||||
|
}
|
||||||
ae := r.Header.Get("Accept-Encoding")
|
ae := r.Header.Get("Accept-Encoding")
|
||||||
if ae == "" {
|
if ae == "" {
|
||||||
return w
|
return w
|
||||||
|
|
Loading…
Reference in a new issue