@@ -229,30 +229,23 @@ func New(options ...Option) http.RoundTripper {
229
229
// RoundTrip only process first redirect at present
230
230
func (rt * transport ) RoundTrip (req * http.Request ) (resp * http.Response , err error ) {
231
231
if rt .shouldUseDragonfly (req ) {
232
- // delete the Accept-Encoding header to avoid returning the same cached
233
- // result for different requests
234
- req .Header .Del ("Accept-Encoding" )
235
-
236
- ctx := req .Context ()
237
- if req .URL .Scheme == "https" {
238
- // for https, the trace info is in request header
239
- ctx = traceContext .Extract (req .Context (), propagation .HeaderCarrier (req .Header ))
232
+ resp , err = rt .roundTripWithDragonfly (req )
233
+ if err != nil {
234
+ metrics .ProxyErrorRequestViaDragonflyCount .Add (1 )
235
+ logger .With ("method" , req .Method , "url" , req .URL .String ()).Errorf ("round trip with dragonfly error: %s" , err )
236
+ return resp , err
240
237
}
241
-
242
- logger .Debugf ("round trip with dragonfly: %s" , req .URL .String ())
243
- metrics .ProxyRequestViaDragonflyCount .Add (1 )
244
- resp , err = rt .download (ctx , req )
245
238
} else {
246
239
logger .Debugf ("round trip directly, method: %s, url: %s" , req .Method , req .URL .String ())
247
240
req .Host = req .URL .Host
248
241
req .Header .Set ("Host" , req .Host )
249
242
metrics .ProxyRequestNotViaDragonflyCount .Add (1 )
250
243
resp , err = rt .baseRoundTripper .RoundTrip (req )
251
- }
252
244
253
- if err != nil {
254
- logger .With ("method" , req .Method , "url" , req .URL .String ()).Errorf ("round trip error: %s" , err )
255
- return resp , err
245
+ if err != nil {
246
+ logger .With ("method" , req .Method , "url" , req .URL .String ()).Errorf ("round trip directly error: %s" , err )
247
+ return resp , err
248
+ }
256
249
}
257
250
258
251
if resp .ContentLength > 0 {
@@ -263,6 +256,22 @@ func (rt *transport) RoundTrip(req *http.Request) (resp *http.Response, err erro
263
256
return resp , err
264
257
}
265
258
259
+ func (rt * transport ) roundTripWithDragonfly (req * http.Request ) (* http.Response , error ) {
260
+ // delete the Accept-Encoding header to avoid returning the same cached
261
+ // result for different requests
262
+ req .Header .Del ("Accept-Encoding" )
263
+
264
+ ctx := req .Context ()
265
+ if req .URL .Scheme == "https" {
266
+ // for https, the trace info is in request header
267
+ ctx = traceContext .Extract (req .Context (), propagation .HeaderCarrier (req .Header ))
268
+ }
269
+
270
+ logger .Debugf ("round trip with dragonfly: %s" , req .URL .String ())
271
+ metrics .ProxyRequestViaDragonflyCount .Add (1 )
272
+ return rt .download (ctx , req )
273
+ }
274
+
266
275
// NeedUseDragonfly is the default value for shouldUseDragonfly, which downloads all
267
276
// images layers with dragonfly.
268
277
func NeedUseDragonfly (req * http.Request ) bool {
0 commit comments