Skip to content

Commit

Permalink
attacker: Simplify timeout configuration (tsenart#397)
Browse files Browse the repository at this point in the history
* attacker: Simplify timeout configuration

This commit simplifies the timeout configuration internally to make use
of the `http.Client.Timeout` field which didn't exist when this code was
originally written.

It also fixes tsenart#396: a test flake of `TestTimeout`.

* Update lib/attack_test.go
  • Loading branch information
tsenart authored Apr 29, 2019
1 parent 620397c commit e827e02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
19 changes: 7 additions & 12 deletions lib/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ func NewAttacker(opts ...func(*Attacker)) *Attacker {
a.dialer = &net.Dialer{
LocalAddr: &net.TCPAddr{IP: DefaultLocalAddr.IP, Zone: DefaultLocalAddr.Zone},
KeepAlive: 30 * time.Second,
Timeout: DefaultTimeout,
}

a.client = http.Client{
Timeout: DefaultTimeout,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: a.dialer.Dial,
ResponseHeaderTimeout: DefaultTimeout,
TLSClientConfig: DefaultTLSConfig,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConnsPerHost: DefaultConnections,
Proxy: http.ProxyFromEnvironment,
Dial: a.dialer.Dial,
TLSClientConfig: DefaultTLSConfig,
MaxIdleConnsPerHost: DefaultConnections,
},
}

Expand Down Expand Up @@ -132,13 +130,10 @@ func Proxy(proxy func(*http.Request) (*url.URL, error)) func(*Attacker) {
}

// Timeout returns a functional option which sets the maximum amount of time
// an Attacker will wait for a request to be responded to.
// an Attacker will wait for a request to be responded to and completely read.
func Timeout(d time.Duration) func(*Attacker) {
return func(a *Attacker) {
tr := a.client.Transport.(*http.Transport)
tr.ResponseHeaderTimeout = d
a.dialer.Timeout = d
tr.Dial = a.dialer.Dial
a.client.Timeout = d
}
}

Expand Down
15 changes: 6 additions & 9 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func TestTimeout(t *testing.T) {
atk := NewAttacker(Timeout(10 * time.Millisecond))
tr := NewStaticTargeter(Target{Method: "GET", URL: server.URL})
res := atk.hit(tr, "")
want := "net/http: timeout awaiting response headers"
if got := res.Error; !strings.HasSuffix(got, want) {
want := "Client.Timeout exceeded while awaiting headers"
if got := res.Error; !strings.Contains(got, want) {
t.Fatalf("want: '%v' in '%v'", want, got)
}
}
Expand Down Expand Up @@ -326,18 +326,15 @@ func TestClient(t *testing.T) {
dialer := &net.Dialer{
LocalAddr: &net.TCPAddr{IP: DefaultLocalAddr.IP, Zone: DefaultLocalAddr.Zone},
KeepAlive: 30 * time.Second,
Timeout: DefaultTimeout,
}

client := &http.Client{
Timeout: time.Duration(1 * time.Nanosecond),
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
ResponseHeaderTimeout: DefaultTimeout,
TLSClientConfig: DefaultTLSConfig,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConnsPerHost: DefaultConnections,
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
TLSClientConfig: DefaultTLSConfig,
MaxIdleConnsPerHost: DefaultConnections,
},
}

Expand Down

0 comments on commit e827e02

Please sign in to comment.