Skip to content

Commit

Permalink
expose all the http config values
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed May 18, 2018
1 parent e4fb1bf commit c1dfc9f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ server.rsa.key
*.json
*.yml
*.toml
coverage.out
63 changes: 59 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,67 @@ type ServiceConfig struct {
// Extra configuration for customized behaviour
ExtraConfig ExtraConfig `mapstructure:"extra_config"`

ReadTimeout time.Duration `mapstructure:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
// ReadTimeout is the maximum duration for reading the entire
// request, including the body.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout time.Duration `mapstructure:"read_timeout"`
// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
WriteTimeout time.Duration `mapstructure:"write_timeout"`
// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled. If IdleTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, ReadHeaderTimeout is used.
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body.
ReadHeaderTimeout time.Duration `mapstructure:"read_header_timeout"`

MaxIdleConnsPerHost int `mapstructure:"max_idle_connections"`
// DisableKeepAlives, if true, prevents re-use of TCP connections
// between different HTTP requests.
DisableKeepAlives bool `mapstructure:"disable_keep_alives"`
// DisableCompression, if true, prevents the Transport from
// requesting compression with an "Accept-Encoding: gzip"
// request header when the Request contains no existing
// Accept-Encoding value. If the Transport requests gzip on
// its own and gets a gzipped response, it's transparently
// decoded in the Response.Body. However, if the user
// explicitly requested gzip it is not automatically
// uncompressed.
DisableCompression bool `mapstructure:"disable_compression"`
// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int `mapstructure:"max_idle_connections"`
// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int `mapstructure:"max_idle_connections_per_host"`
// IdleConnTimeout is the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing
// itself.
// Zero means no limit.
IdleConnTimeout time.Duration `mapstructure:"idle_connection_timeout"`
// ResponseHeaderTimeout, if non-zero, specifies the amount of
// time to wait for a server's response headers after fully
// writing the request (including its body, if any). This
// time does not include the time to read the response body.
ResponseHeaderTimeout time.Duration `mapstructure:"response_header_timeout"`
// ExpectContinueTimeout, if non-zero, specifies the amount of
// time to wait for a server's first response headers after fully
// writing the request headers if the request has an
// "Expect: 100-continue" header. Zero means no timeout and
// causes the body to be sent immediately, without
// waiting for the server to approve.
// This time does not include the time to send the request header.
ExpectContinueTimeout time.Duration `mapstructure:"expect_continue_timeout"`

// DisableStrictREST flags if the REST enforcement is disabled
DisableStrictREST bool `mapstructure:"disable_rest"`
Expand Down
68 changes: 40 additions & 28 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,50 @@ func (p parser) Parse(configFile string) (ServiceConfig, error) {
}

type parseableServiceConfig struct {
Endpoints []*parseableEndpointConfig `json:"endpoints"`
Timeout string `json:"timeout"`
CacheTTL string `json:"cache_ttl"`
Host []string `json:"host"`
Port int `json:"port"`
Version int `json:"version"`
ExtraConfig *ExtraConfig `json:"extra_config,omitempty"`
ReadTimeout string `json:"read_timeout"`
WriteTimeout string `json:"write_timeout"`
IdleTimeout string `json:"idle_timeout"`
ReadHeaderTimeout string `json:"read_header_timeout"`
MaxIdleConnsPerHost int `json:"max_idle_connections"`
OutputEncoding string `json:"output_encoding"`
Debug bool
Plugin *Plugin
Endpoints []*parseableEndpointConfig `json:"endpoints"`
Timeout string `json:"timeout"`
CacheTTL string `json:"cache_ttl"`
Host []string `json:"host"`
Port int `json:"port"`
Version int `json:"version"`
ExtraConfig *ExtraConfig `json:"extra_config,omitempty"`
ReadTimeout string `json:"read_timeout"`
WriteTimeout string `json:"write_timeout"`
IdleTimeout string `json:"idle_timeout"`
ReadHeaderTimeout string `json:"read_header_timeout"`
DisableKeepAlives bool `json:"disable_keep_alives"`
DisableCompression bool `json:"disable_compression"`
MaxIdleConns int `json:"max_idle_connections"`
MaxIdleConnsPerHost int `json:"max_idle_connections_per_host"`
IdleConnTimeout string `json:"idle_connection_timeout"`
ResponseHeaderTimeout string `json:"response_header_timeout"`
ExpectContinueTimeout string `json:"expect_continue_timeout"`
OutputEncoding string `json:"output_encoding"`
Debug bool
Plugin *Plugin
}

func (p *parseableServiceConfig) normalize() ServiceConfig {
cfg := ServiceConfig{
Timeout: parseDuration(p.Timeout),
CacheTTL: parseDuration(p.CacheTTL),
Host: p.Host,
Port: p.Port,
Version: p.Version,
Debug: p.Debug,
ReadTimeout: parseDuration(p.ReadTimeout),
WriteTimeout: parseDuration(p.WriteTimeout),
IdleTimeout: parseDuration(p.IdleTimeout),
ReadHeaderTimeout: parseDuration(p.ReadHeaderTimeout),
OutputEncoding: p.OutputEncoding,
MaxIdleConnsPerHost: p.MaxIdleConnsPerHost,
Plugin: p.Plugin,
Timeout: parseDuration(p.Timeout),
CacheTTL: parseDuration(p.CacheTTL),
Host: p.Host,
Port: p.Port,
Version: p.Version,
Debug: p.Debug,
ReadTimeout: parseDuration(p.ReadTimeout),
WriteTimeout: parseDuration(p.WriteTimeout),
IdleTimeout: parseDuration(p.IdleTimeout),
ReadHeaderTimeout: parseDuration(p.ReadHeaderTimeout),
DisableKeepAlives: p.DisableKeepAlives,
DisableCompression: p.DisableCompression,
MaxIdleConns: p.MaxIdleConns,
MaxIdleConnsPerHost: p.MaxIdleConnsPerHost,
IdleConnTimeout: parseDuration(p.IdleConnTimeout),
ResponseHeaderTimeout: parseDuration(p.ResponseHeaderTimeout),
ExpectContinueTimeout: parseDuration(p.ExpectContinueTimeout),
OutputEncoding: p.OutputEncoding,
Plugin: p.Plugin,
}
if p.ExtraConfig != nil {
cfg.ExtraConfig = *p.ExtraConfig
Expand Down
9 changes: 8 additions & 1 deletion router/gin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ func (r ginRouter) Run(cfg config.ServiceConfig) {
r.cfg.Logger.Debug("Debug enabled")
}

http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = cfg.MaxIdleConnsPerHost
transport := http.DefaultTransport.(*http.Transport)
transport.DisableCompression = cfg.DisableCompression
transport.DisableKeepAlives = cfg.DisableKeepAlives
transport.MaxIdleConns = cfg.MaxIdleConns
transport.MaxIdleConnsPerHost = cfg.MaxIdleConnsPerHost
transport.IdleConnTimeout = cfg.IdleConnTimeout
transport.ResponseHeaderTimeout = cfg.ResponseHeaderTimeout
transport.ExpectContinueTimeout = cfg.ExpectContinueTimeout

r.cfg.Engine.RedirectTrailingSlash = true
r.cfg.Engine.RedirectFixedPath = true
Expand Down
9 changes: 8 additions & 1 deletion router/mux/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ func (r httpRouter) Run(cfg config.ServiceConfig) {
r.cfg.Engine.Handle(r.cfg.DebugPattern, DebugHandler(r.cfg.Logger))
}

http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = cfg.MaxIdleConnsPerHost
transport := http.DefaultTransport.(*http.Transport)
transport.DisableCompression = cfg.DisableCompression
transport.DisableKeepAlives = cfg.DisableKeepAlives
transport.MaxIdleConns = cfg.MaxIdleConns
transport.MaxIdleConnsPerHost = cfg.MaxIdleConnsPerHost
transport.IdleConnTimeout = cfg.IdleConnTimeout
transport.ResponseHeaderTimeout = cfg.ResponseHeaderTimeout
transport.ExpectContinueTimeout = cfg.ExpectContinueTimeout

r.registerKrakendEndpoints(cfg.Endpoints)

Expand Down

0 comments on commit c1dfc9f

Please sign in to comment.