-
-
Save alexcrownus/472438942b3ba7fd3871dbe4303a256a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pool | |
type Task interface { | |
Execute(id int) | |
} | |
type Pool struct { | |
tasks chan Task | |
kill chan bool | |
} | |
func NewPool(size, queue int) *Pool { | |
pool := &Pool{ | |
tasks: make(chan Task, queue), | |
kill: make(chan bool), | |
} | |
for i := 0; i < size; i++ { | |
go pool.worker(i) | |
} | |
return pool | |
} | |
func (p *Pool) worker(id int) { | |
for { | |
select { | |
case task, ok := <-p.tasks: | |
if !ok { | |
return | |
} | |
task.Execute(id) | |
case <-p.kill: | |
// should really kill it | |
return | |
} | |
} | |
} | |
func (p *Pool) Close() { | |
close(p.tasks) | |
} | |
func (p *Pool) Exec(task Task) { | |
p.tasks <- task | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment