lib/httpserver: add -http.connTimeout command-line flag for limiting the lifetime for incoming http connections

This can be useful for balancing incoming connections among multiple services.
This commit is contained in:
Aliaksandr Valialkin 2020-09-03 22:22:26 +03:00
parent e1c2757f70
commit d387da142e

View file

@ -18,6 +18,7 @@ import (
"sync/atomic"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
"github.com/VictoriaMetrics/metrics"
@ -43,6 +44,8 @@ var (
shutdownDelay = flag.Duration("http.shutdownDelay", 0, "Optional delay before http server shutdown. During this dealy the servier returns non-OK responses "+
"from /health page, so load balancers can route new requests to other servers")
idleConnTimeout = flag.Duration("http.idleConnTimeout", time.Minute, "Timeout for incoming idle http connections")
connTimeout = flag.Duration("http.connTimeout", 2*time.Minute, "Incoming http connections are closed after the configured timeout. This may help spreading incoming load "+
"among a cluster of services behind load balancer")
)
var (
@ -111,6 +114,11 @@ func serveWithListener(addr string, ln net.Listener, rh RequestHandler) {
// since these timeouts must be controlled by request handlers.
ErrorLog: logger.StdErrorLogger(),
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
startTime := fasttime.UnixTimestamp()
return context.WithValue(ctx, connStartTimeKey, &startTime)
},
}
serversLock.Lock()
servers[addr] = &s
@ -124,6 +132,15 @@ func serveWithListener(addr string, ln net.Listener, rh RequestHandler) {
}
}
func whetherToCloseConn(r *http.Request) bool {
ctx := r.Context()
v := ctx.Value(connStartTimeKey)
st, ok := v.(*uint64)
return ok && fasttime.UnixTimestamp()-*st > uint64(*connTimeout/time.Second)
}
var connStartTimeKey = interface{}("startTime")
// Stop stops the http server on the given addr, which has been started
// via Serve func.
func Stop(addr string) error {
@ -168,9 +185,14 @@ func gzipHandler(s *server, rh RequestHandler) http.HandlerFunc {
}
var metricsHandlerDuration = metrics.NewHistogram(`vm_http_request_duration_seconds{path="/metrics"}`)
var connTimeoutClosedConns = metrics.NewCounter(`vm_http_conn_timeout_closed_conns_total`)
func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh RequestHandler) {
requestsTotal.Inc()
if whetherToCloseConn(r) {
connTimeoutClosedConns.Inc()
w.Header().Set("Connection", "close")
}
path, err := getCanonicalPath(r.URL.Path)
if err != nil {
Errorf(w, r, "cannot get canonical path: %s", err)