Skip to content

Commit

Permalink
make http client configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmuth committed Sep 2, 2017
1 parent 8a3e194 commit 7c0e828
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
21 changes: 15 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
log "github.com/sirupsen/logrus"
"github.com/kelseyhightower/envconfig"
"net"
)

func main() {
Expand All @@ -19,12 +20,20 @@ func main() {
log.Info("Listening on", config.Port)

httpClient := &http.Client{
// TODO: make these options configurable
Transport: &http.Transport{
MaxIdleConnsPerHost: 100,
},
Timeout: time.Millisecond * 1500,
}
Transport: &http.Transport{
MaxIdleConnsPerHost: config.HTTPClientMaxIdleConnsPerHost,
DialContext: (&net.Dialer{
Timeout: time.Duration(config.HTTPClientDialerTimeoutMS) * time.Millisecond,
KeepAlive: time.Duration(config.HTTPClientDialerKeepAliveMS) * time.Millisecond,
}).DialContext,
MaxIdleConns: config.HTTPClientMaxIdleConns,
IdleConnTimeout: time.Duration(config.HTTPClientIdleConnTimeoutMS) * time.Millisecond,
TLSHandshakeTimeout: time.Duration(config.HTTPClientTLSHandshakeTimeoutMS) * time.Millisecond,
ExpectContinueTimeout: time.Duration(config.HTTPClientExpectContinueTimeoutMS) * time.Millisecond,

},
Timeout: time.Duration(config.HTTPClientTimeoutMS) * time.Millisecond,
}

service := &Service{config.ServiceBaseURL, httpClient}
handler := &Handler{*service}
Expand Down
14 changes: 11 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package main

type AppConfig struct {
Port int `default:"8000"`
ServiceBaseURL string `envconfig:"SERVICE_BASE_URL" required:"true"`
Env string `envconfig:"ENV_NAME" required:"true"`
Port int `default:"8000"`
ServiceBaseURL string `envconfig:"SERVICE_BASE_URL" required:"true"`
Env string `envconfig:"ENV_NAME" required:"true"`
HTTPClientMaxIdleConnsPerHost int `envconfig:"HTTP_CLIENT_MAX_IDLE_CONNS_PER_HOST" required:"true"`
HTTPClientMaxIdleConns int `envconfig:"HTTP_CLIENT_MAX_IDLE_CONNS" required:"true"`
HTTPClientDialerTimeoutMS int `envconfig:"HTTP_CLIENT_DIALER_TIMEOUT_MS" required:"true"`
HTTPClientDialerKeepAliveMS int `envconfig:"HTTP_CLIENT_DIALER_KEEPALIVE_MS" required:"true"`
HTTPClientIdleConnTimeoutMS int `envconfig:"HTTP_CLIENT_IDLE_CONN_TIMEOUT_MS" required:"true"`
HTTPClientTLSHandshakeTimeoutMS int `envconfig:"HTTP_CLIENT_TLS_HANDSHAKE_TIMEOUT_MS" required:"true"`
HTTPClientExpectContinueTimeoutMS int `envconfig:"HTTP_CLIENT_EXPECT_CONTINUE_TIMEOUT_MS" required:"true"`
HTTPClientTimeoutMS int `envconfig:"HTTP_CLIENT_TIMEOUT_MS" required:"true"`
}

func (c *AppConfig) IsLocal() bool {
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ services:
- SERVICE_BASE_URL=http://fake-service:9090/service
- ENV_NAME=local
- LOG_LEVEL=debug
- HTTP_CLIENT_MAX_IDLE_CONNS_PER_HOST=1
- HTTP_CLIENT_MAX_IDLE_CONNS=1
- HTTP_CLIENT_DIALER_TIMEOUT_MS=30000
- HTTP_CLIENT_DIALER_KEEPALIVE_MS=30000
- HTTP_CLIENT_IDLE_CONN_TIMEOUT_MS=90000
- HTTP_CLIENT_TLS_HANDSHAKE_TIMEOUT_MS=10000
- HTTP_CLIENT_EXPECT_CONTINUE_TIMEOUT_MS=1000
- HTTP_CLIENT_TIMEOUT_MS=1500

links:
- fake-service
ports:
Expand Down
3 changes: 0 additions & 3 deletions locust/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ def api(l):
class UserBehavior(TaskSet):
tasks = {api: 5}

def on_start(self):
login(self)

class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 5
Expand Down
3 changes: 3 additions & 0 deletions scripts/docker-build-app-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

sudo docker build -t http-client-test .
18 changes: 18 additions & 0 deletions scripts/docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -e
set -u

sudo docker run -p '8000:8000' \
-e 'ENV_NAME=test' \
-e 'SERVICE_BASE_URL=http://ec2-54-197-30-116.compute-1.amazonaws.com:8001/service' \
-e 'LOG_LEVEL=warn' \
-e 'HTTP_CLIENT_MAX_IDLE_CONNS_PER_HOST=100' \
-e 'HTTP_CLIENT_MAX_IDLE_CONNS=100' \
-e 'HTTP_CLIENT_DIALER_TIMEOUT_MS=30000' \
-e 'HTTP_CLIENT_DIALER_KEEPALIVE_MS=30000' \
-e 'HTTP_CLIENT_IDLE_CONN_TIMEOUT_MS=90000' \
-e 'HTTP_CLIENT_TLS_HANDSHAKE_TIMEOUT_MS=10000' \
-e 'HTTP_CLIENT_EXPECT_CONTINUE_TIMEOUT_MS=1000' \
-e 'HTTP_CLIENT_TIMEOUT_MS=1500' \
http-client-test

0 comments on commit 7c0e828

Please sign in to comment.