refactor: usefilebuffer on writter

This commit is contained in:
Urko 2023-08-06 16:40:01 +02:00
parent ec7b19808f
commit 88abec1d61
1 changed files with 10 additions and 10 deletions

View File

@ -123,6 +123,7 @@ func (b *BackBalze) Sync(ctx context.Context) error {
msg, err := copyFile(ctx, bc, src)
if err != nil {
log.Printf("error copying file %s: %v\n", src, err)
continue
}
msgsChan <- msg
}
@ -261,28 +262,27 @@ func (b *BackBalze) OldSync() error {
return nil
}
func copyFile(ctx context.Context, bucket *b2.Bucket, src string) (UploadMessage, error) {
func copyFile(ctx context.Context, bucket *b2.Bucket, src string) error {
f, err := os.Open(src)
if err != nil {
return UploadMessage{}, err
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return UploadMessage{}, err
return err
}
w := bucket.Object(fi.Name()).NewWriter(ctx)
w.ConcurrentUploads = writers
msg := UploadMessage{
key: fi.Name(),
startAt: time.Now(),
}
// w.ChunkSize = 1e9 / 2
w.UseFileBuffer = true
log.Println("start copying", fi.Name())
if _, err := io.Copy(w, f); err != nil {
w.Close()
return msg, err
return err
}
msg.endAt = time.Now()
return msg, w.Close()
log.Println("end copying", fi.Name())
return w.Close()
}
func cleanBucket(ctx context.Context, bucket *b2.Bucket, files []string) error {