From d069ee31b2acec66bca1dc29cd626679615c8873 Mon Sep 17 00:00:00 2001 From: George Armhold Date: Tue, 31 Oct 2017 08:01:43 -0400 Subject: [PATCH] GS backend: limit http concurrency in Save(), Stat(), Test(), Remove(), List() as per discussion in PR #1399 --- internal/backend/gs/gs.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/backend/gs/gs.go b/internal/backend/gs/gs.go index 1141497a1..ce02dc691 100644 --- a/internal/backend/gs/gs.go +++ b/internal/backend/gs/gs.go @@ -204,14 +204,15 @@ func (be *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err debug.Log("Save %v at %v", h, objName) + be.sem.GetToken() + // Check key does not already exist if _, err := be.service.Objects.Get(be.bucketName, objName).Do(); err == nil { debug.Log("%v already exists", h) + be.sem.ReleaseToken() return errors.New("key already exists") } - be.sem.GetToken() - debug.Log("InsertObject(%v, %v)", be.bucketName, objName) // Set chunk size to zero to disable resumable uploads. @@ -323,7 +324,10 @@ func (be *Backend) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInf objName := be.Filename(h) + be.sem.GetToken() obj, err := be.service.Objects.Get(be.bucketName, objName).Do() + be.sem.ReleaseToken() + if err != nil { debug.Log("GetObject() err %v", err) return restic.FileInfo{}, errors.Wrap(err, "service.Objects.Get") @@ -336,7 +340,11 @@ func (be *Backend) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInf func (be *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) { found := false objName := be.Filename(h) + + be.sem.GetToken() _, err := be.service.Objects.Get(be.bucketName, objName).Do() + be.sem.ReleaseToken() + if err == nil { found = true } @@ -348,7 +356,10 @@ func (be *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) { func (be *Backend) Remove(ctx context.Context, h restic.Handle) error { objName := be.Filename(h) + be.sem.GetToken() err := be.service.Objects.Delete(be.bucketName, objName).Do() + be.sem.ReleaseToken() + if er, ok := err.(*googleapi.Error); ok { if er.Code == 404 { err = nil @@ -378,7 +389,10 @@ func (be *Backend) List(ctx context.Context, t restic.FileType) <-chan string { listReq := be.service.Objects.List(be.bucketName).Prefix(prefix).MaxResults(int64(be.listMaxItems)) for { + be.sem.GetToken() obj, err := listReq.Do() + be.sem.ReleaseToken() + if err != nil { fmt.Fprintf(os.Stderr, "error listing %v: %v\n", prefix, err) return