From bbf2362c9a0672705e57ad20f1fda470d90aaea7 Mon Sep 17 00:00:00 2001 From: Intizar Date: Wed, 10 Jul 2024 14:05:48 +0900 Subject: [PATCH] remove ctx from pool struct --- node/pkg/utils/pool/pool.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/node/pkg/utils/pool/pool.go b/node/pkg/utils/pool/pool.go index f2b40ce00..a4f92a8b4 100644 --- a/node/pkg/utils/pool/pool.go +++ b/node/pkg/utils/pool/pool.go @@ -7,7 +7,6 @@ import ( type Pool struct { jobChannel chan func() workerCount int - ctx context.Context Cancel context.CancelFunc IsRunning bool } @@ -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 }