Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from klebervirgilio/configurable-timeouts
Browse files Browse the repository at this point in the history
#18 Add flags to configure timeouts
  • Loading branch information
cezarsa authored May 19, 2017
2 parents 847b83a + 2d34d53 commit 1f9996a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 49 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -272,6 +276,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,
Expand Down
6 changes: 5 additions & 1 deletion reverseproxy/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 9 additions & 5 deletions reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1f9996a

Please sign in to comment.