mirror of
https://github.com/nxshock/gwp.git
synced 2024-11-27 03:31:02 +05:00
Add better naming for channels
This commit is contained in:
parent
267c3604e6
commit
6bcb8d3356
20
gwp.go
20
gwp.go
@ -10,8 +10,8 @@ import (
|
|||||||
|
|
||||||
// WorkerPool represents pool of workers.
|
// WorkerPool represents pool of workers.
|
||||||
type WorkerPool struct {
|
type WorkerPool struct {
|
||||||
f chan func() error
|
jobChan chan func() error
|
||||||
r chan error
|
resultChan chan error
|
||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
EstimateCount int
|
EstimateCount int
|
||||||
@ -29,8 +29,8 @@ func New(threadCount int) *WorkerPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
workerPool := &WorkerPool{
|
workerPool := &WorkerPool{
|
||||||
f: make(chan func() error),
|
jobChan: make(chan func() error),
|
||||||
r: make(chan error),
|
resultChan: make(chan error),
|
||||||
stopChan: make(chan struct{})}
|
stopChan: make(chan struct{})}
|
||||||
|
|
||||||
workerPool.wg.Add(threadCount)
|
workerPool.wg.Add(threadCount)
|
||||||
@ -55,7 +55,7 @@ func New(threadCount int) *WorkerPool {
|
|||||||
workerPool.currentSpeed = float64(workerPool.processedCount-prevPos) * float64(time.Second) / float64(time.Now().Sub(prevTime))
|
workerPool.currentSpeed = float64(workerPool.processedCount-prevPos) * float64(time.Second) / float64(time.Now().Sub(prevTime))
|
||||||
prevPos = workerPool.processedCount
|
prevPos = workerPool.processedCount
|
||||||
prevTime = time.Now()
|
prevTime = time.Now()
|
||||||
case err := <-workerPool.r:
|
case err := <-workerPool.resultChan:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
workerPool.errorCount++
|
workerPool.errorCount++
|
||||||
}
|
}
|
||||||
@ -70,8 +70,8 @@ func New(threadCount int) *WorkerPool {
|
|||||||
go func() {
|
go func() {
|
||||||
defer workerPool.wg.Done()
|
defer workerPool.wg.Done()
|
||||||
|
|
||||||
for f := range workerPool.f {
|
for f := range workerPool.jobChan {
|
||||||
workerPool.r <- f()
|
workerPool.resultChan <- f()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -101,15 +101,15 @@ func (workerPool *WorkerPool) printProgress() {
|
|||||||
|
|
||||||
// Add sends specified task for execution.
|
// Add sends specified task for execution.
|
||||||
func (workerPool *WorkerPool) Add(f func() error) {
|
func (workerPool *WorkerPool) Add(f func() error) {
|
||||||
workerPool.f <- f
|
workerPool.jobChan <- f
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseAndWait stops accepting tasks and waits for all tasks to complete.
|
// CloseAndWait stops accepting tasks and waits for all tasks to complete.
|
||||||
func (workerPool *WorkerPool) CloseAndWait() {
|
func (workerPool *WorkerPool) CloseAndWait() {
|
||||||
close(workerPool.f)
|
close(workerPool.jobChan)
|
||||||
workerPool.wg.Wait()
|
workerPool.wg.Wait()
|
||||||
workerPool.stopChan <- struct{}{}
|
workerPool.stopChan <- struct{}{}
|
||||||
close(workerPool.r)
|
close(workerPool.resultChan)
|
||||||
|
|
||||||
workerPool.printProgress()
|
workerPool.printProgress()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user