Skip to content

Commit

Permalink
Close channel after Stop has been called
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed Feb 1, 2019
1 parent ac5ee2a commit 09a63fb
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type pool struct {

// Delegate job to a workers
// will block if channel is full, you might want to wrap it with goroutine to avoid it
// will panic if called after Stop()
func (p *pool) Delegate(args ...interface{}) {
p.queue <- buildQueueValue(args)
}
Expand All @@ -35,6 +36,10 @@ func (p *pool) Start(maxWorkers int, fn interface{}) error {
return fmt.Errorf("%s is not a reflect.Func", reflect.TypeOf(fn))
}

if err := p.ctx.Err(); err != nil {
return err
}

for i := 1; i <= maxWorkers; i++ {
h := reflect.ValueOf(fn)

Expand All @@ -55,6 +60,7 @@ func (p *pool) Start(maxWorkers int, fn interface{}) error {

// Stop all workers
func (p *pool) Stop() {
defer close(p.queue)
p.cancel()
}

Expand Down

0 comments on commit 09a63fb

Please sign in to comment.