From 7c3de6635f0185f3075c5dc74a0f714ec88ea82b Mon Sep 17 00:00:00 2001 From: nxshock Date: Sat, 27 Mar 2021 10:52:23 +0500 Subject: [PATCH] Add option to show progress --- README.md | 3 ++- gwp.go | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b3cb64b..550c92f 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,8 @@ func f(i int) error { func main() { worker := gwp.New(4) // Create pool with specified number of workers - worker.EstimateCount = 100 // Set estimate jobs count count to show progress + worker.ShowProgress = true // Enable progress indicator + worker.EstimateCount = 100 // Set total number on jobs to calculate ETA for i := 0; i < 100; i++ { n := i diff --git a/gwp.go b/gwp.go index 32d7a01..8d459c1 100644 --- a/gwp.go +++ b/gwp.go @@ -10,11 +10,13 @@ import ( // WorkerPool represents pool of workers. type WorkerPool struct { - jobChan chan func() error - resultChan chan error - stopChan chan struct{} - wg sync.WaitGroup + jobChan chan func() error + resultChan chan error + stopChan chan struct{} + wg sync.WaitGroup + EstimateCount int + ShowProgress bool processedCount int // processed jobs count errorCount int // processed jobs count that returned error @@ -80,7 +82,7 @@ func New(threadCount int) *WorkerPool { } func (workerPool *WorkerPool) printProgress() { - if workerPool.EstimateCount == 0 { + if !workerPool.ShowProgress { return } @@ -92,7 +94,7 @@ func (workerPool *WorkerPool) printProgress() { fmt.Fprintf(os.Stderr, " Errors: %d (%.1f%%)", workerPool.errorCount, float64(workerPool.errorCount*100)/float64(workerPool.EstimateCount)) } - if workerPool.currentSpeed > 0 { + if workerPool.EstimateCount > 0 && workerPool.currentSpeed > 0 { fmt.Fprintf(os.Stderr, " ETA: %s at %.2f rps", time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed), workerPool.currentSpeed) }