Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse http client #47

Merged
merged 4 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- "1.12"
- "1.14"
install:
- go get -t ./...
- curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.18.0
Expand Down
68 changes: 63 additions & 5 deletions http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ package spectator
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"

"github.com/pkg/errors"
)

type HttpClient struct {
registry *Registry
timeout time.Duration
client *http.Client
}

func NewHttpClient(registry *Registry, timeout time.Duration) *HttpClient {
return &HttpClient{registry, timeout}
return &HttpClient{registry, newSingleHostClient(timeout)}
}

func userFriendlyErr(errStr string) string {
Expand Down Expand Up @@ -76,11 +79,10 @@ func (h *HttpClient) doHttpPost(uri string, jsonBytes []byte, attemptNumber int)
if err != nil {
panic(err)
}
client := http.Client{}
client.Timeout = h.timeout
entry := NewLogEntry(h.registry, "POST", uri)
log.Debugf("posting data to %s, payload %d bytes", uri, len(jsonBytes))
resp, err := client.Do(req)
defer h.client.CloseIdleConnections()
resp, err := h.client.Do(req)
if err != nil {
var status string
if urlErr, ok := err.(*url.Error); ok {
Expand Down Expand Up @@ -143,3 +145,59 @@ func (h *HttpClient) PostJson(uri string, jsonBytes []byte) (response HttpRespon
}
return
}
func newSingleHostClient(timeout time.Duration) *http.Client {
return &http.Client{
Timeout: timeout,
Transport: &keepAliveTransport{wrapped: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 50,
MaxIdleConnsPerHost: 50,
MaxConnsPerHost: 100,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 2 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// DisableKeepAlives: true,
}},
}
}

type keepAliveTransport struct {
wrapped http.RoundTripper
}

func (k *keepAliveTransport) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := k.wrapped.RoundTrip(r)
if err != nil {
return resp, err
}
resp.Body = &drainingReadCloser{rdr: resp.Body}
return resp, nil
}

type drainingReadCloser struct {
rdr io.ReadCloser
seenEOF uint32
}

func (d *drainingReadCloser) Read(p []byte) (n int, err error) {
n, err = d.rdr.Read(p)
if err == io.EOF || n == 0 {
atomic.StoreUint32(&d.seenEOF, 1)
}
return
}

func (d *drainingReadCloser) Close() error {
// drain buffer
if atomic.LoadUint32(&d.seenEOF) != 1 {
//#nosec
_, _ = io.Copy(ioutil.Discard, d.rdr)
}
return d.rdr.Close()
}