Simplify cp and sync routines - don't forget to close syncQueue channel :-)

This commit is contained in:
Harshavardhana
2015-06-13 20:05:03 -07:00
parent 9e3487fa56
commit 0935a7b3d3
2 changed files with 36 additions and 30 deletions
+18 -16
View File
@@ -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
+18 -14
View File
@@ -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 {