Skip to content

Commit 464fb0d

Browse files
committed
Add wait group
1 parent ddbed3b commit 464fb0d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pkg/rsstorage/internal/chunks.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log/slog"
1212
"path/filepath"
1313
"strings"
14+
"sync"
1415
"time"
1516

1617
"github.com/rstudio/platform-lib/v3/pkg/rsstorage"
@@ -263,14 +264,30 @@ func (w *DefaultChunkUtils) readChunks(
263264
}
264265
}(writer)
265266

267+
var wg sync.WaitGroup
268+
errs := make(chan error)
269+
266270
totalBytesWritten := uint64(0)
267271

268272
for i := uint64(1); i <= numChunks; i++ {
269-
bytesRead, err = w.retryingChunkRead(ctx, i, address, chunkDir, complete, writer)
270-
if err != nil {
271-
return writer.CloseWithError(err)
273+
wg.Add(1)
274+
go func() {
275+
defer wg.Done()
276+
bytesRead, err = w.retryingChunkRead(ctx, i, address, chunkDir, complete, writer)
277+
if err != nil {
278+
errs <- writer.CloseWithError(err)
279+
return
280+
}
281+
totalBytesWritten += bytesRead
282+
}()
283+
}
284+
wg.Wait()
285+
close(errs)
286+
287+
for readErr := range errs {
288+
if readErr != nil {
289+
return readErr
272290
}
273-
totalBytesWritten += bytesRead
274291
}
275292
if totalBytesWritten != fileSize {
276293
err = fmt.Errorf("expected to read '%d' bytes from file but only read '%d' bytes", fileSize, totalBytesWritten)

0 commit comments

Comments
 (0)