2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 14:17:42 +00:00

Implement RetryBackend.List()

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
This commit is contained in:
Igor Fedorenko 2018-01-24 10:25:40 -05:00
parent c40002246d
commit aa333f4d49
4 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,3 @@
Enhancement: Retry Backend.List() in case of errors
https://github.com/restic/restic/pull/1579

View File

@ -128,3 +128,17 @@ func (be *RetryBackend) Test(ctx context.Context, h restic.Handle) (exists bool,
}) })
return exists, err return exists, err
} }
// List runs fn for each file in the backend which has the type t.
func (be *RetryBackend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
listed := make(map[string]struct{})
return be.retry(ctx, fmt.Sprintf("List(%v)", t), func() error {
return be.Backend.List(ctx, t, func(fi restic.FileInfo) error {
if _, ok := listed[fi.Name]; ok {
return nil
}
listed[fi.Name] = struct{}{}
return fn(fi)
})
})
}

View File

@ -88,3 +88,38 @@ func TestBackendSaveRetry(t *testing.T) {
t.Errorf("wrong data written to backend") t.Errorf("wrong data written to backend")
} }
} }
func TestBackendListRetry(t *testing.T) {
const (
ID1 = "id1"
ID2 = "id2"
)
retry := 0
be := &mock.Backend{
ListFn: func(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
// fail during first retry, succeed during second
retry++
if retry == 1 {
fn(restic.FileInfo{Name: ID1})
return errors.New("test list error")
}
fn(restic.FileInfo{Name: ID1})
fn(restic.FileInfo{Name: ID2})
return nil
},
}
retryBackend := RetryBackend{
Backend: be,
}
var listed []string
err := retryBackend.List(context.TODO(), restic.DataFile, func(fi restic.FileInfo) error {
listed = append(listed, fi.Name)
return nil
})
test.OK(t, err) // assert overall success
test.Equals(t, 2, retry) // assert retried once
test.Equals(t, []string{ID1, ID2}, listed) // assert no duplicate files
}

View File

@ -35,6 +35,9 @@ type Backend interface {
// List runs fn for each file in the backend which has the type t. When an // List runs fn for each file in the backend which has the type t. When an
// error occurs (or fn returns an error), List stops and returns it. // error occurs (or fn returns an error), List stops and returns it.
// //
// The function fn is called exactly once for each file during successful
// execution and at most once in case of an error.
//
// The function fn is called in the same Goroutine that List() is called // The function fn is called in the same Goroutine that List() is called
// from. // from.
List(ctx context.Context, t FileType, fn func(FileInfo) error) error List(ctx context.Context, t FileType, fn func(FileInfo) error) error