From 6c2325129fc110cb475667a4844176f689d4e844 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Sat, 24 Oct 2020 22:02:35 +0100 Subject: [PATCH] Simply queueing to parallel manager (#3468) --- cmd/cp-main.go | 13 ++++++------- cmd/mirror-main.go | 29 +++++++++++++---------------- cmd/parallel-manager.go | 12 +++++++++--- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index 8fd988cf..afe1a58a 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -455,12 +455,11 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli. var quitCh = make(chan struct{}) var statusCh = make(chan URLs) - parallel, queueCh := newParallelManager(statusCh) + parallel := newParallelManager(statusCh) go func() { gracefulStop := func() { - close(queueCh) - parallel.wait() + parallel.stopAndWait() close(statusCh) } @@ -531,13 +530,13 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli. // Verify if previously copied, notify progress bar. if isCopied != nil && isCopied(cpURLs.SourceContent.URL.String()) { - queueCh <- func() URLs { + parallel.queueTask(func() URLs { return doCopyFake(ctx, cpURLs, pg, isMvCmd) - } + }) } else { - queueCh <- func() URLs { + parallel.queueTask(func() URLs { return doCopy(ctx, cpURLs, pg, encKeyDB, isMvCmd, preserve) - } + }) } } } diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index a3d12c67..774b6b99 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -202,7 +202,6 @@ type mirrorJob struct { // Hold operation status information status Status - queueCh chan func() URLs parallel *ParallelManager // channel for status messages @@ -479,9 +478,9 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) // to avoid copying it. continue } - mj.queueCh <- func() URLs { + mj.parallel.queueTask(func() URLs { return mj.doMirrorWatch(ctx, targetPath, tgtSSE, mirrorURL) - } + }) } else if event.Type == EventRemove { if strings.Contains(event.UserAgent, uaMirrorAppName) { continue @@ -498,9 +497,9 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo) mirrorURL.TotalCount = mj.status.GetCounts() mirrorURL.TotalSize = mj.status.Get() if mirrorURL.TargetContent != nil && (mj.opts.isRemove || mj.opts.activeActive) { - mj.queueCh <- func() URLs { + mj.parallel.queueTask(func() URLs { return mj.doRemove(ctx, mirrorURL) - } + }) } } } @@ -528,9 +527,9 @@ func (mj *mirrorJob) watchMirror(ctx context.Context, stopParallel func()) { return } if err != nil { - mj.queueCh <- func() URLs { + mj.parallel.queueTask(func() URLs { return URLs{Error: err} - } + }) } case <-globalContext.Done(): stopParallel() @@ -585,13 +584,13 @@ func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.Cance sURLs.TotalSize = mj.status.Get() if sURLs.SourceContent != nil { - mj.queueCh <- func() URLs { + mj.parallel.queueTask(func() URLs { return mj.doMirror(ctx, sURLs) - } + }) } else if sURLs.TargetContent != nil && mj.opts.isRemove { - mj.queueCh <- func() URLs { + mj.parallel.queueTask(func() URLs { return mj.doRemove(ctx, sURLs) - } + }) } case <-globalContext.Done(): stopParallel() @@ -614,8 +613,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc go func() { defer wg.Done() stopParallel := func() { - close(mj.queueCh) - mj.parallel.wait() + mj.parallel.stopAndWait() cancelMirror() } mj.watchMirror(ctx, stopParallel) @@ -628,8 +626,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc defer wg.Done() stopParallel := func() { if !mj.opts.isWatch { - close(mj.queueCh) - mj.parallel.wait() + mj.parallel.stopAndWait() cancelMirror() } } @@ -657,7 +654,7 @@ func newMirrorJob(srcURL, dstURL string, opts mirrorOptions) *mirrorJob { watcher: NewWatcher(UTCNow()), } - mj.parallel, mj.queueCh = newParallelManager(mj.statusCh) + mj.parallel = newParallelManager(mj.statusCh) // we'll define the status to use here, // do we want the quiet status? or the progressbar diff --git a/cmd/parallel-manager.go b/cmd/parallel-manager.go index f7955537..f3eba1e2 100644 --- a/cmd/parallel-manager.go +++ b/cmd/parallel-manager.go @@ -50,6 +50,7 @@ type ParallelManager struct { // Channel to receive tasks to run queueCh chan func() URLs + // Channel to send back results resultCh chan URLs @@ -134,14 +135,19 @@ func (p *ParallelManager) monitorProgress() { }() } +func (p *ParallelManager) queueTask(fn func() URLs) { + p.queueCh <- fn +} + // Wait for all workers to finish tasks before shutting down Parallel -func (p *ParallelManager) wait() { +func (p *ParallelManager) stopAndWait() { + close(p.queueCh) p.wg.Wait() close(p.stopMonitorCh) } // newParallelManager starts new workers waiting for executing tasks -func newParallelManager(resultCh chan URLs) (*ParallelManager, chan func() URLs) { +func newParallelManager(resultCh chan URLs) *ParallelManager { p := &ParallelManager{ wg: &sync.WaitGroup{}, workersNum: 0, @@ -158,5 +164,5 @@ func newParallelManager(resultCh chan URLs) (*ParallelManager, chan func() URLs) // Start monitoring tasks progress p.monitorProgress() - return p, p.queueCh + return p }