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