repository: add BlobBufferPool to BlobSaverAsync

This commit is contained in:
Michael Eischer committed 2026-11-22 17:54:09 +01:00
1 parent 3446754c41
commit 4718be1aee
4 files changed
+28

No files matched your search

+8
View File
@@ -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) { func appendToFile(t testing.TB, filename string, data []byte) {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil { 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) { func TestArchiverAbortEarlyOnError(t *testing.T) {
var tests = []struct { var tests = []struct {
src TestDir src TestDir
+4
View File
@@ -19,6 +19,10 @@ type mockSaver struct {
mutex sync.Mutex 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)) { 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 // Fake async operation
go func() { go func() {
+15
View File
@@ -54,6 +54,8 @@ type Repository struct {
zeroChunkOnce sync.Once zeroChunkOnce sync.Once
zeroChunkID restic.ID zeroChunkID restic.ID
blobBuffers *restic.BlobBufferPool
} }
// internalRepository allows using SaveUnpacked and RemoveUnpacked with all FileTypes // 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()}) 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 { func (r *Repository) WithBlobUploader(ctx context.Context, fn func(ctx context.Context, uploader restic.BlobSaverWithAsync) error) error {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
buffers := restic.NewBlobBufferPool(r.blobBufferCapacity())
r.blobBuffers = buffers
defer func() { r.blobBuffers = nil }()
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
// pack uploader + wg.Go below + blob saver (CPU bound) // pack uploader + wg.Go below + blob saver (CPU bound)
wg.SetLimit(2 + runtime.GOMAXPROCS(0)) wg.SetLimit(2 + runtime.GOMAXPROCS(0))
@@ -621,6 +632,10 @@ type blobSaverRepo struct {
repo *Repository 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) { 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) return r.repo.saveBlob(ctx, t, buf, id, storeDuplicate)
} }
+1
View File
@@ -131,6 +131,7 @@ type BlobSaver interface {
} }
type BlobSaverAsync interface { type BlobSaverAsync interface {
BlobBufferPool() *BlobBufferPool
// SaveBlobAsync saves a blob to the repository. ctx must be derived from the context created by WithBlobUploader. // 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. // 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)) SaveBlobAsync(ctx context.Context, tpe BlobType, buf []byte, id ID, storeDuplicate bool, cb func(newID ID, known bool, sizeInRepo int, err error))