Skip to content

Commit 74a7726

Browse files
Updated condition on the cap size
1 parent 188eff2 commit 74a7726

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

‎chapter7/patterns/pool/pool.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var ErrInvalidCapacity = errors.New("Capacity value too small.")
2929
// function that can allocate a new resource and the number of
3030
// resources that can be allocated.
3131
func New(fn func() (io.Closer, error), cap uint) (*Pool, error) {
32-
if cap == 0 {
32+
if cap <= 0 {
3333
return nil, ErrInvalidCapacity
3434
}
3535

@@ -59,12 +59,16 @@ func (p *Pool) Acquire() (io.Closer, error) {
5959

6060
// Release places a new resource onto the pool.
6161
func (p *Pool) Release(r io.Closer) {
62-
// Secure this operation with the Close operation.
62+
// Secure reading the closed flag.
63+
var isClosed bool
6364
p.Lock()
64-
defer p.Unlock()
65+
{
66+
isClosed = p.closed
67+
}
68+
p.Unlock()
6569

6670
// If the pool is closed, discard the resource.
67-
if p.closed {
71+
if isClosed {
6872
r.Close()
6973
return
7074
}

0 commit comments

Comments
 (0)