Skip to content

Commit

Permalink
LimitListener to limit max number of connections
Browse files Browse the repository at this point in the history
This also drops tcp keep-alive in ListenAndServe but it's no longer
necessary since we now close idle connections long before that.
  • Loading branch information
agaoglu committed Dec 6, 2016
1 parent 9986b28 commit e487477
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
10 changes: 9 additions & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -112,6 +113,7 @@ type Options struct {

ListenAddress string
ReadTimeout time.Duration
MaxConnections int
ExternalURL *url.URL
RoutePrefix string
MetricsPath string
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e487477

Please sign in to comment.