Skip to content

Commit

Permalink
Implement worker execution and task handling
Browse files Browse the repository at this point in the history
- 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 <tao.hu@merico.dev>
  • Loading branch information
daniel-hutao committed Jul 14, 2023
1 parent 8921e1d commit aa15268
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions pool.go
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions worker.go
Expand Up @@ -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
Expand Down

0 comments on commit aa15268

Please sign in to comment.