Skip to content

Commit

Permalink
Add config to enable keepalive/persistent connections
Browse files Browse the repository at this point in the history
  • Loading branch information
pushrax committed Sep 9, 2018
1 parent 21f500c commit 20edf7a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions dist/helm/chihaya/values.yaml
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions example_config.yaml
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions frontend/http/frontend.go
Expand Up @@ -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"`
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 20edf7a

Please sign in to comment.