From a6f4ab907b6c5bfd3c2131f1776d924821429783 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Sat, 22 Feb 2020 12:33:37 -0600 Subject: [PATCH] Use locks to support downloading archives in parallel Signed-off-by: Eric Bailey --- pkg/downloader/chart_downloader.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index 0013dbdf0..be252969e 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -16,6 +16,7 @@ limitations under the License. package downloader import ( + "context" "fmt" "io" "io/ioutil" @@ -23,7 +24,9 @@ import ( "os" "path/filepath" "strings" + "time" + "github.com/gofrs/flock" "github.com/pkg/errors" "helm.sh/helm/v3/internal/urlutil" @@ -114,6 +117,20 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven return "", nil, err } + name := filepath.Base(u.Path) + destfile := filepath.Join(dest, name) + + // Acquire a file lock for process synchronization + fileLock := flock.New(destfile) + lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + locked, err := fileLock.TryLockContext(lockCtx, time.Second) + if err != nil { + return "", nil, err + } else if locked { + defer fileLock.Unlock() + } + g, err := c.Getters.ByScheme(u.Scheme) if err != nil { return "", nil, err @@ -124,8 +141,6 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven return "", nil, err } - name := filepath.Base(u.Path) - destfile := filepath.Join(dest, name) if err := atomicWriteFile(destfile, data, 0644); err != nil { return destfile, nil, err }