Skip to content

Commit 89f5665

Browse files
committed
allow to configure host header
1 parent f8849ae commit 89f5665

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

boomer/boomer.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package boomer
1818
import (
1919
"crypto/tls"
2020
"math"
21+
"net"
2122
"runtime"
2223
"sync"
2324
"time"
24-
"net"
2525

2626
"github.com/Clever/leakybucket"
2727
"github.com/Clever/leakybucket/memory"
@@ -40,12 +40,13 @@ type Result struct {
4040
type Boomer struct {
4141
// Request is the request to be made.
4242
Request *fasthttp.Request
43+
Addr string
4344

4445
// Timeout in seconds.
45-
Timeout time.Duration
46+
Timeout time.Duration
4647
ConnectTimeout time.Duration
47-
ReadTimeout time.Duration
48-
WriteTimeout time.Duration
48+
ReadTimeout time.Duration
49+
WriteTimeout time.Duration
4950

5051
// C is the concurrency level, the number of concurrent workers to run.
5152
C uint
@@ -66,13 +67,14 @@ type Boomer struct {
6667
jobs chan *fasthttp.Request
6768
running bool
6869
wg *sync.WaitGroup
69-
client *fasthttp.Client
70+
client *fasthttp.HostClient
7071
}
7172

7273
// NewBoomer returns a new instance of Boomer for the specified request.
73-
func NewBoomer(req *fasthttp.Request) *Boomer {
74+
func NewBoomer(addr string, req *fasthttp.Request) *Boomer {
7475
return &Boomer{
7576
C: uint(runtime.NumCPU()),
77+
Addr: addr,
7678
Request: req,
7779
results: make(chan Result),
7880
stop: make(chan struct{}),
@@ -169,15 +171,16 @@ func (b *Boomer) Run() {
169171
if b.running {
170172
return
171173
}
172-
b.client = &fasthttp.Client{
174+
b.client = &fasthttp.HostClient{
175+
Addr: b.Addr,
173176
Dial: func(addr string) (net.Conn, error) {
174177
return fasthttp.DialTimeout(addr, b.ConnectTimeout)
175178
},
176179
TLSConfig: &tls.Config{
177180
InsecureSkipVerify: true,
178181
},
179-
MaxConnsPerHost: math.MaxInt32,
180-
ReadTimeout: b.ReadTimeout,
182+
MaxConns: math.MaxInt32,
183+
ReadTimeout: b.ReadTimeout,
181184
WriteTimeout: b.WriteTimeout,
182185
}
183186
b.running = true

boomer/boomer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestN(t *testing.T) {
3838
req := fasthttp.AcquireRequest()
3939
req.SetRequestURI(server.URL)
4040
req.Header.SetMethod("GET")
41-
boomer := NewBoomer(req).
41+
boomer := NewBoomer(string(req.Host()), req).
4242
WithAmount(20).
4343
WithConcurrency(2)
4444
go func() {
@@ -64,7 +64,7 @@ func TestQPS(t *testing.T) {
6464
req := fasthttp.AcquireRequest()
6565
req.SetRequestURI(server.URL)
6666
req.Header.SetMethod("GET")
67-
boomer := NewBoomer(req).
67+
boomer := NewBoomer(string(req.Host()), req).
6868
WithAmount(20).
6969
WithConcurrency(2).
7070
WithRateLimit(1, time.Second)
@@ -103,7 +103,7 @@ func TestRequest(t *testing.T) {
103103
req.Header.Set("Content-Type", "text/html")
104104
req.Header.Set("X-some", "value")
105105
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("username:password")))
106-
boomer := NewBoomer(req).
106+
boomer := NewBoomer(string(req.Host()), req).
107107
WithAmount(1).
108108
WithConcurrency(1)
109109
go func() {
@@ -141,7 +141,7 @@ func TestBody(t *testing.T) {
141141
req.SetRequestURI(server.URL)
142142
req.Header.SetMethod("POST")
143143
req.SetBody([]byte("Body"))
144-
boomer := NewBoomer(req).
144+
boomer := NewBoomer(string(req.Host()), req).
145145
WithAmount(10).
146146
WithConcurrency(1)
147147
go func() {

pla.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ func main() {
107107
usageAndExit("invalid url ''" + req.URI().String() + "'', unable to detect host")
108108
}
109109
}
110+
addr := string(req.URI().Host())
111+
if !strings.Contains(addr, ":") {
112+
addr = addr + ":80"
113+
}
110114
req.Header.SetMethod(method)
111115
req.SetBodyString(*body)
112116
req.Header.SetContentLength(len(req.Body()))
@@ -120,7 +124,11 @@ func main() {
120124
if err != nil {
121125
usageAndExit(err.Error())
122126
}
123-
req.Header.Set(match[1], match[2])
127+
if match[1] == "Host" || match[1] == "host" {
128+
req.SetHost(match[2])
129+
} else {
130+
req.Header.Set(match[1], match[2])
131+
}
124132
}
125133

126134
if !*disableCompression {
@@ -132,7 +140,7 @@ func main() {
132140
}
133141

134142
ui = interfaces.NewBasicInterface()
135-
boomerInstance = boomer.NewBoomer(req).
143+
boomerInstance = boomer.NewBoomer(addr, req).
136144
WithAmount(*n).
137145
WithConcurrency(*c).
138146
WithDuration(*duration).

0 commit comments

Comments
 (0)