Skip to content

Commit c86c2eb

Browse files
author
gbbr
committed
Placed bool read in sync
1 parent 6e1ebb6 commit c86c2eb

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,4 @@ func performQueries(query int, p pool.Interface) {
8888
// Wait to simulate a query response.
8989
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
9090
fmt.Printf("Query: QID[%d] CID[%d]\n", query, conn.(*dbConnection).ID)
91-
9291
}

‎chapter7/patterns/pool/pool.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ func (p *pool) Acquire() (Resource, error) {
7474

7575
// Release places a new resource onto the pool
7676
func (p *pool) Release(r Resource) {
77+
// Secure this operation with the Close operation.
78+
p.Lock()
79+
defer p.Unlock()
80+
7781
// If the pool is closed, discard the resource.
7882
if p.closed {
7983
r.Close()
8084
return
8185
}
8286

83-
// Secure this operation with the Close operation.
84-
p.Lock()
85-
defer p.Unlock()
86-
8787
select {
8888
// Attempt to place the new resource on the queue.
8989
case p.resources <- r:

0 commit comments

Comments
 (0)