1
0
mirror of https://github.com/distribution/distribution synced 2024-11-12 05:45:51 +01:00

Merge pull request #2905 from 2opremio/master

Honor contexts passed to registry client methods
This commit is contained in:
João Pereira 2021-02-06 15:25:03 +00:00 committed by GitHub
commit 038a5060de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 12 deletions

@ -98,7 +98,11 @@ func (r *registry) Repositories(ctx context.Context, entries []string, last stri
return 0, err return 0, err
} }
resp, err := r.client.Get(u) req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil {
return 0, err
}
resp, err := r.client.Do(req)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -214,7 +218,11 @@ func (t *tags) All(ctx context.Context) ([]string, error) {
} }
for { for {
resp, err := t.client.Get(listURL.String()) req, err := http.NewRequestWithContext(ctx, "GET", listURL.String(), nil)
if err != nil {
return nil, err
}
resp, err := t.client.Do(req)
if err != nil { if err != nil {
return tags, err return tags, err
} }
@ -307,7 +315,7 @@ func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, er
} }
newRequest := func(method string) (*http.Response, error) { newRequest := func(method string) (*http.Response, error) {
req, err := http.NewRequest(method, u, nil) req, err := http.NewRequestWithContext(ctx, method, u, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -376,7 +384,11 @@ func (ms *manifests) Exists(ctx context.Context, dgst digest.Digest) (bool, erro
return false, err return false, err
} }
resp, err := ms.client.Head(u) req, err := http.NewRequestWithContext(ctx, "HEAD", u, nil)
if err != nil {
return false, err
}
resp, err := ms.client.Do(req)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -467,7 +479,7 @@ func (ms *manifests) Get(ctx context.Context, dgst digest.Digest, options ...dis
return nil, err return nil, err
} }
req, err := http.NewRequest("GET", u, nil) req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -552,7 +564,7 @@ func (ms *manifests) Put(ctx context.Context, m distribution.Manifest, options .
return "", err return "", err
} }
putRequest, err := http.NewRequest("PUT", manifestURL, bytes.NewReader(p)) putRequest, err := http.NewRequestWithContext(ctx, "PUT", manifestURL, bytes.NewReader(p))
if err != nil { if err != nil {
return "", err return "", err
} }
@ -587,7 +599,7 @@ func (ms *manifests) Delete(ctx context.Context, dgst digest.Digest) error {
if err != nil { if err != nil {
return err return err
} }
req, err := http.NewRequest("DELETE", u, nil) req, err := http.NewRequestWithContext(ctx, "DELETE", u, nil)
if err != nil { if err != nil {
return err return err
} }
@ -657,7 +669,7 @@ func (bs *blobs) Open(ctx context.Context, dgst digest.Digest) (distribution.Rea
return nil, err return nil, err
} }
return transport.NewHTTPReadSeeker(bs.client, blobURL, return transport.NewHTTPReadSeeker(ctx, bs.client, blobURL,
func(resp *http.Response) error { func(resp *http.Response) error {
if resp.StatusCode == http.StatusNotFound { if resp.StatusCode == http.StatusNotFound {
return distribution.ErrBlobUnknown return distribution.ErrBlobUnknown
@ -838,7 +850,11 @@ func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (distributi
return distribution.Descriptor{}, err return distribution.Descriptor{}, err
} }
resp, err := bs.client.Head(u) req, err := http.NewRequestWithContext(ctx, "HEAD", u, nil)
if err != nil {
return distribution.Descriptor{}, err
}
resp, err := bs.client.Do(req)
if err != nil { if err != nil {
return distribution.Descriptor{}, err return distribution.Descriptor{}, err
} }
@ -890,7 +906,7 @@ func (bs *blobStatter) Clear(ctx context.Context, dgst digest.Digest) error {
return err return err
} }
req, err := http.NewRequest("DELETE", blobURL, nil) req, err := http.NewRequestWithContext(ctx, "DELETE", blobURL, nil)
if err != nil { if err != nil {
return err return err
} }

@ -1,6 +1,7 @@
package transport package transport
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -28,8 +29,9 @@ type ReadSeekCloser interface {
// request. When seeking and starting a read from a non-zero offset // request. When seeking and starting a read from a non-zero offset
// the a "Range" header will be added which sets the offset. // the a "Range" header will be added which sets the offset.
// TODO(dmcgowan): Move this into a separate utility package // TODO(dmcgowan): Move this into a separate utility package
func NewHTTPReadSeeker(client *http.Client, url string, errorHandler func(*http.Response) error) ReadSeekCloser { func NewHTTPReadSeeker(ctx context.Context, client *http.Client, url string, errorHandler func(*http.Response) error) ReadSeekCloser {
return &httpReadSeeker{ return &httpReadSeeker{
ctx: ctx,
client: client, client: client,
url: url, url: url,
errorHandler: errorHandler, errorHandler: errorHandler,
@ -37,6 +39,7 @@ func NewHTTPReadSeeker(client *http.Client, url string, errorHandler func(*http.
} }
type httpReadSeeker struct { type httpReadSeeker struct {
ctx context.Context
client *http.Client client *http.Client
url string url string
@ -168,7 +171,7 @@ func (hrs *httpReadSeeker) reader() (io.Reader, error) {
return hrs.rc, nil return hrs.rc, nil
} }
req, err := http.NewRequest("GET", hrs.url, nil) req, err := http.NewRequestWithContext(hrs.ctx, "GET", hrs.url, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }