Skip to content

Commit f9ecc46

Browse files
Better work pool pattern.
1 parent 2c38c3a commit f9ecc46

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎chapter7/patterns/work/main/main.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package main
88

99
import (
1010
"fmt"
11+
"log"
1112
"sync"
1213
"time"
1314

@@ -34,10 +35,16 @@ func (m *namePrinter) Work(id int) {
3435
time.Sleep(time.Second)
3536
}
3637

38+
// logger is called by the work pool when
39+
// it has things to log.
40+
func logger(message string) {
41+
log.Println(message)
42+
}
43+
3744
// main is the entry point for all Go programs.
3845
func main() {
3946
// Create a work value with 2 goroutines.
40-
w, _ := work.New(2, time.Minute)
47+
w, _ := work.New(2, time.Second, logger)
4148

4249
var wg sync.WaitGroup
4350
wg.Add(100 * len(names))

‎chapter7/patterns/work/work.go‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package work
66
import (
77
"errors"
88
"fmt"
9-
"log"
109
"sync"
1110
"sync/atomic"
1211
"time"
@@ -43,10 +42,12 @@ type Work struct {
4342
routines int64 // Number of routines
4443
active int64 // Active number of routines in the work pool.
4544
pending int64 // Pending number of routines waiting to submit work.
45+
46+
logFunc func(message string) // Function called to providing logging support
4647
}
4748

4849
// New creates a new Worker.
49-
func New(minRoutines int, statTime time.Duration) (*Work, error) {
50+
func New(minRoutines int, statTime time.Duration, logFunc func(message string)) (*Work, error) {
5051
if minRoutines <= 0 {
5152
return nil, ErrorInvalidMinRoutines
5253
}
@@ -62,6 +63,7 @@ func New(minRoutines int, statTime time.Duration) (*Work, error) {
6263
control: make(chan int),
6364
kill: make(chan struct{}),
6465
shutdown: make(chan struct{}),
66+
logFunc: logFunc,
6567
}
6668

6769
// Start the manager.
@@ -113,7 +115,7 @@ done:
113115
atomic.AddInt64(&w.routines, -1)
114116
w.wg.Done()
115117

116-
log.Println("Work : gr : Info : Shutdown")
118+
w.log("Worker : Shutting Down")
117119
}
118120

119121
// Run wait for the goroutine pool to take the work
@@ -138,7 +140,7 @@ func (w *Work) manager() {
138140
w.wg.Add(1)
139141

140142
go func() {
141-
log.Println("Work : manager : Started")
143+
w.log("Work Manager : Started")
142144

143145
// Create a timer to run stats.
144146
timer := time.NewTimer(w.statTime)
@@ -161,7 +163,7 @@ func (w *Work) manager() {
161163
case c := <-w.control:
162164
switch c {
163165
case addRoutine:
164-
log.Println("Work : manager : Info : Add Routine")
166+
w.log("Work Manager : Add Routine")
165167

166168
// Capture a unique id.
167169
w.counter++
@@ -174,14 +176,14 @@ func (w *Work) manager() {
174176
go w.work(w.counter)
175177

176178
case rmvRoutine:
177-
log.Println("Work : manager : Info : Remove Routine")
179+
w.log("Work Manager : Remove Routine")
178180

179181
// Capture the number of routines.
180182
routines := int(atomic.LoadInt64(&w.routines))
181183

182184
// Are there routines to remove.
183185
if routines <= w.minRoutines {
184-
log.Println("Work : manager : Info : Remove Routine Cancelled")
186+
w.log("Work Manager : Reached Minimum Can't Remove")
185187
break
186188
}
187189

@@ -196,11 +198,18 @@ func (w *Work) manager() {
196198
active := atomic.LoadInt64(&w.active)
197199

198200
// Display the stats.
199-
fmt.Printf("Work : manager : Stats : G[%d] P[%d] A[%d]\n", routines, pending, active)
201+
w.log(fmt.Sprintf("Work Manager : Stats : G[%d] P[%d] A[%d]", routines, pending, active))
200202

201203
// Reset the clock.
202204
timer.Reset(w.statTime)
203205
}
204206
}
205207
}()
206208
}
209+
210+
// log sending logging messages back to the client.
211+
func (w *Work) log(message string) {
212+
if w.logFunc != nil {
213+
w.logFunc(message)
214+
}
215+
}

0 commit comments

Comments
 (0)