From 738b2a0445a751d80ce6b139500c8dfecb73af1f Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 15 Oct 2022 17:25:45 +0200 Subject: [PATCH] parallelize more List usages --- cmd/restic/cmd_debug.go | 6 +++++- cmd/restic/cmd_key.go | 6 +++++- internal/restic/lock.go | 9 +++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/restic/cmd_debug.go b/cmd/restic/cmd_debug.go index 4c1ffc383..413a90507 100644 --- a/cmd/restic/cmd_debug.go +++ b/cmd/restic/cmd_debug.go @@ -13,6 +13,7 @@ import ( "os" "runtime" "sort" + "sync" "time" "github.com/klauspost/compress/zstd" @@ -105,7 +106,8 @@ type Blob struct { func printPacks(ctx context.Context, repo *repository.Repository, wr io.Writer) error { - return repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error { + var m sync.Mutex + return restic.ParallelList(ctx, repo.Backend(), restic.PackFile, repo.Connections(), func(ctx context.Context, id restic.ID, size int64) error { blobs, _, err := repo.ListPack(ctx, id, size) if err != nil { Warnf("error for pack %v: %v\n", id.Str(), err) @@ -125,6 +127,8 @@ func printPacks(ctx context.Context, repo *repository.Repository, wr io.Writer) } } + m.Lock() + defer m.Unlock() return prettyPrintJSON(wr, p) }) } diff --git a/cmd/restic/cmd_key.go b/cmd/restic/cmd_key.go index a673e5756..bd2ee65cb 100644 --- a/cmd/restic/cmd_key.go +++ b/cmd/restic/cmd_key.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "strings" + "sync" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" @@ -56,9 +57,10 @@ func listKeys(ctx context.Context, s *repository.Repository, gopts GlobalOptions Created string `json:"created"` } + var m sync.Mutex var keys []keyInfo - err := s.List(ctx, restic.KeyFile, func(id restic.ID, size int64) error { + err := restic.ParallelList(ctx, s.Backend(), restic.KeyFile, s.Connections(), func(ctx context.Context, id restic.ID, size int64) error { k, err := repository.LoadKey(ctx, s, id) if err != nil { Warnf("LoadKey() failed: %v\n", err) @@ -73,6 +75,8 @@ func listKeys(ctx context.Context, s *repository.Repository, gopts GlobalOptions Created: k.Created.Local().Format(TimeFormat), } + m.Lock() + defer m.Unlock() keys = append(keys, key) return nil }) diff --git a/internal/restic/lock.go b/internal/restic/lock.go index 0f093ae1e..acccb1d22 100644 --- a/internal/restic/lock.go +++ b/internal/restic/lock.go @@ -7,6 +7,7 @@ import ( "os/signal" "os/user" "sync" + "sync/atomic" "syscall" "testing" "time" @@ -301,15 +302,15 @@ func RemoveStaleLocks(ctx context.Context, repo Repository) (uint, error) { // RemoveAllLocks removes all locks forcefully. func RemoveAllLocks(ctx context.Context, repo Repository) (uint, error) { - var processed uint - err := repo.List(ctx, LockFile, func(id ID, size int64) error { + var processed uint32 + err := ParallelList(ctx, repo.Backend(), LockFile, repo.Connections(), func(ctx context.Context, id ID, size int64) error { err := repo.Backend().Remove(ctx, Handle{Type: LockFile, Name: id.String()}) if err == nil { - processed++ + atomic.AddUint32(&processed, 1) } return err }) - return processed, err + return uint(processed), err } // ForAllLocks reads all locks in parallel and calls the given callback.