Skip to content

Commit d3129e5

Browse files
Merge pull request goinaction#10 from gbbr/pool-c7
Some corrections
2 parents 39cabdf + e6a04d5 commit d3129e5

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

‎chapter7/patterns/pool/main/main.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ const (
1919
pooledResources = 2 // number of resources in the pool
2020
)
2121

22-
var (
23-
connectionID int32 // counter
24-
wg sync.WaitGroup // waits for program to finish
25-
)
26-
2722
// dbConnection simulates a resource to share.
2823
type dbConnection struct {
2924
ID int32
@@ -35,17 +30,20 @@ func (dbConn *dbConnection) Close() {
3530
fmt.Println("Close: Connection", dbConn.ID)
3631
}
3732

33+
var counter int32
34+
3835
// createConnection is a factory method called by the pool
3936
// framework when new connections are needed.
4037
func createConnection() (pool.Resource, error) {
41-
id := atomic.AddInt32(&connectionID, 1)
38+
id := atomic.AddInt32(&counter, 1)
4239
fmt.Println("Create: New Connection", id)
4340

4441
return &dbConnection{id}, nil
4542
}
4643

4744
// main is the entry point for all Go programs.
4845
func main() {
46+
var wg sync.WaitGroup // waits for program to finish
4947
wg.Add(maxGoroutines)
5048

5149
// Create the buffered channel to hold
@@ -58,10 +56,10 @@ func main() {
5856
// Perform queries using a connection from the pool.
5957
for q := 0; q < maxGoroutines; q++ {
6058
// TODO: Explain about closures and why we are using params
61-
go func(query int, pool pool.Interface) {
62-
performQueries(query, pool)
59+
go func(n int) {
60+
performQueries(n, p)
6361
wg.Done()
64-
}(q, p)
62+
}(q)
6563

6664
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
6765
}

‎chapter7/patterns/pool/pool.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var ErrInvalidCapacity = errors.New("Capacity needs to be greater than zero.")
4343

4444
// New creates a pool from a set of factory functions. A pool provides capacity
4545
// number of resources that can be shared safely by multiple goroutines.
46-
func New(fn func() (Resource, error), capacity uint) (*pool, error) {
46+
func New(fn func() (Resource, error), capacity uint) (Interface, error) {
4747
if capacity == 0 {
4848
return nil, ErrInvalidCapacity
4949
}

0 commit comments

Comments
 (0)