Skip to content

Commit 5c96d25

Browse files
committed
feat: reuse connections and limit the number of connections for preheating
Signed-off-by: Gaius <gaius.qi@gmail.com>
1 parent a97584a commit 5c96d25

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

manager/job/preheat.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ const (
6565
PreheatFileType PreheatType = "file"
6666
)
6767

68+
// defaultHTTPTransport is the default http transport.
69+
var defaultHTTPTransport = &http.Transport{
70+
MaxIdleConns: 400,
71+
MaxIdleConnsPerHost: 20,
72+
MaxConnsPerHost: 50,
73+
IdleConnTimeout: 120 * time.Second,
74+
}
75+
6876
// accessURLPattern is the pattern of access url.
6977
var accessURLPattern, _ = regexp.Compile("^(.*)://(.*)/v2/(.*)/manifests/(.*)")
7078

@@ -77,20 +85,34 @@ type Preheat interface {
7785
// preheat is an implementation of Preheat.
7886
type preheat struct {
7987
job *internaljob.Job
80-
registryTimeout time.Duration
81-
rootCAs *x509.CertPool
8288
certificateChain [][]byte
8389
insecureSkipVerify bool
90+
httpClient *http.Client
8491
}
8592

8693
// newPreheat creates a new Preheat.
8794
func newPreheat(job *internaljob.Job, registryTimeout time.Duration, rootCAs *x509.CertPool, insecureSkipVerify bool) (Preheat, error) {
88-
var certificateChain [][]byte
95+
p := &preheat{
96+
job: job,
97+
insecureSkipVerify: insecureSkipVerify,
98+
httpClient: &http.Client{
99+
Timeout: registryTimeout,
100+
Transport: &http.Transport{
101+
DialContext: nethttp.NewSafeDialer().DialContext,
102+
TLSClientConfig: &tls.Config{RootCAs: rootCAs, InsecureSkipVerify: insecureSkipVerify},
103+
MaxIdleConns: defaultHTTPTransport.MaxIdleConns,
104+
MaxIdleConnsPerHost: defaultHTTPTransport.MaxIdleConnsPerHost,
105+
MaxConnsPerHost: defaultHTTPTransport.MaxConnsPerHost,
106+
IdleConnTimeout: defaultHTTPTransport.IdleConnTimeout,
107+
},
108+
},
109+
}
110+
89111
if rootCAs != nil {
90-
certificateChain = rootCAs.Subjects()
112+
p.certificateChain = rootCAs.Subjects()
91113
}
92114

93-
return &preheat{job, registryTimeout, rootCAs, certificateChain, insecureSkipVerify}, nil
115+
return p, nil
94116
}
95117

96118
// CreatePreheat creates a preheat job.
@@ -191,26 +213,20 @@ func (p *preheat) getImageLayers(ctx context.Context, args types.PreheatArgs) ([
191213
return nil, err
192214
}
193215

194-
opts := []imageAuthClientOption{
195-
withHTTPClient(&http.Client{
196-
Timeout: p.registryTimeout,
197-
Transport: &http.Transport{
198-
DialContext: nethttp.NewSafeDialer().DialContext,
199-
TLSClientConfig: &tls.Config{RootCAs: p.rootCAs, InsecureSkipVerify: p.insecureSkipVerify},
200-
},
201-
}),
216+
options := []imageAuthClientOption{
217+
withHTTPClient(p.httpClient),
202218
withBasicAuth(args.Username, args.Password),
203219
}
204220
// Background:
205-
// Harbor uses the V1 preheat request and will carry the auth info in the headers.
221+
// Harbor uses the V1 preheat request and will carry the auth info in the headers.
206222
header := nethttp.MapToHeader(args.Headers)
207223
if token := header.Get("Authorization"); len(token) > 0 {
208-
opts = append(opts, withIssuedToken(token))
224+
options = append(options, withIssuedToken(token))
209225
header.Set("Authorization", token)
210226
}
211227

212228
// Init docker auth client.
213-
client, err := newImageAuthClient(image, opts...)
229+
client, err := newImageAuthClient(image, options...)
214230
if err != nil {
215231
return nil, err
216232
}
@@ -395,8 +411,11 @@ type imageAuthClient struct {
395411

396412
// newImageAuthClient creates a new imageAuthClient.
397413
func newImageAuthClient(image *preheatImage, opts ...imageAuthClientOption) (*imageAuthClient, error) {
414+
httpClient := http.DefaultClient
415+
httpClient.Transport = defaultHTTPTransport
416+
398417
d := &imageAuthClient{
399-
httpClient: http.DefaultClient,
418+
httpClient: httpClient,
400419
interceptorTokenHandler: newInterceptorTokenHandler(),
401420
}
402421

manager/job/preheat_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package job
22

33
import (
44
"context"
5+
"net/http"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -42,7 +43,7 @@ func TestPreheat_getImageLayers(t *testing.T) {
4243

4344
for _, tc := range tests {
4445
t.Run(tc.name, func(t *testing.T) {
45-
p := &preheat{}
46+
p := &preheat{httpClient: http.DefaultClient}
4647
layers, err := p.getImageLayers(context.Background(), tc.args)
4748
if err != nil {
4849
t.Fatal(err)

0 commit comments

Comments
 (0)