mirror of
https://github.com/restic/restic.git
synced 2026-11-23 13:52:39 +00:00
repository: add BlobBufferPool to BlobSaverAsync
This commit is contained in:
1 parent
3446754c41
commit
4718be1aee
4 files changed
+28
No files matched your search
@@ -500,6 +500,10 @@ func (repo *blobCountingSaver) SaveBlobAsync(ctx context.Context, t restic.BlobT
|
||||
})
|
||||
}
|
||||
|
||||
func (repo *blobCountingSaver) BlobBufferPool() *restic.BlobBufferPool {
|
||||
return repo.saver.BlobBufferPool()
|
||||
}
|
||||
|
||||
func appendToFile(t testing.TB, filename string, data []byte) {
|
||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
@@ -2368,6 +2372,10 @@ func (f *failSaveSaver) SaveBlobAsync(ctx context.Context, t restic.BlobType, bu
|
||||
})
|
||||
}
|
||||
|
||||
func (f *failSaveSaver) BlobBufferPool() *restic.BlobBufferPool {
|
||||
return f.saver.BlobBufferPool()
|
||||
}
|
||||
|
||||
func TestArchiverAbortEarlyOnError(t *testing.T) {
|
||||
var tests = []struct {
|
||||
src TestDir
|
||||
|
||||
@@ -19,6 +19,10 @@ type mockSaver struct {
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func (m *mockSaver) BlobBufferPool() *restic.BlobBufferPool {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockSaver) SaveBlobAsync(_ context.Context, _ restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool, cb func(newID restic.ID, known bool, sizeInRepo int, err error)) {
|
||||
// Fake async operation
|
||||
go func() {
|
||||
|
||||
@@ -54,6 +54,8 @@ type Repository struct {
|
||||
|
||||
zeroChunkOnce sync.Once
|
||||
zeroChunkID restic.ID
|
||||
|
||||
blobBuffers *restic.BlobBufferPool
|
||||
}
|
||||
|
||||
// internalRepository allows using SaveUnpacked and RemoveUnpacked with all FileTypes
|
||||
@@ -568,9 +570,18 @@ func (r *Repository) removeUnpacked(ctx context.Context, t restic.FileType, id r
|
||||
return r.be.Remove(ctx, backend.Handle{Type: backend.FileType(t), Name: id.String()})
|
||||
}
|
||||
|
||||
func (r *Repository) blobBufferCapacity() int {
|
||||
// add 1kb to account for overhead from crypto / compression
|
||||
return chunker.MaxSize + 1024
|
||||
}
|
||||
|
||||
func (r *Repository) WithBlobUploader(ctx context.Context, fn func(ctx context.Context, uploader restic.BlobSaverWithAsync) error) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
buffers := restic.NewBlobBufferPool(r.blobBufferCapacity())
|
||||
r.blobBuffers = buffers
|
||||
defer func() { r.blobBuffers = nil }()
|
||||
|
||||
wg, ctx := errgroup.WithContext(ctx)
|
||||
// pack uploader + wg.Go below + blob saver (CPU bound)
|
||||
wg.SetLimit(2 + runtime.GOMAXPROCS(0))
|
||||
@@ -621,6 +632,10 @@ type blobSaverRepo struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func (r *blobSaverRepo) BlobBufferPool() *restic.BlobBufferPool {
|
||||
return r.repo.blobBuffers
|
||||
}
|
||||
|
||||
func (r *blobSaverRepo) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool) (newID restic.ID, known bool, size int, err error) {
|
||||
return r.repo.saveBlob(ctx, t, buf, id, storeDuplicate)
|
||||
}
|
||||
|
||||
@@ -131,6 +131,7 @@ type BlobSaver interface {
|
||||
}
|
||||
|
||||
type BlobSaverAsync interface {
|
||||
BlobBufferPool() *BlobBufferPool
|
||||
// SaveBlobAsync saves a blob to the repository. ctx must be derived from the context created by WithBlobUploader.
|
||||
// The callback is called asynchronously from a different goroutine.
|
||||
SaveBlobAsync(ctx context.Context, tpe BlobType, buf []byte, id ID, storeDuplicate bool, cb func(newID ID, known bool, sizeInRepo int, err error))
|
||||
|
||||
Reference in new issue
Block a user