@@ -12,102 +12,110 @@ import (
1212 "sync"
1313)
1414
15- type (
16- // Resource must be implemented by types to use the pool.
17- Resource interface {
18- Close ()
19- }
15+ // Pool manages a set of resources that can be acquired, released and closed.
16+ // We name it Interface because when the package is imported it will be used
17+ // as pool.Interface, which is better semantics.
18+ type Interface interface {
19+ Acquire () (Resource , error )
20+ Release (Resource )
21+ Close ()
22+ }
2023
21- // Factory is a user supplied function that creates resources.
22- Factory func () (Resource , error )
23- )
24+ // An interface allows us to decouple the pool from its implementation, which is
25+ // a good practice for writing testable and maintainable software.
2426
25- type (
26- // resources is a named type for the channel of resources.
27- queue chan Resource
28-
29- // Pool contains a queue of resources and methods that provide
30- // support to share resources between goroutines.
31- Pool struct {
32- mutex sync.Mutex
33- resources queue
34- factory Factory
35- closed bool
36- }
37- )
27+ // Resource must be implemented by types to use the pool.
28+ type Resource interface {
29+ Close ()
30+ }
31+
32+ // Pool manages a set of resources that can be shared safely by multiple goroutines
33+ type pool struct {
34+ sync.Mutex
35+ resources chan Resource
36+ factory func () (Resource , error )
37+ closed bool
38+ }
3839
39- // New creates a pool for managing resources.
40- func New (factory Factory , capacity int ) (* Pool , error ) {
41- // Check the capacity is greater than zero else
42- // we could create an unbuffered channel or panic.
43- if capacity <= 0 {
44- return nil , fmt .Errorf ("Invalid Capacity Value: %d" , capacity )
40+ // ErrInvalidCapacity is returned when there has been an attempt to create an
41+ // unbuffered pool.
42+ var ErrInvalidCapacity = errors .New ("Capacity needs to be greater than zero." )
43+
44+ // New creates a pool from a set of factory functions. A pool provides capacity
45+ // number of resources that can be shared safely by multiple goroutines.
46+ func New (fn func () (Resource , error ), capacity uint ) (* pool , error ) {
47+ if capacity == 0 {
48+ return nil , ErrInvalidCapacity
4549 }
4650
47- return & Pool {
48- factory : factory ,
49- resources : make (queue , capacity ),
51+ return & pool {
52+ factory : fn ,
53+ resources : make (chan Resource , capacity ),
5054 }, nil
5155}
5256
5357// Acquire retrieves a resource from the pool.
54- func (p * Pool ) Acquire () (Resource , error ) {
58+ func (p * pool ) Acquire () (Resource , error ) {
5559 select {
56- case resource , ok := <- p .resources :
60+ // Check for a free resource
61+ case r , ok := <- p .resources :
5762 fmt .Println ("Acquire:" , "Shared Resource" )
5863 if ! ok {
5964 return nil , errors .New ("Pool has been closed." )
6065 }
61- return resource , nil
66+ return r , nil
6267
68+ // Or provide a new one
6369 default :
6470 fmt .Println ("Acquire:" , "New Resource" )
6571 return p .factory ()
6672 }
6773}
6874
69- // Release places the resource back into the queue or closes
70- // the resource for good.
71- func (p * Pool ) Release (resource Resource ) {
75+ // Release places a new resource onto the pool
76+ func (p * pool ) Release (r Resource ) {
7277 // Secure this operation with the Close operation.
73- p .mutex . Lock ()
74- defer p .mutex . Unlock ()
78+ p .Lock ()
79+ defer p .Unlock ()
7580
76- // If the pool is closed, close the resource before returning .
81+ // If the pool is closed, discard the resource.
7782 if p .closed {
78- resource .Close ()
83+ r .Close ()
7984 return
8085 }
8186
82- // The queue is still open so release the resource.
83-
8487 select {
85- // Attempt to place the resource back into the queue first. If the queue
86- // is full, then the default case will be executed.
87- case p .resources <- resource :
88+ // Attempt to place the new resource on the queue.
89+ case p .resources <- r :
8890 fmt .Println ("Release:" , "In Queue" )
8991
90- // The queue is full so just close this resource.
92+ // If the queue is already at capacity we close the resource.
9193 default :
9294 fmt .Println ("Release:" , "Closing" )
93- resource .Close ()
95+ r .Close ()
9496 }
9597}
9698
9799// Close will shutdown the pool and close all existing resources.
98- func (p * Pool ) Close () {
100+ func (p * pool ) Close () {
99101 // Secure this operation with the Release operation.
100- p .mutex .Lock ()
101- defer p .mutex .Unlock ()
102+ p .Lock ()
103+ defer p .Unlock ()
104+
105+ if p .closed {
106+ return
107+ }
102108
103- // If the pool is not closed, the close it down.
104- if ! p .closed {
105- p .closed = true
109+ // TODO: Explain why flag needs to be off before calling CLose() on each
110+ // resource
106111
107- // Close the queue and close all existing resources.
108- close (p .resources )
109- for resource := range p .resources {
110- resource .Close ()
111- }
112+ // Toggle the flag
113+ p .closed = true
114+ // Close the channel
115+ close (p .resources )
116+
117+ // Close the resources
118+ for r := range p .resources {
119+ r .Close ()
112120 }
113121}
0 commit comments