From 0935a7b3d3a4fb44335d1aef15388b7f9723cb02 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 13 Jun 2015 20:04:57 -0700 Subject: [PATCH] Simplify cp and sync routines - don't forget to close syncQueue channel :-) --- cmd-cp.go | 34 ++++++++++++++++++---------------- cmd-sync.go | 32 ++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/cmd-cp.go b/cmd-cp.go index b51ac4fd..ae3b95c4 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -125,6 +125,20 @@ func doCopyInRoutine(cURLs cpURLs, bar *barSend, cpQueue chan bool, errCh chan e <-cpQueue // Signal that this copy routine is done. } +func doPrepareCopyURLs(sourceURLs []string, targetURL string, bar barSend, lock countlock.Locker) { + for cURLs := range prepareCopyURLs(sourceURLs, targetURL) { + if cURLs.Error != nil { + // no need to print errors here, any error here + // will be printed later during Copy() + continue + } + if !globalQuietFlag { + bar.Extend(cURLs.SourceContent.Size) + lock.Up() // Let copy routine know that it has to catch up. + } + } +} + func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error { errCh := make(chan error) go func(sourceURLs []string, targetURL string, bar barSend, errCh chan error) { @@ -136,27 +150,15 @@ func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error defer lock.Close() } - go func(sourceURLs []string, targetURL string, bar barSend, lock countlock.Locker) { - var cURLs cpURLs - for cURLs = range prepareCopyURLs(sourceURLs, targetURL) { - if cURLs.Error != nil { - // no need to print errors here, any error here - // will be printed later during Copy() - continue - } - if !globalQuietFlag { - bar.Extend(cURLs.SourceContent.Size) - lock.Up() // Let copy routine know that it has to catch up. - } - } - }(sourceURLs, targetURL, bar, lock) + // Wait for all copy routines to complete. + wg := new(sync.WaitGroup) // Pool limited copy routines in parallel. cpQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1))) defer close(cpQueue) - // Wait for all copy routines to complete. - wg := new(sync.WaitGroup) + go doPrepareCopyURLs(sourceURLs, targetURL, bar, lock) + for cURLs := range prepareCopyURLs(sourceURLs, targetURL) { if cURLs.Error != nil { errCh <- cURLs.Error diff --git a/cmd-sync.go b/cmd-sync.go index 39f09614..e15b7c00 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -109,6 +109,20 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, errCh chan error, <-syncQueue // Signal that this copy routine is done. } +func doPrepareSyncURLs(sourceURL string, targetURLs []string, bar barSend, lock countlock.Locker) { + for sURLs := range prepareSyncURLs(sourceURL, targetURLs) { + if sURLs.Error != nil { + // no need to print errors here, any error here + // will be printed later during Sync() + continue + } + if !globalQuietFlag { + bar.Extend(sURLs.SourceContent.Size) + lock.Up() // Let copy routine know that it has to catch up. + } + } +} + func doSyncCmd(sourceURL string, targetURLs []string, bar barSend) <-chan error { errCh := make(chan error) go func(sourceURL string, targetURLs []string, bar barSend, errCh chan error) { @@ -119,22 +133,12 @@ func doSyncCmd(sourceURL string, targetURLs []string, bar barSend) <-chan error lock = countlock.New() defer lock.Close() } - go func(sourceURL string, targetURLs []string) { - for sURLs := range prepareSyncURLs(sourceURL, targetURLs) { - if sURLs.Error != nil { - // no need to print errors here, any error here - // will be printed later during Sync() - continue - } - if !globalQuietFlag { - bar.Extend(sURLs.SourceContent.Size) - lock.Up() // Let copy routine know that it has to catch up. - } - } - }(sourceURL, targetURLs) - syncQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1))) wg := new(sync.WaitGroup) + syncQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1))) + defer close(syncQueue) + + go doPrepareSyncURLs(sourceURL, targetURLs, bar, lock) for sURLs := range prepareSyncURLs(sourceURL, targetURLs) { if sURLs.Error != nil {