Skip to content

Commit 8eff369

Browse files
Updated comments.
1 parent f5409a4 commit 8eff369

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ type dbConnection struct {
2727
ID int32
2828
}
2929

30-
// Close implements the interface for the pool package.
31-
// Close performs any resource release management.
30+
// Close implements the io.Closer interface so dbConnection
31+
// can be managed by the pool. Close performs any resource
32+
// release management.
3233
func (dbConn *dbConnection) Close() error {
3334
fmt.Println("Close: Connection", dbConn.ID)
3435
return nil
3536
}
3637

37-
// isCounter provides support for giving each
38-
// connection a unique id.
38+
// idCounter provides support for giving each connection a unique id.
3939
var idCounter int32
4040

41-
// createConnection is a factory method called by the pool
42-
// framework when new connections are needed.
41+
// createConnection is a factory method that will be called by
42+
// the pool when a new connection is needed.
4343
func createConnection() (io.Closer, error) {
4444
id := atomic.AddInt32(&idCounter, 1)
4545
fmt.Println("Create: New Connection", id)
@@ -49,17 +49,16 @@ func createConnection() (io.Closer, error) {
4949

5050
// main is the entry point for all Go programs.
5151
func main() {
52-
var wg sync.WaitGroup // waits for program to finish
52+
var wg sync.WaitGroup
5353
wg.Add(maxGoroutines)
5454

55-
// Create the buffered channel to hold
56-
// and manage the connections.
55+
// Create the pool to manage our connections.
5756
p, err := pool.New(createConnection, pooledResources)
5857
if err != nil {
5958
fmt.Println(err)
6059
}
6160

62-
// Perform queries using a connection from the pool.
61+
// Perform queries using connections from the pool.
6362
for query := 0; query < maxGoroutines; query++ {
6463
// Each goroutine needs its own copy of the query
6564
// value else they will all be sharing the same query

‎chapter7/patterns/pool/pool.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
// Pool manages a set of resources that can be shared safely by multiple goroutines.
17+
// The resource being managed must implement to io.Closer interface.
1718
type Pool struct {
1819
sync.Mutex
1920
resources chan io.Closer
@@ -25,8 +26,9 @@ type Pool struct {
2526
// unbuffered pool.
2627
var ErrInvalidCapacity = errors.New("Capacity needs to be greater than zero.")
2728

28-
// New creates a pool from a set of factory functions. A pool provides capacity
29-
// number of resources that can be shared safely by multiple goroutines.
29+
// New creates a pool that manages resources. A pool requires a function
30+
// that can allocate a new resource and the number of resources that can
31+
// be allocated.
3032
func New(fn func() (io.Closer, error), capacity uint) (*Pool, error) {
3133
if capacity == 0 {
3234
return nil, ErrInvalidCapacity

0 commit comments

Comments
 (0)