-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
460 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"golang.org/x/net/http2" | ||
|
||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
type client interface { | ||
do(int) (int, time.Duration, error) | ||
} | ||
|
||
type clientDoer interface { | ||
Do(req *fasthttp.Request, resp *fasthttp.Response) error | ||
} | ||
|
||
type clientConfig struct { | ||
method string | ||
url string | ||
headers Headers | ||
host string | ||
stream bool | ||
body []byte | ||
http2 bool | ||
maxConns int | ||
timeout time.Duration | ||
tlsConfig *tls.Config | ||
disableKeepAlives bool | ||
throughput *int64 | ||
} | ||
|
||
type fasthttpClient struct { | ||
doer clientDoer | ||
reqs []*fasthttp.Request | ||
resps []*fasthttp.Response | ||
readers []*bytes.Reader | ||
body []byte | ||
} | ||
|
||
func newFasthttpClient(cc clientConfig) (client, error) { | ||
c := &fasthttpClient{ | ||
reqs: make([]*fasthttp.Request, cc.maxConns, cc.maxConns), | ||
resps: make([]*fasthttp.Response, cc.maxConns, cc.maxConns), | ||
readers: make([]*bytes.Reader, cc.maxConns, cc.maxConns), | ||
body: cc.body, | ||
} | ||
|
||
isTLS, addr, err := getIsTLSAndAddr(cc.url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i := 0; i < cc.maxConns; i++ { | ||
req := fasthttp.AcquireRequest() | ||
req.Header.SetMethod(cc.method) | ||
req.SetRequestURI(cc.url) | ||
cc.headers.WriteToFasthttp(req) | ||
if cc.stream { | ||
c.readers[i] = bytes.NewReader(nil) | ||
} else { | ||
// set constant body | ||
req.SetBody(cc.body) | ||
} | ||
if cc.disableKeepAlives { | ||
req.Header.ConnectionClose() | ||
} | ||
if cc.host != "" { | ||
req.URI().SetHost(cc.host) | ||
} | ||
c.reqs[i] = req | ||
c.resps[i] = fasthttp.AcquireResponse() | ||
} | ||
|
||
if cc.disableKeepAlives { | ||
c.doer = &fasthttp.HostClient{ | ||
Addr: addr, | ||
NoDefaultUserAgentHeader: false, | ||
Dial: fasthttpDialFunc(cc.throughput, cc.timeout), | ||
DialDualStack: false, | ||
IsTLS: isTLS, | ||
TLSConfig: cc.tlsConfig, | ||
MaxConns: cc.maxConns, | ||
ReadTimeout: cc.timeout, | ||
DisableHeaderNamesNormalizing: true, | ||
DisablePathNormalizing: true, | ||
} | ||
} else { | ||
c.doer = &fasthttp.PipelineClient{ | ||
Addr: addr, | ||
MaxConns: cc.maxConns, | ||
Dial: fasthttpDialFunc(cc.throughput, cc.timeout), | ||
IsTLS: isTLS, | ||
TLSConfig: cc.tlsConfig, | ||
ReadTimeout: cc.timeout, | ||
} | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *fasthttpClient) do(i int) (code int, latency time.Duration, err error) { | ||
var ( | ||
req = c.reqs[i] | ||
resp = c.resps[i] | ||
reader = c.readers[i] | ||
) | ||
|
||
if reader != nil { | ||
reader.Reset(c.body) | ||
req.SetBodyStream(reader, -1) | ||
} | ||
|
||
start := time.Now() | ||
if err = c.doer.Do(req, resp); err != nil { | ||
return | ||
} | ||
|
||
code = resp.StatusCode() | ||
latency = time.Since(start) | ||
|
||
return | ||
} | ||
|
||
var ( | ||
strHTTP = []byte("http") | ||
strHTTPS = []byte("https") | ||
) | ||
|
||
func getIsTLSAndAddr(url string) (isTLS bool, addr string, err error) { | ||
req := fasthttp.AcquireRequest() | ||
defer fasthttp.ReleaseRequest(req) | ||
req.SetRequestURI(url) | ||
uri := req.URI() | ||
host := uri.Host() | ||
|
||
scheme := uri.Scheme() | ||
if bytes.Equal(scheme, strHTTPS) { | ||
isTLS = true | ||
} else if !bytes.Equal(scheme, strHTTP) { | ||
err = fmt.Errorf("unsupported protocol %q. http and https are supported", scheme) | ||
return | ||
} | ||
|
||
addr = addMissingPort(string(host), isTLS) | ||
|
||
return | ||
} | ||
|
||
func addMissingPort(addr string, isTLS bool) string { | ||
n := strings.Index(addr, ":") | ||
if n >= 0 { | ||
return addr | ||
} | ||
port := 80 | ||
if isTLS { | ||
port = 443 | ||
} | ||
return net.JoinHostPort(addr, strconv.Itoa(port)) | ||
} | ||
|
||
type httpClient struct { | ||
client *http.Client | ||
reqs []*http.Request | ||
readers []*bytes.Reader | ||
body []byte | ||
} | ||
|
||
func newHttpClient(cc clientConfig) (client, error) { | ||
c := &httpClient{ | ||
reqs: make([]*http.Request, 0, cc.maxConns), | ||
readers: make([]*bytes.Reader, 0, cc.maxConns), | ||
} | ||
|
||
var err error | ||
|
||
for i := 0; i < cc.maxConns; i++ { | ||
if c.reqs[i], err = http.NewRequest(cc.method, cc.url, nil); err != nil { | ||
return nil, fmt.Errorf("failed to new request: %w", err) | ||
} | ||
req := c.reqs[i] | ||
|
||
cc.headers.WriteToHttp(req) | ||
|
||
if cc.host != "" { | ||
req.Host = cc.host | ||
} | ||
} | ||
|
||
transport := &http.Transport{ | ||
TLSClientConfig: cc.tlsConfig, | ||
MaxIdleConnsPerHost: cc.maxConns, | ||
DisableKeepAlives: cc.disableKeepAlives, | ||
} | ||
transport.DialContext = httpDialContextFunc(cc.throughput) | ||
if cc.http2 { | ||
if err = http2.ConfigureTransport(transport); err != nil { | ||
return nil, fmt.Errorf("failed to setup http2: %w", err) | ||
} | ||
} else { | ||
transport.TLSNextProto = make( | ||
map[string]func(authority string, c *tls.Conn) http.RoundTripper, | ||
) | ||
} | ||
|
||
c.client = &http.Client{ | ||
Transport: transport, | ||
Timeout: cc.timeout, | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *httpClient) do(i int) (code int, latency time.Duration, err error) { | ||
req := c.reqs[i] | ||
|
||
reader := c.readers[i] | ||
reader.Reset(c.body) | ||
req.Body = ioutil.NopCloser(reader) | ||
|
||
var resp *http.Response | ||
start := time.Now() | ||
|
||
if resp, err = c.client.Do(req); err != nil { | ||
return | ||
} | ||
|
||
if err = resp.Body.Close(); err != nil { | ||
return | ||
} | ||
|
||
code = resp.StatusCode | ||
latency = time.Since(start) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
// counterConn counts read bytes and written bytes | ||
type counterConn struct { | ||
net.Conn | ||
n *int64 | ||
} | ||
|
||
func (cc *counterConn) Read(b []byte) (n int, err error) { | ||
n, err = cc.Conn.Read(b) | ||
|
||
if err == nil { | ||
atomic.AddInt64(cc.n, int64(n)) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (cc *counterConn) Write(b []byte) (n int, err error) { | ||
n, err = cc.Conn.Write(b) | ||
|
||
if err == nil { | ||
atomic.AddInt64(cc.n, int64(n)) | ||
} | ||
|
||
return | ||
} | ||
|
||
var fasthttpDialFunc = func(throughput *int64, timeout time.Duration) func(string) (net.Conn, error) { | ||
dialer := &fasthttp.TCPDialer{} | ||
return func(address string) (net.Conn, error) { | ||
conn, err := dialer.DialDualStackTimeout(address, timeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cc := &counterConn{ | ||
Conn: conn, | ||
n: throughput, | ||
} | ||
|
||
return cc, nil | ||
} | ||
} | ||
|
||
var httpDialContextFunc = func(throughput *int64) func(context.Context, string, string) (net.Conn, error) { | ||
dialer := &net.Dialer{} | ||
return func(ctx context.Context, network, address string) (net.Conn, error) { | ||
conn, err := dialer.DialContext(ctx, network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cc := &counterConn{ | ||
Conn: conn, | ||
n: throughput, | ||
} | ||
|
||
return cc, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package internal | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type errorMap sync.Map | ||
|
||
func (e errorMap) add(err error) { | ||
} | ||
|
||
func (e errorMap) get(err error) uint64 { | ||
return 0 | ||
} | ||
|
||
func (e errorMap) sum() uint64 { | ||
return 0 | ||
} |
Oops, something went wrong.