Skip to content

Commit

Permalink
reverseproxy: Fix overwriting of max_idle_conns_per_host (closes #4201)
Browse files Browse the repository at this point in the history
Also split the Caddyfile subdirective keepalive_idle_conns into two properties so the conns and conns_per_host can be set separately.

This is technically a breaking change, but probably anyone who this breaks already had a broken config anyway, and silently fixing it won't help them fix their configs.
  • Loading branch information
mholt committed Jun 15, 2021
1 parent 6d25261 commit 7c68809
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
22 changes: 12 additions & 10 deletions modules/caddyhttp/reverseproxy/caddyfile.go
Expand Up @@ -978,6 +978,18 @@ func (h *HTTPTransport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
h.KeepAlive = new(KeepAlive)
}
h.KeepAlive.MaxIdleConns = num

case "keepalive_idle_conns_per_host":
if !d.NextArg() {
return d.ArgErr()
}
num, err := strconv.Atoi(d.Val())
if err != nil {
return d.Errf("bad integer value '%s': %v", d.Val(), err)
}
if h.KeepAlive == nil {
h.KeepAlive = new(KeepAlive)
}
h.KeepAlive.MaxIdleConnsPerHost = num

case "versions":
Expand All @@ -1004,16 +1016,6 @@ func (h *HTTPTransport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}
h.MaxConnsPerHost = num

case "max_idle_conns_per_host":
if !d.NextArg() {
return d.ArgErr()
}
num, err := strconv.Atoi(d.Val())
if err != nil {
return d.Errf("bad integer value '%s': %v", d.Val(), err)
}
h.MaxIdleConnsPerHost = num

default:
return d.Errf("unrecognized subdirective %s", d.Val())
}
Expand Down
10 changes: 3 additions & 7 deletions modules/caddyhttp/reverseproxy/httptransport.go
Expand Up @@ -62,9 +62,6 @@ type HTTPTransport struct {
// Maximum number of connections per host. Default: 0 (no limit)
MaxConnsPerHost int `json:"max_conns_per_host,omitempty"`

// Maximum number of idle connections per host. Default: 0 (uses Go's default of 2)
MaxIdleConnsPerHost int `json:"max_idle_conns_per_host,omitempty"`

// How long to wait before timing out trying to connect to
// an upstream.
DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
Expand Down Expand Up @@ -197,7 +194,6 @@ func (h *HTTPTransport) NewTransport(ctx caddy.Context) (*http.Transport, error)
return conn, nil
},
MaxConnsPerHost: h.MaxConnsPerHost,
MaxIdleConnsPerHost: h.MaxIdleConnsPerHost,
ResponseHeaderTimeout: time.Duration(h.ResponseHeaderTimeout),
ExpectContinueTimeout: time.Duration(h.ExpectContinueTimeout),
MaxResponseHeaderBytes: h.MaxResponseHeaderSize,
Expand Down Expand Up @@ -412,13 +408,13 @@ type KeepAlive struct {
// How often to probe for liveness.
ProbeInterval caddy.Duration `json:"probe_interval,omitempty"`

// Maximum number of idle connections.
// Maximum number of idle connections. Default: 0, which means no limit.
MaxIdleConns int `json:"max_idle_conns,omitempty"`

// Maximum number of idle connections per upstream host.
// Maximum number of idle connections per host. Default: 0, which uses Go's default of 2.

This comment has been minimized.

Copy link
@akosyakov

akosyakov Jun 16, 2021

Default: 32?

This comment has been minimized.

Copy link
@mholt

mholt Jun 16, 2021

Author Member

Yes, thank you. Forgot we changed that.

This comment has been minimized.

Copy link
@akosyakov

akosyakov Jun 18, 2021

that's actually a bit confusing, as far as I understood if you don't specify http transport then default is 32, but otherwise it is 0

This comment has been minimized.

Copy link
@francislavoie

francislavoie Jun 18, 2021

Member

In Go, the default value for an int is 0, so we use that as a marker for the default value. For things that can be "turned off" then -1 is typically what's passed.

FWIW this comment was fixed in 238914d#diff-8839c76ab9a0e424d77cabc5357fbcdfb166d6b6cc7a076114fb24a5b16814b8

MaxIdleConnsPerHost int `json:"max_idle_conns_per_host,omitempty"`

// How long connections should be kept alive when idle.
// How long connections should be kept alive when idle. Default: 0, which means no timeout.
IdleConnTimeout caddy.Duration `json:"idle_timeout,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/reverseproxy/reverseproxy.go
Expand Up @@ -204,7 +204,7 @@ func (h *Handler) Provision(ctx caddy.Context) error {
KeepAlive: &KeepAlive{
ProbeInterval: caddy.Duration(30 * time.Second),
IdleConnTimeout: caddy.Duration(2 * time.Minute),
MaxIdleConnsPerHost: 32,
MaxIdleConnsPerHost: 32, // seems about optimal, see #2805
},
DialTimeout: caddy.Duration(10 * time.Second),
}
Expand Down

0 comments on commit 7c68809

Please sign in to comment.