File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,16 +26,16 @@ type Pool struct {
2626var ErrPoolClosed = errors .New ("Pool has been closed." )
2727
2828// New creates a pool that manages resources. A pool requires a
29- // function that can allocate a new resource and the number of
30- // resources that can be allocated .
31- func New (fn func () (io.Closer , error ), cap uint ) (* Pool , error ) {
32- if cap <= 0 {
33- return nil , errors .New ("Capacity value too small." )
29+ // function that can allocate a new resource and the size of
30+ // the pool .
31+ func New (fn func () (io.Closer , error ), size uint ) (* Pool , error ) {
32+ if size <= 0 {
33+ return nil , errors .New ("Size value too small." )
3434 }
3535
3636 return & Pool {
3737 factory : fn ,
38- resources : make (chan io.Closer , cap ),
38+ resources : make (chan io.Closer , size ),
3939 }, nil
4040}
4141
Original file line number Diff line number Diff line change 88package main
99
1010import (
11- "flag"
1211 "log"
1312 "time"
1413
1514 "github.com/goinaction/code/chapter7/patterns/runner"
1615)
1716
18- // flagSec is a command line flag to set the timeout in seconds .
19- var flagSec = flag . Int ( "ttl" , 3 , "timeout in seconds" )
17+ // timeout is the number of second the program has to finish .
18+ const timeout = 3 * time . Second
2019
2120// main is the entry point for the program.
2221func main () {
23- // Parse all command line flags.
24- flag .Parse ()
2522 log .Println ("Starting work." )
2623
2724 // Create a new timer value for this run.
28- r := runner .New (time . Duration ( * flagSec ) )
25+ r := runner .New (timeout )
2926
3027 // Add the tasks to be run and start running.
3128 r .Add (createTask (1 ), createTask (2 ), createTask (1 ))
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ func New(d time.Duration) *Runner {
3535 return & Runner {
3636 interrupt : make (chan os.Signal , 1 ),
3737 complete : make (chan error ),
38- timeout : time .After (d * time . Second ),
38+ timeout : time .After (d ),
3939 }
4040}
4141
@@ -76,7 +76,7 @@ func (r *Runner) run() error {
7676 for id , task := range r .tasks {
7777 // Check for an interrupt signal from the OS.
7878 if r .gotInterrupt () {
79- return errors .New ("Early Shutdown" )
79+ return errors .New ("Interrupt - Shutdown Program " )
8080 }
8181
8282 // Execute the registered task.
Original file line number Diff line number Diff line change @@ -28,16 +28,16 @@ type namePrinter struct {
2828 name string
2929}
3030
31- // Work implements the Worker interface.
32- func (m * namePrinter ) Work () {
31+ // Task implements the Worker interface.
32+ func (m * namePrinter ) Task () {
3333 fmt .Println (m .name )
3434 time .Sleep (time .Second )
3535}
3636
3737// main is the entry point for all Go programs.
3838func main () {
39- // Create a work value with 2 goroutines.
40- w := work .New (2 )
39+ // Create a work pool with 2 goroutines.
40+ p := work .New (2 )
4141
4242 var wg sync.WaitGroup
4343 wg .Add (100 * len (names ))
@@ -54,15 +54,15 @@ func main() {
5454 go func () {
5555 // Submit the task to be worked on. When RunTask
5656 // returns we know it is being handled.
57- w .Run (& np )
57+ p .Run (& np )
5858 wg .Done ()
5959 }()
6060 }
6161 }
6262
6363 wg .Wait ()
6464
65- // Shutdown the work and wait for all existing work
65+ // Shutdown the work pool and wait for all existing work
6666 // to be completed.
67- w .Shutdown ()
67+ p .Shutdown ()
6868}
Original file line number Diff line number Diff line change @@ -7,44 +7,44 @@ package work
77import "sync"
88
99// Worker must be implemented by types that want to use
10- // this worker processes .
10+ // the work pool .
1111type Worker interface {
12- Work ()
12+ Task ()
1313}
1414
15- // Work provides a pool of goroutines that can execute any Worker
15+ // Pool provides a pool of goroutines that can execute any Worker
1616// tasks that are submitted.
17- type Work struct {
18- tasks chan Worker
19- wg sync.WaitGroup
17+ type Pool struct {
18+ work chan Worker
19+ wg sync.WaitGroup
2020}
2121
22- // New creates a new Worker .
23- func New (goroutines int ) * Work {
24- w := Work {
25- tasks : make (chan Worker ),
22+ // New creates a new work pool .
23+ func New (goroutines int ) * Pool {
24+ p := Pool {
25+ work : make (chan Worker ),
2626 }
2727
28- w .wg .Add (goroutines )
28+ p .wg .Add (goroutines )
2929 for i := 0 ; i < goroutines ; i ++ {
3030 go func () {
31- for task := range w . tasks {
32- task . Work ()
31+ for w := range p . work {
32+ w . Task ()
3333 }
34- w .wg .Done ()
34+ p .wg .Done ()
3535 }()
3636 }
3737
38- return & w
38+ return & p
3939}
4040
4141// Run submits work to the pool.
42- func (w * Work ) Run (task Worker ) {
43- w . tasks <- task
42+ func (p * Pool ) Run (w Worker ) {
43+ p . work <- w
4444}
4545
4646// Shutdown waits for all the goroutines to shutdown.
47- func (w * Work ) Shutdown () {
48- close (w . tasks )
49- w .wg .Wait ()
47+ func (p * Pool ) Shutdown () {
48+ close (p . work )
49+ p .wg .Wait ()
5050}
You can’t perform that action at this time.
0 commit comments