@@ -65,6 +65,14 @@ const (
65
65
PreheatFileType PreheatType = "file"
66
66
)
67
67
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
+
68
76
// accessURLPattern is the pattern of access url.
69
77
var accessURLPattern , _ = regexp .Compile ("^(.*)://(.*)/v2/(.*)/manifests/(.*)" )
70
78
@@ -77,20 +85,34 @@ type Preheat interface {
77
85
// preheat is an implementation of Preheat.
78
86
type preheat struct {
79
87
job * internaljob.Job
80
- registryTimeout time.Duration
81
- rootCAs * x509.CertPool
82
88
certificateChain [][]byte
83
89
insecureSkipVerify bool
90
+ httpClient * http.Client
84
91
}
85
92
86
93
// newPreheat creates a new Preheat.
87
94
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
+
89
111
if rootCAs != nil {
90
- certificateChain = rootCAs .Subjects ()
112
+ p . certificateChain = rootCAs .Subjects ()
91
113
}
92
114
93
- return & preheat { job , registryTimeout , rootCAs , certificateChain , insecureSkipVerify } , nil
115
+ return p , nil
94
116
}
95
117
96
118
// CreatePreheat creates a preheat job.
@@ -191,26 +213,20 @@ func (p *preheat) getImageLayers(ctx context.Context, args types.PreheatArgs) ([
191
213
return nil , err
192
214
}
193
215
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 ),
202
218
withBasicAuth (args .Username , args .Password ),
203
219
}
204
220
// 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.
206
222
header := nethttp .MapToHeader (args .Headers )
207
223
if token := header .Get ("Authorization" ); len (token ) > 0 {
208
- opts = append (opts , withIssuedToken (token ))
224
+ options = append (options , withIssuedToken (token ))
209
225
header .Set ("Authorization" , token )
210
226
}
211
227
212
228
// Init docker auth client.
213
- client , err := newImageAuthClient (image , opts ... )
229
+ client , err := newImageAuthClient (image , options ... )
214
230
if err != nil {
215
231
return nil , err
216
232
}
@@ -395,8 +411,11 @@ type imageAuthClient struct {
395
411
396
412
// newImageAuthClient creates a new imageAuthClient.
397
413
func newImageAuthClient (image * preheatImage , opts ... imageAuthClientOption ) (* imageAuthClient , error ) {
414
+ httpClient := http .DefaultClient
415
+ httpClient .Transport = defaultHTTPTransport
416
+
398
417
d := & imageAuthClient {
399
- httpClient : http . DefaultClient ,
418
+ httpClient : httpClient ,
400
419
interceptorTokenHandler : newInterceptorTokenHandler (),
401
420
}
402
421
0 commit comments