From d566905d7a45f401c31915cb0ec678a012746b36 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 31 May 2018 12:38:22 -0700 Subject: [PATCH] Properly shutdown parallel copy in cp command (#2448) --- cmd/cp-main.go | 75 ++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 6e3d320e..3ffd5c3b 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -315,50 +315,59 @@ func doCopySession(session *sessionV8) error { pg = newAccounter(session.Header.TotalBytes) } + var quitCh = make(chan struct{}) var statusCh = make(chan URLs) parallel, queueCh := newParallelManager(statusCh, pg) - stopParallel := func() { - close(queueCh) - parallel.wait() - } go func() { - // Loop through all urls. - for urlScanner.Scan() { - var cpURLs URLs - // Unmarshal copyURLs from each line. - json.Unmarshal([]byte(urlScanner.Text()), &cpURLs) + gracefulStop := func() { + close(queueCh) + parallel.wait() + close(statusCh) + } - // Save total count. - cpURLs.TotalCount = session.Header.TotalObjects - - // Save totalSize. - cpURLs.TotalSize = session.Header.TotalBytes - - // Check and handle storage class if passed in command line args - if _, ok := session.Header.CommandStringFlags["storage-class"]; ok { - if cpURLs.TargetContent.Metadata == nil { - cpURLs.TargetContent.Metadata = make(map[string]string) + for { + select { + case <-quitCh: + gracefulStop() + return + default: + if !urlScanner.Scan() { + // No more entries, quit immediately + gracefulStop() + return } - cpURLs.TargetContent.Metadata["X-Amz-Storage-Class"] = session.Header.CommandStringFlags["storage-class"] - } + var cpURLs URLs + // Unmarshal copyURLs from each line. + json.Unmarshal([]byte(urlScanner.Text()), &cpURLs) - // Verify if previously copied, notify progress bar. - if isCopied(cpURLs.SourceContent.URL.String()) { - queueCh <- func() URLs { - return doCopyFake(cpURLs, pg) + // Save total count. + cpURLs.TotalCount = session.Header.TotalObjects + + // Save totalSize. + cpURLs.TotalSize = session.Header.TotalBytes + + // Check and handle storage class if passed in command line args + if _, ok := session.Header.CommandStringFlags["storage-class"]; ok { + if cpURLs.TargetContent.Metadata == nil { + cpURLs.TargetContent.Metadata = make(map[string]string) + } + cpURLs.TargetContent.Metadata["X-Amz-Storage-Class"] = session.Header.CommandStringFlags["storage-class"] } - } else { - queueCh <- func() URLs { - return doCopy(ctx, cpURLs, pg) + + // Verify if previously copied, notify progress bar. + if isCopied(cpURLs.SourceContent.URL.String()) { + queueCh <- func() URLs { + return doCopyFake(cpURLs, pg) + } + } else { + queueCh <- func() URLs { + return doCopy(ctx, cpURLs, pg) + } } } } - - stopParallel() - close(statusCh) - }() var retErr error @@ -367,7 +376,7 @@ loop: for { select { case <-trapCh: - stopParallel() + quitCh <- struct{}{} cancelCopy() // Receive interrupt notification. if !globalQuiet && !globalJSON {