Skip to content

Commit 09a63fb

Browse files
committed
Close channel after Stop has been called
1 parent ac5ee2a commit 09a63fb

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pool.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type pool struct {
2121

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

39+
if err := p.ctx.Err(); err != nil {
40+
return err
41+
}
42+
3843
for i := 1; i <= maxWorkers; i++ {
3944
h := reflect.ValueOf(fn)
4045

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

5661
// Stop all workers
5762
func (p *pool) Stop() {
63+
defer close(p.queue)
5864
p.cancel()
5965
}
6066

0 commit comments

Comments
 (0)