Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to configure timeout #183

Merged
merged 2 commits into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type HTTPClientConfig struct {
FollowRedirects int
Debug bool
OriginalHost bool
Timeout time.Duration
}

type HTTPClient struct {
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions output_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type HTTPOutputConfig struct {

elasticSearch string

Timeout time.Duration
OriginalHost bool

Debug bool
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down