@marcel91200 / caddy Mirror · read-only / commits / 9ee68c1b
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 01:30 UTC

reverseproxy: Adjust defaults, document defaults (#4436)

* reverseproxy: Adjust defaults, document defaults Related to some of the issues in https://github.com/caddyserver/caddy/issues/4245, a complaint about the proxy transport defaults not being properly documented in https://caddy.community/t/default-values-for-directives/14254/6. - Dug into the stdlib to find the actual defaults for some of the timeouts and buffer limits, documenting them in godoc so the JSON docs get them next release. - Moved the keep-alive and dial-timeout defaults from `reverseproxy.go` to `httptransport.go`. It doesn't make sense to set defaults in the proxy, because then any time the transport is configured with non-defaults, the keep-alive and dial-timeout defaults are lost! - Sped up the dial timeout from 10s to 3s, in practice it rarely makes sense to wait a whole 10s for dialing. A shorter timeout helps a lot with the load balancer retries, so using something lower helps with user experience. * reverseproxy: Make keepalive interval configurable via Caddyfile * fastcgi: DialTimeout default for fastcgi transport too

Francis Lavoie committed Nov 24, 2021 at 01:32 UTC 9ee68c1bd57d72e8a969f1da492bd51bfa5ed9a0
5 files changed +54 -20
caddytest/integration/caddyfile_adapt/reverse_proxy_options.txt
+3 -1
@@ -22,6 +22,7 @@ https://example.com {
22 compression off
23 max_conns_per_host 5
24 keepalive_idle_conns_per_host 2
25 + keepalive_interval 30s
26 }
27 }
28 }
@@ -80,7 +81,8 @@ https://example.com {
81 "dial_timeout": 3000000000,
82 "expect_continue_timeout": 9000000000,
83 "keep_alive": {
83 - "max_idle_conns_per_host": 2
84 + "max_idle_conns_per_host": 2,
85 + "probe_interval": 30000000000
86 },
87 "max_conns_per_host": 5,
88 "max_response_header_size": 30000000,
modules/caddyhttp/reverseproxy/caddyfile.go
+15
@@ -806,7 +806,9 @@ func (h *Handler) FinalizeUnmarshalCaddyfile(helper httpcaddyfile.Helper) error
806 // tls_trusted_ca_certs <cert_files...>
807 // tls_server_name <sni>
808 // keepalive [off|<duration>]
809 +// keepalive_interval <interval>
810 // keepalive_idle_conns <max_count>
811 +// keepalive_idle_conns_per_host <count>
812 // versions <versions...>
813 // compression off
814 // max_conns_per_host <count>
@@ -966,6 +968,19 @@ func (h *HTTPTransport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
968 }
969 h.KeepAlive.IdleConnTimeout = caddy.Duration(dur)
970
971 + case "keepalive_interval":
972 + if !d.NextArg() {
973 + return d.ArgErr()
974 + }
975 + dur, err := caddy.ParseDuration(d.Val())
976 + if err != nil {
977 + return d.Errf("bad interval value '%s': %v", d.Val(), err)
978 + }
979 + if h.KeepAlive == nil {
980 + h.KeepAlive = new(KeepAlive)
981 + }
982 + h.KeepAlive.ProbeInterval = caddy.Duration(dur)
983 +
984 case "keepalive_idle_conns":
985 if !d.NextArg() {
986 return d.ArgErr()
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+10 -1
@@ -64,7 +64,7 @@ type Transport struct {
64 // Extra environment variables.
65 EnvVars map[string]string `json:"env,omitempty"`
66
67 - // The duration used to set a deadline when connecting to an upstream.
67 + // The duration used to set a deadline when connecting to an upstream. Default: `3s`.
68 DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
69
70 // The duration used to set a deadline when reading from the FastCGI server.
@@ -88,13 +88,22 @@ func (Transport) CaddyModule() caddy.ModuleInfo {
88 // Provision sets up t.
89 func (t *Transport) Provision(ctx caddy.Context) error {
90 t.logger = ctx.Logger(t)
91 +
92 if t.Root == "" {
93 t.Root = "{http.vars.root}"
94 }
95 +
96 t.serverSoftware = "Caddy"
97 if mod := caddy.GoModule(); mod.Version != "" {
98 t.serverSoftware += "/" + mod.Version
99 }
100 +
101 + // Set a relatively short default dial timeout.
102 + // This is helpful to make load-balancer retries more speedy.
103 + if t.DialTimeout == 0 {
104 + t.DialTimeout = caddy.Duration(3 * time.Second)
105 + }
106 +
107 return nil
108 }
109
modules/caddyhttp/reverseproxy/httptransport.go
+25 -10
@@ -63,28 +63,28 @@ type HTTPTransport struct {
63 MaxConnsPerHost int `json:"max_conns_per_host,omitempty"`
64
65 // How long to wait before timing out trying to connect to
66 - // an upstream.
66 + // an upstream. Default: `3s`.
67 DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
68
69 // How long to wait before spawning an RFC 6555 Fast Fallback
70 - // connection. A negative value disables this.
70 + // connection. A negative value disables this. Default: `300ms`.
71 FallbackDelay caddy.Duration `json:"dial_fallback_delay,omitempty"`
72
73 - // How long to wait for reading response headers from server.
73 + // How long to wait for reading response headers from server. Default: No timeout.
74 ResponseHeaderTimeout caddy.Duration `json:"response_header_timeout,omitempty"`
75
76 // The length of time to wait for a server's first response
77 // headers after fully writing the request headers if the
78 - // request has a header "Expect: 100-continue".
78 + // request has a header "Expect: 100-continue". Default: No timeout.
79 ExpectContinueTimeout caddy.Duration `json:"expect_continue_timeout,omitempty"`
80
81 - // The maximum bytes to read from response headers.
81 + // The maximum bytes to read from response headers. Default: `10MiB`.
82 MaxResponseHeaderSize int64 `json:"max_response_header_size,omitempty"`
83
84 - // The size of the write buffer in bytes.
84 + // The size of the write buffer in bytes. Default: `4KiB`.
85 WriteBufferSize int `json:"write_buffer_size,omitempty"`
86
87 - // The size of the read buffer in bytes.
87 + // The size of the read buffer in bytes. Default: `4KiB`.
88 ReadBufferSize int `json:"read_buffer_size,omitempty"`
89
90 // The versions of HTTP to support. As a special case, "h2c"
@@ -147,6 +147,21 @@ func (h *HTTPTransport) Provision(ctx caddy.Context) error {
147
148 // NewTransport builds a standard-lib-compatible http.Transport value from h.
149 func (h *HTTPTransport) NewTransport(ctx caddy.Context) (*http.Transport, error) {
150 + // Set keep-alive defaults if it wasn't otherwise configured
151 + if h.KeepAlive == nil {
152 + h.KeepAlive = &KeepAlive{
153 + ProbeInterval: caddy.Duration(30 * time.Second),
154 + IdleConnTimeout: caddy.Duration(2 * time.Minute),
155 + MaxIdleConnsPerHost: 32, // seems about optimal, see #2805
156 + }
157 + }
158 +
159 + // Set a relatively short default dial timeout.
160 + // This is helpful to make load-balancer retries more speedy.
161 + if h.DialTimeout == 0 {
162 + h.DialTimeout = caddy.Duration(3 * time.Second)
163 + }
164 +
165 dialer := &net.Dialer{
166 Timeout: time.Duration(h.DialTimeout),
167 FallbackDelay: time.Duration(h.FallbackDelay),
@@ -303,7 +318,7 @@ type TLSConfig struct {
318 // option except in testing or local development environments.
319 InsecureSkipVerify bool `json:"insecure_skip_verify,omitempty"`
320
306 - // The duration to allow a TLS handshake to a server.
321 + // The duration to allow a TLS handshake to a server. Default: No timeout.
322 HandshakeTimeout caddy.Duration `json:"handshake_timeout,omitempty"`
323
324 // The server name (SNI) to use in TLS handshakes.
@@ -405,7 +420,7 @@ type KeepAlive struct {
420 // Whether HTTP Keep-Alive is enabled. Default: true
421 Enabled *bool `json:"enabled,omitempty"`
422
408 - // How often to probe for liveness.
423 + // How often to probe for liveness. Default: `30s`.
424 ProbeInterval caddy.Duration `json:"probe_interval,omitempty"`
425
426 // Maximum number of idle connections. Default: 0, which means no limit.
@@ -414,7 +429,7 @@ type KeepAlive struct {
429 // Maximum number of idle connections per host. Default: 32.
430 MaxIdleConnsPerHost int `json:"max_idle_conns_per_host,omitempty"`
431
417 - // How long connections should be kept alive when idle. Default: 0, which means no timeout.
432 + // How long connections should be kept alive when idle. Default: `2m`.
433 IdleConnTimeout caddy.Duration `json:"idle_timeout,omitempty"`
434 }
435
modules/caddyhttp/reverseproxy/reverseproxy.go
+1 -8
@@ -204,14 +204,7 @@ func (h *Handler) Provision(ctx caddy.Context) error {
204
205 // set up transport
206 if h.Transport == nil {
207 - t := &HTTPTransport{
208 - KeepAlive: &KeepAlive{
209 - ProbeInterval: caddy.Duration(30 * time.Second),
210 - IdleConnTimeout: caddy.Duration(2 * time.Minute),
211 - MaxIdleConnsPerHost: 32, // seems about optimal, see #2805
212 - },
213 - DialTimeout: caddy.Duration(10 * time.Second),
214 - }
207 + t := &HTTPTransport{}
208 err := t.Provision(ctx)
209 if err != nil {
210 return fmt.Errorf("provisioning default transport: %v", err)