From 4718be1aee1dbbb16446f982ea5542e604a26293 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 10 Jul 2026 17:48:10 +0200 Subject: [PATCH] repository: add BlobBufferPool to BlobSaverAsync --- internal/archiver/archiver_test.go | 8 ++++++++ internal/archiver/tree_saver_test.go | 4 ++++ internal/repository/repository.go | 15 +++++++++++++++ internal/restic/repository.go | 1 + 4 files changed, 28 insertions(+) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index 312009617..1c08e7699 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -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 diff --git a/internal/archiver/tree_saver_test.go b/internal/archiver/tree_saver_test.go index d3ba741c2..81c6024b6 100644 --- a/internal/archiver/tree_saver_test.go +++ b/internal/archiver/tree_saver_test.go @@ -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() { diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 0a3ca836d..0fc9edccf 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -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) } diff --git a/internal/restic/repository.go b/internal/restic/repository.go index 07f6c8a50..6cb7bc714 100644 --- a/internal/restic/repository.go +++ b/internal/restic/repository.go @@ -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))