Skip to content

Commit 77c8192

Browse files
Checking capacity value on New
1 parent dc3365b commit 77c8192

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎chapter6/patterns/pool/pool.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ type (
3333
)
3434

3535
// New creates a pool for managing resources.
36-
func New(factory Factory, capacity int) *Pool {
36+
func New(factory Factory, capacity int) (*Pool, error) {
37+
// Check the capacity is greater than zero else
38+
// we could create an unbuffered channel or panic.
39+
if capacity <= 0 {
40+
return nil, fmt.Errorf("Invalid Capacity Value: %d", capacity)
41+
}
42+
3743
return &Pool{
3844
factory: factory,
3945
resources: make(queue, capacity),
40-
}
46+
}, nil
4147
}
4248

4349
// getQueue returns a safe copy of the resource queue.
@@ -52,9 +58,9 @@ func (p *Pool) getQueue() queue {
5258
return resources
5359
}
5460

55-
// Acquire retrieves a resource from the pool.
61+
// Acquire retrieves a resource from the pool.
5662
func (p *Pool) Acquire() (Resource, error) {
57-
// Get a safe copy of the resource queue and check
63+
// Get a safe copy of the queue and check
5864
// if the queue has been closed.
5965
resources := p.getQueue()
6066
if resources == nil {

0 commit comments

Comments
 (0)