From 083c97a9c407d4235d784c9925ce5f027ee027f4 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Fri, 31 Jul 2015 22:39:01 +0300 Subject: [PATCH 1/2] Option to configure timeout --- http_client.go | 7 ++++++- output_http.go | 2 ++ settings.go | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/http_client.go b/http_client.go index ea898115..f9ab412a 100644 --- a/http_client.go +++ b/http_client.go @@ -21,6 +21,7 @@ type HTTPClientConfig struct { FollowRedirects int Debug bool OriginalHost bool + Timeout time.Duration } type HTTPClient struct { @@ -43,6 +44,10 @@ func NewHTTPClient(baseURL string, config *HTTPClientConfig) *HTTPClient { u.Host += ":" + defaultPorts[u.Scheme] } + if config.Timeout.Nanoseconds() == 0 { + config.Timeout = 5 * time.Second + } + client := new(HTTPClient) client.baseURL = u.String() client.host = u.Host @@ -112,7 +117,7 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) { } } - timeout := time.Now().Add(5 * time.Second) + timeout := time.Now().Add(c.config.Timeout) c.conn.SetWriteDeadline(timeout) diff --git a/output_http.go b/output_http.go index 907fc285..2908b2eb 100644 --- a/output_http.go +++ b/output_http.go @@ -18,6 +18,7 @@ type HTTPOutputConfig struct { elasticSearch string + Timeout time.Duration OriginalHost bool Debug bool @@ -97,6 +98,7 @@ func (o *HTTPOutput) startWorker() { FollowRedirects: o.config.redirectLimit, Debug: o.config.Debug, OriginalHost: o.config.OriginalHost, + Timeout: o.config.Timeout, }) deathCount := 0 diff --git a/settings.go b/settings.go index 8bcc7c9b..dd2a0002 100644 --- a/settings.go +++ b/settings.go @@ -87,6 +87,7 @@ func init() { flag.Var(&Settings.outputHTTP, "output-http", "Forwards incoming requests to given http address.\n\t# Redirect all incoming requests to staging.com address \n\tgor --input-raw :80 --output-http http://staging.com") flag.IntVar(&Settings.outputHTTPConfig.workers, "output-http-workers", 0, "Gor uses dynamic worker scaling by default. Enter a number to run a set number of workers.") flag.IntVar(&Settings.outputHTTPConfig.redirectLimit, "output-http-redirects", 0, "Enable how often redirects should be followed.") + flag.DurationVar(&Settings.outputHTTPConfig.Timeout, "output-http-timeout", 0, "Specify HTTP request/response timeout. By default 5s. Example: --output-http-timeout 30s") flag.BoolVar(&Settings.outputHTTPConfig.stats, "output-http-stats", false, "Report http output queue stats to console every 5 seconds.") From 587b9ad2d55ed88a0eeb4f09ba992e313a7aacc3 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Fri, 31 Jul 2015 22:41:32 +0300 Subject: [PATCH 2/2] Update Readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index f4a13169..b544ee21 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,12 @@ gor --input-tcp replay.local:28020 --output-http http://staging.com --output-htt ``` The given example will follow up to 2 redirects per request. +### HTTP timeouts +By default http timeout for both request and response is 5 seconds. You can override it like this: +``` +gor --input-tcp replay.local:28020 --output-http http://staging.com --output-http-timeout 30s +``` + ### Rate limiting Rate limiting can be useful if you want forward only part of production traffic and not overload your staging environment. There is 2 strategies: dropping random requests or dropping fraction of requests based on Header or URL param value.