Skip to content

Commit

Permalink
feat: reuse connections and limit the number of connections for prehe…
Browse files Browse the repository at this point in the history
…ating

Signed-off-by: Gaius <gaius.qi@gmail.com>
  • Loading branch information
gaius-qi committed Dec 6, 2024
1 parent a97584a commit 472aa30
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions manager/job/preheat.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ const (
PreheatFileType PreheatType = "file"
)

// defaultHTTPTransport is the default http transport.
var defaultHTTPTransport = &http.Transport{
MaxIdleConns: 400,
MaxIdleConnsPerHost: 20,
MaxConnsPerHost: 50,
IdleConnTimeout: 120 * time.Second,
}

// accessURLPattern is the pattern of access url.
var accessURLPattern, _ = regexp.Compile("^(.*)://(.*)/v2/(.*)/manifests/(.*)")

Expand All @@ -77,20 +85,34 @@ type Preheat interface {
// preheat is an implementation of Preheat.
type preheat struct {
job *internaljob.Job
registryTimeout time.Duration
rootCAs *x509.CertPool
certificateChain [][]byte
insecureSkipVerify bool
httpClient *http.Client
}

// newPreheat creates a new Preheat.
func newPreheat(job *internaljob.Job, registryTimeout time.Duration, rootCAs *x509.CertPool, insecureSkipVerify bool) (Preheat, error) {
var certificateChain [][]byte
p := &preheat{
job: job,
insecureSkipVerify: insecureSkipVerify,
httpClient: &http.Client{
Timeout: registryTimeout,
Transport: &http.Transport{
DialContext: nethttp.NewSafeDialer().DialContext,
TLSClientConfig: &tls.Config{RootCAs: rootCAs, InsecureSkipVerify: insecureSkipVerify},
MaxIdleConns: defaultHTTPTransport.MaxIdleConns,
MaxIdleConnsPerHost: defaultHTTPTransport.MaxIdleConnsPerHost,
MaxConnsPerHost: defaultHTTPTransport.MaxConnsPerHost,
IdleConnTimeout: defaultHTTPTransport.IdleConnTimeout,
},
},
}

if rootCAs != nil {
certificateChain = rootCAs.Subjects()
p.certificateChain = rootCAs.Subjects()
}

return &preheat{job, registryTimeout, rootCAs, certificateChain, insecureSkipVerify}, nil
return p, nil
}

// CreatePreheat creates a preheat job.
Expand Down Expand Up @@ -192,13 +214,7 @@ func (p *preheat) getImageLayers(ctx context.Context, args types.PreheatArgs) ([
}

opts := []imageAuthClientOption{
withHTTPClient(&http.Client{
Timeout: p.registryTimeout,
Transport: &http.Transport{
DialContext: nethttp.NewSafeDialer().DialContext,
TLSClientConfig: &tls.Config{RootCAs: p.rootCAs, InsecureSkipVerify: p.insecureSkipVerify},
},
}),
withHTTPClient(p.httpClient),
withBasicAuth(args.Username, args.Password),
}
// Background:
Expand Down Expand Up @@ -395,8 +411,11 @@ type imageAuthClient struct {

// newImageAuthClient creates a new imageAuthClient.
func newImageAuthClient(image *preheatImage, opts ...imageAuthClientOption) (*imageAuthClient, error) {
httpClient := http.DefaultClient
httpClient.Transport = defaultHTTPTransport

d := &imageAuthClient{
httpClient: http.DefaultClient,
httpClient: httpClient,
interceptorTokenHandler: newInterceptorTokenHandler(),
}

Expand Down

0 comments on commit 472aa30

Please sign in to comment.