Skip to content

Commit

Permalink
Merge pull request #69 from fuweid/weifu/fix-connection
Browse files Browse the repository at this point in the history
request: should not use cached transport
  • Loading branch information
fuweid authored Jan 26, 2024
2 parents c7a1c2d + 5c70bf9 commit 5274e46
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions request/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@ package request
import (
"fmt"
"math"
"net/http"

"github.com/Azure/kperf/api/types"

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/metrics"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/scheme"
)

// FIXME(weifu):
//
// Write UT to cover it instead of hook
type transportCacheTracker struct{}

// Increment implements k8s.io/client-go/tools/metrics.TransportCreateCallsMetric interface.
func (t *transportCacheTracker) Increment(result string) {
if result != "uncacheable" {
klog.Fatal("unexpected use cache transport")
}
klog.V(3).Infof("transport cache: %s", result)
}

func init() {
metrics.Register(metrics.RegisterOpts{
TransportCreateCalls: &transportCacheTracker{},
})
}

// NewClients creates N rest.Interface.
//
// FIXME(weifu):
Expand All @@ -29,6 +51,16 @@ func NewClients(kubeCfgPath string, connsNum int, opts ...ClientCfgOpt) ([]rest.
}
restCfg.NegotiatedSerializer = scheme.Codecs.WithoutConversion()

// NOTE:
//
// Make transport uncacheable. With default proxy function, client-go
// will create new transport even if multiple clients use the same TLS
// configuration. If not, all the clients will share one transport.
// If protocol is HTTP2, there will be only one connection.
//
// REF: https://github.com/kubernetes/client-go/blob/c5938c6876a62f53c1f4ee55b879ca5c74253ae8/transport/cache.go#L154
restCfg.Proxy = http.ProxyFromEnvironment

err = cfg.apply(restCfg)
if err != nil {
return nil, err
Expand Down

0 comments on commit 5274e46

Please sign in to comment.