Set read-timeout for http.Server

This also specifies a timeout for idle client connections, which may
cause "too many open files" errors.
See #2238
This commit is contained in:
Erdem Agaoglu 2016-12-01 16:29:45 +03:00
parent 63fe65bf2f
commit 9986b28380
2 changed files with 9 additions and 3 deletions

View file

@ -80,6 +80,10 @@ func init() {
&cfg.web.ListenAddress, "web.listen-address", ":9090", &cfg.web.ListenAddress, "web.listen-address", ":9090",
"Address to listen on for the web interface, API, and telemetry.", "Address to listen on for the web interface, API, and telemetry.",
) )
cfg.fs.DurationVar(
&cfg.web.ReadTimeout, "web.read-timeout", 30*time.Second,
"Maximum duration before timing out read of the request, and closing idle connections.",
)
cfg.fs.StringVar( cfg.fs.StringVar(
&cfg.prometheusURL, "web.external-url", "", &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.", "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

@ -111,6 +111,7 @@ type Options struct {
Flags map[string]string Flags map[string]string
ListenAddress string ListenAddress string
ReadTimeout time.Duration
ExternalURL *url.URL ExternalURL *url.URL
RoutePrefix string RoutePrefix string
MetricsPath string MetricsPath string
@ -247,9 +248,10 @@ func (h *Handler) Reload() <-chan chan error {
func (h *Handler) Run() { func (h *Handler) Run() {
log.Infof("Listening on %s", h.options.ListenAddress) log.Infof("Listening on %s", h.options.ListenAddress)
server := &http.Server{ server := &http.Server{
Addr: h.options.ListenAddress, Addr: h.options.ListenAddress,
Handler: h.router, Handler: h.router,
ErrorLog: log.NewErrorLogger(), ErrorLog: log.NewErrorLogger(),
ReadTimeout: h.options.ReadTimeout,
} }
h.listenErrCh <- server.ListenAndServe() h.listenErrCh <- server.ListenAndServe()
} }