diff --git a/README.md b/README.md index 838b6f9..05446b8 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,14 @@ The following flags are available for configuring PlanB on start-up: be sent to stdout. Default value is ``./access.log``. - ``--request-timeout``: Total backend request timeout in seconds. Default value is ``30``. -- ``--dial-timeout``: Dial backend request timeout in seconds. Default value is - ``10``. +- ``--dial-timeout``: Dial backend request timeout in seconds. Default value is ``10``. +- ``--client-read-timeout``: Maximum duration for reading the entire request, including the body. Default + value is ``0``. +- ``--client-read-header-timeout``: Amount of time allowed to read request headers. Default value is ``0``. +- ``--client-write-timeout``: Maximum duration before timing out writes of the response. Default + value is ``0``. +- ``--client-idle-timeout``: Maximum amount of time to wait for the next request when keep-alives are enabled. Default value is + ``0``. - ``--dead-backend-time``: Time in seconds a backend will remain disabled after a network failure. Default value is ``30``. - ``--flush-interval``: Time in milliseconds to flush the proxied request. diff --git a/main.go b/main.go index f4d9387..450f1a8 100644 --- a/main.go +++ b/main.go @@ -112,11 +112,15 @@ func runServer(c *cli.Context) { log.Fatal(err) } err = rp.Initialize(reverseproxy.ReverseProxyConfig{ - Router: &r, - RequestIDHeader: c.String("request-id-header"), - FlushInterval: time.Duration(c.Int("flush-interval")) * time.Millisecond, - DialTimeout: time.Duration(c.Int("dial-timeout")) * time.Second, - RequestTimeout: time.Duration(c.Int("request-timeout")) * time.Second, + Router: &r, + RequestIDHeader: c.String("request-id-header"), + FlushInterval: time.Duration(c.Int("flush-interval")) * time.Millisecond, + DialTimeout: time.Duration(c.Int("dial-timeout")) * time.Second, + RequestTimeout: time.Duration(c.Int("request-timeout")) * time.Second, + ReadTimeout: c.Duration("client-read-timeout"), + ReadHeaderTimeout: c.Duration("client-read-header-timeout"), + WriteTimeout: c.Duration("client-write-timeout"), + IdleTimeout: c.Duration("client-idle-timeout"), }) if err != nil { log.Fatal(err) @@ -263,6 +267,46 @@ The value 'none' can be used to disable access logs.`), Value: 10, Usage: "Dial backend request timeout in seconds", }, + cli.DurationFlag{ + Name: "client-read-timeout", + Value: 0, + Usage: "Maximum duration for reading the entire request, including the body", + }, + cli.DurationFlag{ + Name: "client-read-header-timeout", + Value: 0, + Usage: "Amount of time allowed to read request headers", + }, + cli.DurationFlag{ + Name: "client-write-timeout", + Value: 0, + Usage: "Maximum duration before timing out writes of the response", + }, + cli.DurationFlag{ + Name: "client-idle-timeout", + Value: 0, + Usage: "Maximum amount of time to wait for the next request when keep-alives are enabled", + }, + cli.IntFlag{ + Name: "client-read-timeout", + Value: 0, + Usage: "Maximum duration for reading the entire request, including the body", + }, + cli.IntFlag{ + Name: "client-read-header-timeout", + Value: 0, + Usage: "Amount of time allowed to read request headers", + }, + cli.IntFlag{ + Name: "client-write-timeout", + Value: 0, + Usage: "Maximum duration before timing out writes of the response", + }, + cli.IntFlag{ + Name: "client-idle-timeout", + Value: 0, + Usage: "Maximum amount of time to wait for the next request when keep-alives are enabled", + }, cli.IntFlag{ Name: "dead-backend-time", Value: 30, diff --git a/reverseproxy/native.go b/reverseproxy/native.go index 9ca8251..255692d 100644 --- a/reverseproxy/native.go +++ b/reverseproxy/native.go @@ -128,7 +128,11 @@ func (rp *NativeReverseProxy) Initialize(rpConfig ReverseProxyConfig) error { func (rp *NativeReverseProxy) Listen(listener net.Listener) { server := manners.NewWithServer(&http.Server{ - Handler: rp, + ReadTimeout: rp.ReadTimeout, + ReadHeaderTimeout: rp.ReadHeaderTimeout, + WriteTimeout: rp.WriteTimeout, + IdleTimeout: rp.IdleTimeout, + Handler: rp, ConnState: func(c net.Conn, s http.ConnState) { switch s { case http.StateNew: diff --git a/reverseproxy/reverseproxy.go b/reverseproxy/reverseproxy.go index c980406..3d346ed 100644 --- a/reverseproxy/reverseproxy.go +++ b/reverseproxy/reverseproxy.go @@ -34,11 +34,15 @@ type ReverseProxy interface { } type ReverseProxyConfig struct { - Router Router - FlushInterval time.Duration - DialTimeout time.Duration - RequestTimeout time.Duration - RequestIDHeader string + Router Router + FlushInterval time.Duration + DialTimeout time.Duration + RequestTimeout time.Duration + ReadTimeout time.Duration + ReadHeaderTimeout time.Duration + WriteTimeout time.Duration + IdleTimeout time.Duration + RequestIDHeader string } type RequestData struct {