Skip to content

Commit babaceb

Browse files
dmcgowank8s-infra-cherrypick-robot
authored andcommitted
Fix fetch always adding range to requests
Add condition for adding range header to avoid unnecessary header when parallel pull is not being used. Some registries do not properly handle the range header. Signed-off-by: Derek McGowan <[email protected]>
1 parent 64ed272 commit babaceb

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

core/remotes/docker/fetcher.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
442442

443443
chunkSize := int64(r.performances.ConcurrentLayerFetchBuffer)
444444
parallelism := int64(r.performances.MaxConcurrentDownloads)
445-
if chunkSize < minChunkSize {
445+
if chunkSize < minChunkSize || req.body != nil {
446446
parallelism = 1
447447
}
448448
log.G(ctx).WithField("initial_parallelism", r.performances.MaxConcurrentDownloads).
@@ -452,7 +452,9 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
452452
Debug("fetching layer")
453453
req.setMediaType(mediatype)
454454
req.header.Set("Accept-Encoding", "zstd;q=1.0, gzip;q=0.8, deflate;q=0.5")
455-
req.setOffset(offset)
455+
if parallelism > 1 || offset > 0 {
456+
req.setOffset(offset)
457+
}
456458

457459
if err := r.Acquire(ctx, 1); err != nil {
458460
return nil, err
@@ -478,7 +480,7 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
478480
})
479481

480482
remaining, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 0)
481-
if parallelism > 1 && req.body == nil {
483+
if parallelism > 1 {
482484
// If we have a content length, we can use multiple requests to fetch
483485
// the content in parallel. This will make download of bigger bodies
484486
// faster, at the cost of parallelism more requests and max

core/remotes/docker/fetcher_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func TestFetcherOpen(t *testing.T) {
5050
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
5151
if start > 0 {
5252
rw.Header().Set("content-range", fmt.Sprintf("bytes %d-127/128", start))
53+
} else if r.Header.Get("Range") == "bytes=0-" {
54+
// Simulate registries which do not support range requests
55+
rw.WriteHeader(http.StatusBadRequest)
56+
return
5357
}
5458
rw.Header().Set("content-length", strconv.Itoa(len(content[start:])))
5559
_, _ = rw.Write(content[start:])

0 commit comments

Comments
 (0)