Skip to content

Commit

Permalink
Attempt to workaround golang/go#59690
Browse files Browse the repository at this point in the history
  • Loading branch information
patrobinson committed Sep 23, 2024
1 parent dd959ad commit 42281ab
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptrace"
"net/http/httputil"
Expand All @@ -22,6 +23,7 @@ import (

"github.com/buildkite/agent/v3/logger"
"github.com/google/go-querystring/query"
"golang.org/x/net/http2"
)

const (
Expand Down Expand Up @@ -80,30 +82,44 @@ func NewClient(l logger.Logger, conf Config) *Client {
}

httpClient := conf.HTTPClient
if conf.HTTPClient == nil {

// use the default transport as it is optimized and configured for http2
// and will avoid accidents in the future
tr := http.DefaultTransport.(*http.Transport).Clone()

if conf.HTTPClient == nil {
if conf.DisableHTTP2 {
tr.ForceAttemptHTTP2 = false
tr.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
// The default TLSClientConfig has h2 in NextProtos, so the negotiated TLS connection will assume h2 support.
// see https://github.com/golang/go/issues/50571
tr.TLSClientConfig.NextProtos = []string{"http/1.1"}
}

if conf.TLSConfig != nil {
tr.TLSClientConfig = conf.TLSConfig
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableCompression: false,
DisableKeepAlives: false,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
httpClient = &http.Client{
Timeout: 60 * time.Second,
Transport: &authenticatedTransport{
Token: conf.Token,
Delegate: tr,
},
}
} else {
tr := &http2.Transport{
ReadIdleTimeout: 30 * time.Second,
}
if conf.TLSConfig != nil {
tr.TLSClientConfig = conf.TLSConfig
}

httpClient = &http.Client{
Timeout: 60 * time.Second,
Transport: &authenticatedTransport{
Token: conf.Token,
Delegate: tr,
},
httpClient = &http.Client{
Timeout: 60 * time.Second,
Transport: &authenticatedTransport{
Token: conf.Token,
Delegate: tr,
},
}
}
}

Expand Down Expand Up @@ -270,6 +286,7 @@ func (c *Client) doRequest(req *http.Request, v any) (*Response, error) {
c.logger.Debug("%s %s", req.Method, req.URL)

resp, err := c.client.Do(req)

if err != nil {
if c.conf.TraceHTTP {
tracer.EmitTraceToLog(logger.ERROR)
Expand Down Expand Up @@ -377,6 +394,7 @@ func traceHTTPRequest(req *http.Request, t *tracer) *http.Request {
t.LogField("reused", strconv.FormatBool(info.Reused))
t.LogField("idle", strconv.FormatBool(info.WasIdle))
t.LogDuration("idleTime", info.IdleTime)
t.LogField("localAddr", info.Conn.LocalAddr().String())
},
PutIdleConn: func(err error) {
t.LogTiming("putIdleConn")
Expand Down

0 comments on commit 42281ab

Please sign in to comment.