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) }