mirror of
https://github.com/octoleo/restic.git
synced 2025-01-03 07:12:28 +00:00
all: Drop ctxhttp
This package is no longer needed, since we can use the stdlib's http.NewRequestWithContext. backend/rclone already did, but it needed a different error check due to a difference between net/http and ctxhttp. Also, store the http.Client by value in the REST backend (changed to a pointer when ctxhttp was introduced) and use errors.WithStack instead of errors.Wrap where the message was no longer accurate. Errors from http.NewRequestWithContext will start with "net/http" or "net/url", so they're easy to identify.
This commit is contained in:
parent
1a6160d152
commit
d4aadfa389
@ -22,7 +22,6 @@ import (
|
|||||||
"github.com/restic/restic/internal/backend/rest"
|
"github.com/restic/restic/internal/backend/rest"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"golang.org/x/net/context/ctxhttp"
|
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -216,7 +215,7 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// send an HTTP request to the base URL, see if the server is there
|
// send an HTTP request to the base URL, see if the server is there
|
||||||
client := &http.Client{
|
client := http.Client{
|
||||||
Transport: debug.RoundTripper(tr),
|
Transport: debug.RoundTripper(tr),
|
||||||
Timeout: cfg.Timeout,
|
Timeout: cfg.Timeout,
|
||||||
}
|
}
|
||||||
@ -231,7 +230,7 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
|
|||||||
}
|
}
|
||||||
req.Header.Set("Accept", rest.ContentTypeV2)
|
req.Header.Set("Accept", rest.ContentTypeV2)
|
||||||
|
|
||||||
res, err := ctxhttp.Do(ctx, client, req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// ignore subsequent errors
|
// ignore subsequent errors
|
||||||
_ = bg()
|
_ = bg()
|
||||||
@ -240,7 +239,7 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
|
|||||||
// wait for rclone to exit
|
// wait for rclone to exit
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
// try to return the program exit code if communication with rclone has failed
|
// try to return the program exit code if communication with rclone has failed
|
||||||
if be.waitResult != nil && (err == context.Canceled || errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.EPIPE)) {
|
if be.waitResult != nil && (errors.Is(err, context.Canceled) || errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, syscall.EPIPE)) {
|
||||||
err = be.waitResult
|
err = be.waitResult
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,8 +14,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/net/context/ctxhttp"
|
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend"
|
"github.com/restic/restic/internal/backend"
|
||||||
"github.com/restic/restic/internal/backend/sema"
|
"github.com/restic/restic/internal/backend/sema"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
@ -33,7 +31,7 @@ type Backend struct {
|
|||||||
url *url.URL
|
url *url.URL
|
||||||
connections uint
|
connections uint
|
||||||
sem sema.Semaphore
|
sem sema.Semaphore
|
||||||
client *http.Client
|
client http.Client
|
||||||
backend.Layout
|
backend.Layout
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,8 +43,6 @@ const (
|
|||||||
|
|
||||||
// Open opens the REST backend with the given config.
|
// Open opens the REST backend with the given config.
|
||||||
func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
|
func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
|
||||||
client := &http.Client{Transport: rt}
|
|
||||||
|
|
||||||
sem, err := sema.New(cfg.Connections)
|
sem, err := sema.New(cfg.Connections)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -60,7 +56,7 @@ func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
|
|||||||
|
|
||||||
be := &Backend{
|
be := &Backend{
|
||||||
url: cfg.URL,
|
url: cfg.URL,
|
||||||
client: client,
|
client: http.Client{Transport: rt},
|
||||||
Layout: &backend.RESTLayout{URL: url, Join: path.Join},
|
Layout: &backend.RESTLayout{URL: url, Join: path.Join},
|
||||||
connections: cfg.Connections,
|
connections: cfg.Connections,
|
||||||
sem: sem,
|
sem: sem,
|
||||||
@ -138,9 +134,10 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// make sure that client.Post() cannot close the reader by wrapping it
|
// make sure that client.Post() cannot close the reader by wrapping it
|
||||||
req, err := http.NewRequest(http.MethodPost, b.Filename(h), ioutil.NopCloser(rd))
|
req, err := http.NewRequestWithContext(ctx,
|
||||||
|
http.MethodPost, b.Filename(h), ioutil.NopCloser(rd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "NewRequest")
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
req.Header.Set("Accept", ContentTypeV2)
|
req.Header.Set("Accept", ContentTypeV2)
|
||||||
@ -150,7 +147,7 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
|
|||||||
req.ContentLength = rd.Length()
|
req.ContentLength = rd.Length()
|
||||||
|
|
||||||
b.sem.GetToken()
|
b.sem.GetToken()
|
||||||
resp, err := ctxhttp.Do(ctx, b.client, req)
|
resp, err := b.client.Do(req)
|
||||||
b.sem.ReleaseToken()
|
b.sem.ReleaseToken()
|
||||||
|
|
||||||
var cerr error
|
var cerr error
|
||||||
@ -160,7 +157,7 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "client.Post")
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
@ -269,9 +266,9 @@ func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, o
|
|||||||
return nil, errors.Errorf("invalid length %d", length)
|
return nil, errors.Errorf("invalid length %d", length)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", b.Filename(h), nil)
|
req, err := http.NewRequestWithContext(ctx, "GET", b.Filename(h), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "http.NewRequest")
|
return nil, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
byteRange := fmt.Sprintf("bytes=%d-", offset)
|
byteRange := fmt.Sprintf("bytes=%d-", offset)
|
||||||
@ -283,7 +280,7 @@ func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, o
|
|||||||
debug.Log("Load(%v) send range %v", h, byteRange)
|
debug.Log("Load(%v) send range %v", h, byteRange)
|
||||||
|
|
||||||
b.sem.GetToken()
|
b.sem.GetToken()
|
||||||
resp, err := ctxhttp.Do(ctx, b.client, req)
|
resp, err := b.client.Do(req)
|
||||||
b.sem.ReleaseToken()
|
b.sem.ReleaseToken()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -321,17 +318,17 @@ func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, e
|
|||||||
return restic.FileInfo{}, backoff.Permanent(err)
|
return restic.FileInfo{}, backoff.Permanent(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodHead, b.Filename(h), nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodHead, b.Filename(h), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return restic.FileInfo{}, errors.Wrap(err, "NewRequest")
|
return restic.FileInfo{}, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", ContentTypeV2)
|
req.Header.Set("Accept", ContentTypeV2)
|
||||||
|
|
||||||
b.sem.GetToken()
|
b.sem.GetToken()
|
||||||
resp, err := ctxhttp.Do(ctx, b.client, req)
|
resp, err := b.client.Do(req)
|
||||||
b.sem.ReleaseToken()
|
b.sem.ReleaseToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return restic.FileInfo{}, errors.Wrap(err, "client.Head")
|
return restic.FileInfo{}, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = io.Copy(ioutil.Discard, resp.Body)
|
_, _ = io.Copy(ioutil.Discard, resp.Body)
|
||||||
@ -376,14 +373,14 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
|
|||||||
return backoff.Permanent(err)
|
return backoff.Permanent(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("DELETE", b.Filename(h), nil)
|
req, err := http.NewRequestWithContext(ctx, "DELETE", b.Filename(h), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "http.NewRequest")
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", ContentTypeV2)
|
req.Header.Set("Accept", ContentTypeV2)
|
||||||
|
|
||||||
b.sem.GetToken()
|
b.sem.GetToken()
|
||||||
resp, err := ctxhttp.Do(ctx, b.client, req)
|
resp, err := b.client.Do(req)
|
||||||
b.sem.ReleaseToken()
|
b.sem.ReleaseToken()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -415,14 +412,14 @@ func (b *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.Fi
|
|||||||
url += "/"
|
url += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "NewRequest")
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", ContentTypeV2)
|
req.Header.Set("Accept", ContentTypeV2)
|
||||||
|
|
||||||
b.sem.GetToken()
|
b.sem.GetToken()
|
||||||
resp, err := ctxhttp.Do(ctx, b.client, req)
|
resp, err := b.client.Do(req)
|
||||||
b.sem.ReleaseToken()
|
b.sem.ReleaseToken()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"golang.org/x/net/context/ctxhttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Release collects data about a single release on GitHub.
|
// Release collects data about a single release on GitHub.
|
||||||
@ -53,7 +52,7 @@ func GitHubLatestRelease(ctx context.Context, owner, repo string) (Release, erro
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)
|
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Release{}, err
|
return Release{}, err
|
||||||
}
|
}
|
||||||
@ -61,7 +60,7 @@ func GitHubLatestRelease(ctx context.Context, owner, repo string) (Release, erro
|
|||||||
// pin API version 3
|
// pin API version 3
|
||||||
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||||||
|
|
||||||
res, err := ctxhttp.Do(ctx, http.DefaultClient, req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Release{}, err
|
return Release{}, err
|
||||||
}
|
}
|
||||||
@ -112,7 +111,7 @@ func GitHubLatestRelease(ctx context.Context, owner, repo string) (Release, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getGithubData(ctx context.Context, url string) ([]byte, error) {
|
func getGithubData(ctx context.Context, url string) ([]byte, error) {
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -120,7 +119,7 @@ func getGithubData(ctx context.Context, url string) ([]byte, error) {
|
|||||||
// request binary data
|
// request binary data
|
||||||
req.Header.Set("Accept", "application/octet-stream")
|
req.Header.Set("Accept", "application/octet-stream")
|
||||||
|
|
||||||
res, err := ctxhttp.Do(ctx, http.DefaultClient, req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user