LimitListener to limit max number of connections

This also drops tcp keep-alive in ListenAndServe but it's no longer
necessary since we now close idle connections long before that.
This commit is contained in:
Erdem Agaoglu 2016-12-06 12:45:59 +03:00
parent 9986b28380
commit e487477a17
2 changed files with 13 additions and 1 deletions

View File

@ -84,6 +84,10 @@ func init() {
&cfg.web.ReadTimeout, "web.read-timeout", 30*time.Second,
"Maximum duration before timing out read of the request, and closing idle connections.",
)
cfg.fs.IntVar(
&cfg.web.MaxConnections, "web.max-connections", 100,
"Maximum number of simultaneous connections.",
)
cfg.fs.StringVar(
&cfg.prometheusURL, "web.external-url", "",
"The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Prometheus. If omitted, relevant URL components will be derived automatically.",

View File

@ -37,6 +37,7 @@ import (
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"
"golang.org/x/net/context"
"golang.org/x/net/netutil"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/notifier"
@ -112,6 +113,7 @@ type Options struct {
ListenAddress string
ReadTimeout time.Duration
MaxConnections int
ExternalURL *url.URL
RoutePrefix string
MetricsPath string
@ -253,7 +255,13 @@ func (h *Handler) Run() {
ErrorLog: log.NewErrorLogger(),
ReadTimeout: h.options.ReadTimeout,
}
h.listenErrCh <- server.ListenAndServe()
listener, err := net.Listen("tcp", h.options.ListenAddress)
if err != nil {
h.listenErrCh <- err
} else {
limitedListener := netutil.LimitListener(listener, h.options.MaxConnections)
h.listenErrCh <- server.Serve(limitedListener)
}
}
func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) {