Skip to content

Commit b2763f6

Browse files
Fixed some idiomatic issues
1 parent 6c73c6e commit b2763f6

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

‎chapter7/patterns/pool/pool.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ import (
1515
// multiple goroutines. The resource being managed must implement
1616
// the io.Closer interface.
1717
type Pool struct {
18-
sync.Mutex
18+
m sync.Mutex
1919
resources chan io.Closer
2020
factory func() (io.Closer, error)
2121
closed bool
2222
}
2323

24-
// ErrInvalidCapacity is returned when there has been an attempt
25-
// to create an unbuffered pool.
26-
var ErrInvalidCapacity = errors.New("Capacity value too small.")
24+
// ErrPoolClosed is returned when an acquier returns on a
25+
// closed pool.
26+
var ErrPoolClosed = errors.New("Pool has been closed.")
2727

2828
// New creates a pool that manages resources. A pool requires a
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) {
3232
if cap <= 0 {
33-
return nil, ErrInvalidCapacity
33+
return nil, errors.New("Capacity value too small.")
3434
}
3535

3636
return &Pool{
@@ -46,7 +46,7 @@ func (p *Pool) Acquire() (io.Closer, error) {
4646
case r, ok := <-p.resources:
4747
fmt.Println("Acquire:", "Shared Resource")
4848
if !ok {
49-
return nil, errors.New("Pool has been closed.")
49+
return nil, ErrPoolClosed
5050
}
5151
return r, nil
5252

@@ -60,8 +60,8 @@ func (p *Pool) Acquire() (io.Closer, error) {
6060
// Release places a new resource onto the pool.
6161
func (p *Pool) Release(r io.Closer) {
6262
// Secure this operation with the Close operation.
63-
p.Lock()
64-
defer p.Unlock()
63+
p.m.Lock()
64+
defer p.m.Unlock()
6565

6666
// If the pool is closed, discard the resource.
6767
if p.closed {
@@ -84,8 +84,8 @@ func (p *Pool) Release(r io.Closer) {
8484
// Close will shutdown the pool and close all existing resources.
8585
func (p *Pool) Close() {
8686
// Secure this operation with the Release operation.
87-
p.Lock()
88-
defer p.Unlock()
87+
p.m.Lock()
88+
defer p.m.Unlock()
8989

9090
// If the pool is already close, don't do anything.
9191
if p.closed {

0 commit comments

Comments
 (0)