diff --git a/dist/helm/chihaya/values.yaml b/dist/helm/chihaya/values.yaml index c3c42ff..4f90d39 100644 --- a/dist/helm/chihaya/values.yaml +++ b/dist/helm/chihaya/values.yaml @@ -53,6 +53,12 @@ config: # Disabling this should increase performance/decrease load. enable_request_timing: false + # When true, persistent connections will be allowed. Generally this is not + # useful for a public tracker, but helps performance in some cases (use of + # a reverse proxy, or when there are few clients issuing many requests). + enable_keepalive: false + idle_timeout: 30s + # Whether to listen on /announce.php and /scrape.php in addition to their # non-.php counterparts. # This is an option for compatibility with (very) old clients or otherwise diff --git a/example_config.yaml b/example_config.yaml index 5212681..2fe4a6b 100644 --- a/example_config.yaml +++ b/example_config.yaml @@ -36,6 +36,12 @@ chihaya: read_timeout: 5s write_timeout: 5s + # When true, persistent connections will be allowed. Generally this is not + # useful for a public tracker, but helps performance in some cases (use of + # a reverse proxy, or when there are few clients issuing many requests). + enable_keepalive: false + idle_timeout: 30s + # Whether to time requests. # Disabling this should increase performance/decrease load. enable_request_timing: false diff --git a/frontend/http/frontend.go b/frontend/http/frontend.go index ffd5ea6..6e252d9 100644 --- a/frontend/http/frontend.go +++ b/frontend/http/frontend.go @@ -22,6 +22,8 @@ type Config struct { Addr string `yaml:"addr"` ReadTimeout time.Duration `yaml:"read_timeout"` WriteTimeout time.Duration `yaml:"write_timeout"` + IdleTimeout time.Duration `yaml:"idle_timeout"` + EnableKeepAlive bool `yaml:"enable_keepalive"` TLSCertPath string `yaml:"tls_cert_path"` TLSKeyPath string `yaml:"tls_key_path"` EnableLegacyPHPURLs bool `yaml:"enable_legacy_php_urls"` @@ -35,6 +37,8 @@ func (cfg Config) LogFields() log.Fields { "addr": cfg.Addr, "readTimeout": cfg.ReadTimeout, "writeTimeout": cfg.WriteTimeout, + "idleTimeout": cfg.IdleTimeout, + "enableKeepAlive": cfg.EnableKeepAlive, "tlsCertPath": cfg.TLSCertPath, "tlsKeyPath": cfg.TLSKeyPath, "enableLegacyPHPURLs": cfg.EnableLegacyPHPURLs, @@ -51,6 +55,7 @@ func (cfg Config) LogFields() log.Fields { const ( defaultReadTimeout = 2 * time.Second defaultWriteTimeout = 2 * time.Second + defaultIdleTimeout = 30 * time.Second ) // Validate sanity checks values set in a config and returns a new config with @@ -78,6 +83,19 @@ func (cfg Config) Validate() Config { }) } + if cfg.IdleTimeout <= 0 { + validcfg.IdleTimeout = defaultIdleTimeout + + if cfg.EnableKeepAlive { + // If keepalive is disabled, this configuration isn't used anyway. + log.Warn("falling back to default configuration", log.Fields{ + "name": "http.IdleTimeout", + "provided": cfg.IdleTimeout, + "default": validcfg.IdleTimeout, + }) + } + } + return validcfg } @@ -158,10 +176,10 @@ func (f *Frontend) listenAndServe() error { Handler: f.handler(), ReadTimeout: f.ReadTimeout, WriteTimeout: f.WriteTimeout, + IdleTimeout: f.IdleTimeout, } - // Disable KeepAlives. - f.srv.SetKeepAlivesEnabled(false) + f.srv.SetKeepAlivesEnabled(f.EnableKeepAlive) // Start the HTTP server. if f.tlsCfg != nil {