From aa152680ba745ccdc85e606d347684587f463b9b Mon Sep 17 00:00:00 2001 From: Daniel Hu Date: Fri, 14 Jul 2023 15:10:18 +0800 Subject: [PATCH] Implement worker execution and task handling - In pool.go, added a call to the worker's run method after submitting a task to it in the Submit method. - In worker.go, implemented the run method for the Worker struct. This method starts a goroutine that continuously receives tasks from the worker's task channel, executes them, pushes the worker back to the pool, and decrements the pool's running count. Signed-off-by: Daniel Hu --- pool.go | 1 + worker.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/pool.go b/pool.go index 019be07..02721cf 100644 --- a/pool.go +++ b/pool.go @@ -28,6 +28,7 @@ func (p *Pool) Submit(task Task) { } } worker.task <- task + worker.run() p.running++ } else { // TODO: handle the case when the pool is full diff --git a/worker.go b/worker.go index 9638b7e..76cce6a 100644 --- a/worker.go +++ b/worker.go @@ -15,6 +15,21 @@ type Worker struct { lastUsedTime time.Time } +func (w *Worker) run() { + go func() { + for task := range w.task { + if task == nil { + return + } + task() + w.pool.lock.Lock() + w.pool.workers.Push(w) + w.pool.running-- + w.pool.lock.Unlock() + } + }() +} + type WorkerStack interface { Push(w *Worker) Pop() *Worker