Limit number of concurrent connections

Go's net/http library has no limits in place for number of concurrent
requests currently being processed.  This can result in an enormous
number of goroutines being created and the read/write buffer pools
growing unbounded resulting in OOM situations.
This commit is contained in:
Paul Saab 2014-09-25 10:58:07 -07:00
parent 2b8dec07fe
commit 3f5b6b55b0
2 changed files with 5 additions and 0 deletions

View file

@ -82,6 +82,7 @@ type Config struct {
RequestTimeout Duration `json:"request_timeout"` RequestTimeout Duration `json:"request_timeout"`
HttpReadTimeout Duration `json:"http_read_timeout"` HttpReadTimeout Duration `json:"http_read_timeout"`
HttpWriteTimeout Duration `json:"http_write_timeout"` HttpWriteTimeout Duration `json:"http_write_timeout"`
HttpListenLimit int `json:"http_listen_limit"`
NumWantFallback int `json:"default_num_want"` NumWantFallback int `json:"default_num_want"`
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"` ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`

View file

@ -117,10 +117,14 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) {
} }
glog.V(0).Info("Starting on ", cfg.Addr) glog.V(0).Info("Starting on ", cfg.Addr)
if cfg.HttpListenLimit != 0 {
glog.V(0).Info("Limiting connections to ", cfg.HttpListenLimit)
}
grace := &graceful.Server{ grace := &graceful.Server{
Timeout: cfg.RequestTimeout.Duration, Timeout: cfg.RequestTimeout.Duration,
ConnState: srv.connState, ConnState: srv.connState,
ListenLimit: cfg.HttpListenLimit,
Server: &http.Server{ Server: &http.Server{
Addr: cfg.Addr, Addr: cfg.Addr,
Handler: newRouter(srv), Handler: newRouter(srv),