Skip to content

Commit

Permalink
remove ctx from pool struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Jul 10, 2024
1 parent 03ed717 commit e472b54
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions node/pkg/utils/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
type Pool struct {
jobChannel chan func()
workerCount int
ctx context.Context
Cancel context.CancelFunc
IsRunning bool
}
Expand All @@ -21,31 +20,28 @@ func NewPool(workerCount int) *Pool {

func (p *Pool) Run(ctx context.Context) {
poolCtx, cancel := context.WithCancel(ctx)
p.ctx = poolCtx
p.Cancel = cancel
p.IsRunning = true

for i := 0; i < p.workerCount; i++ {
go p.worker()
go p.worker(poolCtx)
}
}

func (p *Pool) worker() {
func (p *Pool) worker(ctx context.Context) {
for {
select {
case job := <-p.jobChannel:
job()
case <-p.ctx.Done():
case <-ctx.Done():
return
}
}
}

func (p *Pool) AddJob(job func()) {
select {
case p.jobChannel <- job:
return
case <-p.ctx.Done():
if !p.IsRunning {
return
}
p.jobChannel <- job
}

0 comments on commit e472b54

Please sign in to comment.