Skip to content

Commit

Permalink
init context in constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Yi Jin <[email protected]>
  • Loading branch information
jnyi committed Apr 11, 2024
1 parent e21429d commit 4161673
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/pool/worker_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,27 @@ type WorkerPool interface {

type workerPool struct {
sync.Once
ctx context.Context
workCh chan Work
cancel context.CancelFunc
}

func NewWorkerPool(workers uint) WorkerPool {
ctx, cancel := context.WithCancel(context.Background())
return &workerPool{
ctx: ctx,
cancel: cancel,
workCh: make(chan Work, workers),
}
}

func (p *workerPool) Init() {
p.Do(func() {
ctx, cancel := context.WithCancel(context.Background())
p.cancel = cancel

for i := 0; i < p.Size(); i++ {
go func() {
for {
select {
case <-ctx.Done():
case <-p.ctx.Done():
// TODO: exhaust workCh before exit
return
case work := <-p.workCh:
Expand Down

0 comments on commit 4161673

Please sign in to comment.