Skip to content

Commit bd91014

Browse files
committed
Simplify GCloud upload code
1 parent 4f5697d commit bd91014

File tree

1 file changed

+17
-28
lines changed
  • components/content-service/pkg/storage

1 file changed

+17
-28
lines changed

components/content-service/pkg/storage/gcloud.go

+17-28
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"path/filepath"
1616
"sort"
1717
"strings"
18-
"sync"
1918
"time"
2019

2120
gcpstorage "cloud.google.com/go/storage"
@@ -366,39 +365,29 @@ func (rs *DirectGCPStorage) Upload(ctx context.Context, source string, name stri
366365
firstBackup = true
367366
}
368367

369-
var wg sync.WaitGroup
370368
var written int64
371369

372-
wg.Add(1)
370+
wc := obj.NewWriter(ctx)
371+
wc.Metadata = options.Annotations
372+
wc.ContentType = options.ContentType
373+
// Increase chunk size for faster uploading
374+
wc.ChunkSize = googleapi.DefaultUploadChunkSize * 4
373375

374-
go func() {
375-
defer wg.Done()
376-
377-
wc := obj.NewWriter(ctx)
378-
wc.Metadata = options.Annotations
379-
wc.ContentType = options.ContentType
380-
// Increase chunk size for faster uploading
381-
wc.ChunkSize = googleapi.DefaultUploadChunkSize * 4
382-
383-
written, err = io.Copy(wc, sfn)
384-
if err != nil {
385-
log.WithError(err).WithField("name", name).Error("Error while uploading file")
386-
return
387-
}
388-
389-
// persist changes in GCS
390-
err = wc.Close()
391-
if err != nil {
392-
log.WithError(err).WithField("name", name).Error("Error while uploading file")
393-
return
394-
}
395-
}()
376+
written, err = io.Copy(wc, sfn)
377+
if err != nil {
378+
log.WithError(err).WithField("name", name).Error("Error while uploading file")
379+
return "", "", err
380+
}
396381

397-
wg.Wait()
382+
// persist changes in GCS
383+
err = wc.Close()
384+
if err != nil {
385+
log.WithError(err).WithField("name", name).Error("Error while uploading file")
386+
return "", "", err
387+
}
398388

399389
if written != totalSize {
400-
err = xerrors.Errorf("Wrote fewer bytes than it should have, %d instead of %d", written, totalSize)
401-
return
390+
return "", "", xerrors.Errorf("Wrote fewer bytes than it should have, %d instead of %d", written, totalSize)
402391
}
403392

404393
// maintain backup trail if we're asked to - we do this prior to overwriting the regular backup file

0 commit comments

Comments
 (0)