Skip to content

Commit cf9f4d0

Browse files
erikdubbelboerkarmi
authored andcommitted
Examples: Improve the "valyala/fasthttp" example
Closes #97 (cherry picked from commit 0c70c42)
1 parent 44daba1 commit cf9f4d0

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

_examples/fasthttp/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ request and response object between `net/http` and `fasthttp`.
99
To run the example:
1010

1111
go run cmd/main.go
12-
# 1000 requests in 1.15 (870.2 req/s) | min: 785.292µs / max: 10.553253ms / mean: 1.052083ms
12+
# 1000 requests in 1.07 (933.4 req/s) | min: 749.961µs / max: 10.003318ms / mean: 1.013992ms
1313

1414
To run the benchmarks:
1515

1616
make bench
1717

18-
BenchmarkHTTPClient/Info()-4 2000 8721355 ns/op 16279 B/op 131 allocs/op
19-
BenchmarkFastHTTPClient/Info()-4 10000 1182717 ns/op 3075 B/op 39 allocs/op
18+
BenchmarkHTTPClient/Info()-4 1591 7139770 ns/op 16725 B/op 132 allocs/op
19+
BenchmarkFastHTTPClient/Info()-4 10000 1315049 ns/op 2255 B/op 24 allocs/op

_examples/fasthttp/fasthttp.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
package fasthttp
66

77
import (
8-
"bytes"
9-
"io"
108
"io/ioutil"
119
"net/http"
10+
"strings"
1211

1312
"github.com/valyala/fasthttp"
1413
)
@@ -22,8 +21,13 @@ type Transport struct{}
2221
//
2322
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
2423
freq := fasthttp.AcquireRequest()
24+
defer fasthttp.ReleaseRequest(freq)
25+
2526
fres := fasthttp.AcquireResponse()
27+
defer fasthttp.ReleaseResponse(fres)
28+
2629
t.copyRequest(freq, req)
30+
2731
err := fasthttp.Do(freq, fres)
2832
if err != nil {
2933
return nil, err
@@ -35,11 +39,9 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
3539
return res, nil
3640
}
3741

38-
// copyRequest converts http.Request to fasthttp.Request
42+
// copyRequest converts a http.Request to fasthttp.Request
3943
//
4044
func (t *Transport) copyRequest(dst *fasthttp.Request, src *http.Request) *fasthttp.Request {
41-
dst.Reset()
42-
4345
dst.SetHost(src.Host)
4446
dst.SetRequestURI(src.URL.String())
4547

@@ -53,16 +55,13 @@ func (t *Transport) copyRequest(dst *fasthttp.Request, src *http.Request) *fasth
5355
}
5456

5557
if src.Body != nil {
56-
var b bytes.Buffer
57-
io.Copy(&b, src.Body)
58-
59-
dst.SetBody(b.Bytes())
58+
dst.SetBodyStream(src.Body, -1)
6059
}
6160

6261
return dst
6362
}
6463

65-
// copyResponse converts http.Response to fasthttp.Response
64+
// copyResponse converts a http.Response to fasthttp.Response
6665
//
6766
func (t *Transport) copyResponse(dst *http.Response, src *fasthttp.Response) *http.Response {
6867
dst.StatusCode = src.StatusCode()
@@ -71,9 +70,9 @@ func (t *Transport) copyResponse(dst *http.Response, src *fasthttp.Response) *ht
7170
dst.Header.Set(string(k), string(v))
7271
})
7372

74-
if src.Body != nil {
75-
dst.Body = ioutil.NopCloser(bytes.NewReader(src.Body()))
76-
}
73+
// Cast to a string to make a copy seeing as src.Body() won't
74+
// be valid after the response is released back to the pool (fasthttp.ReleaseResponse).
75+
dst.Body = ioutil.NopCloser(strings.NewReader(string(src.Body())))
7776

7877
return dst
7978
}

_examples/fasthttp/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ replace github.com/elastic/go-elasticsearch/v7 => ../..
66

77
require (
88
github.com/elastic/go-elasticsearch/v7 v7.0.0-20190407092644-3fb2a278216b
9-
github.com/valyala/fasthttp v1.1.0
9+
github.com/valyala/fasthttp v1.5.0
1010
)

0 commit comments

Comments
 (0)