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

[release-1.24] Create a single transport to reuse connections, fix performance issues and enable tracing #2071

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Azure/go-autorest/autorest/adal v0.9.20
github.com/Azure/go-autorest/autorest/mocks v0.4.2
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/Azure/go-autorest/tracing v0.6.0
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/fsnotify/fsnotify v1.5.4
github.com/go-logr/logr v1.2.3
Expand Down Expand Up @@ -40,7 +41,6 @@ require (
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
Expand Down
51 changes: 51 additions & 0 deletions pkg/azureclients/armclient/azure_armclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package armclient

import (
"context"
"crypto/tls"
"fmt"
"html"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"sync"
Expand All @@ -31,12 +34,27 @@ import (

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/tracing"

"k8s.io/klog/v2"
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
"sigs.k8s.io/cloud-provider-azure/pkg/version"
)

// there is one sender per TLS renegotiation type, i.e. count of tls.RenegotiationSupport enums

type defaultSender struct {
sender autorest.Sender
init *sync.Once
}

// each type of sender will be created on demand in sender()
var defaultSenders defaultSender

func init() {
defaultSenders.init = &sync.Once{}
}

var _ Interface = &Client{}

// Client implements ARM client Interface.
Expand All @@ -47,10 +65,43 @@ type Client struct {
regionalEndpoint string
}

func sender() autorest.Sender {
// note that we can't init defaultSenders in init() since it will
// execute before calling code has had a chance to enable tracing
defaultSenders.init.Do(func() {
// copied from http.DefaultTransport with a TLS minimum version.
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second, // the same as default transport
KeepAlive: 30 * time.Second, // the same as default transport
}).DialContext,
ForceAttemptHTTP2: true, // always attempt HTTP/2 even though custom dialer is provided
MaxIdleConns: 100, // Zero means no limit, the same as default transport
MaxIdleConnsPerHost: 100, // Default is 2, ref:https://cs.opensource.google/go/go/+/go1.18.4:src/net/http/transport.go;l=58
IdleConnTimeout: 90 * time.Second, // the same as default transport
TLSHandshakeTimeout: 10 * time.Second, // the same as default transport
ExpectContinueTimeout: 1 * time.Second, // the same as default transport
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12, //force to use TLS 1.2
Renegotiation: tls.RenegotiateNever, // the same as default transport https://pkg.go.dev/crypto/tls#RenegotiationSupport
},
}
var roundTripper http.RoundTripper = transport
if tracing.IsEnabled() {
roundTripper = tracing.NewTransport(transport)
}
j, _ := cookiejar.New(nil)
defaultSenders.sender = &http.Client{Jar: j, Transport: roundTripper}
})
return defaultSenders.sender
}

// New creates a ARM client
func New(authorizer autorest.Authorizer, clientConfig azureclients.ClientConfig, baseURI, apiVersion string, sendDecoraters ...autorest.SendDecorator) *Client {
restClient := autorest.NewClientWithUserAgent(clientConfig.UserAgent)
restClient.Authorizer = authorizer
restClient.Sender = sender()

if clientConfig.UserAgent == "" {
restClient.UserAgent = GetUserAgent(restClient)
Expand Down