@@ -36,59 +36,117 @@ class Pool {
3636 this . _validate = validate ;
3737 this . _maxIdle = maxIdle ;
3838 this . _pools = { } ;
39+ this . _activeResourceCounts = { } ;
3940 this . _release = this . _release . bind ( this ) ;
4041 }
4142
43+ /**
44+ * Acquire and idle resource fom the pool or create a new one.
45+ * @param {string } key the resource key.
46+ * @return {object } resource that is ready to use.
47+ */
4248 acquire ( key ) {
43- let resource ;
4449 let pool = this . _pools [ key ] ;
4550 if ( ! pool ) {
4651 pool = [ ] ;
4752 this . _pools [ key ] = pool ;
4853 }
4954 while ( pool . length ) {
50- resource = pool . pop ( ) ;
55+ const resource = pool . pop ( ) ;
5156
5257 if ( this . _validate ( resource ) ) {
58+ // idle resource is valid and can be acquired
59+ resourceAcquired ( key , this . _activeResourceCounts ) ;
5360 return resource ;
5461 } else {
5562 this . _destroy ( resource ) ;
5663 }
5764 }
5865
66+ // there exist no idle valid resources, create a new one for acquisition
67+ resourceAcquired ( key , this . _activeResourceCounts ) ;
5968 return this . _create ( key , this . _release ) ;
6069 }
6170
71+ /**
72+ * Destroy all idle resources for the given key.
73+ * @param {string } key the resource key to purge.
74+ */
6275 purge ( key ) {
63- let resource ;
64- let pool = this . _pools [ key ] || [ ] ;
76+ const pool = this . _pools [ key ] || [ ] ;
6577 while ( pool . length ) {
66- resource = pool . pop ( ) ;
78+ const resource = pool . pop ( ) ;
6779 this . _destroy ( resource )
6880 }
6981 delete this . _pools [ key ]
7082 }
7183
84+ /**
85+ * Destroy all idle resources in this pool.
86+ */
7287 purgeAll ( ) {
7388 Object . keys ( this . _pools ) . forEach ( key => this . purge ( key ) ) ;
7489 }
7590
91+ /**
92+ * Check if this pool contains resources for the given key.
93+ * @param {string } key the resource key to check.
94+ * @return {boolean } <code>true</code> when pool contains entries for the given key, <code>false</code> otherwise.
95+ */
7696 has ( key ) {
7797 return ( key in this . _pools ) ;
7898 }
7999
100+ /**
101+ * Get count of active (checked out of the pool) resources for the given key.
102+ * @param {string } key the resource key to check.
103+ * @return {number } count of resources acquired by clients.
104+ */
105+ activeResourceCount ( key ) {
106+ return this . _activeResourceCounts [ key ] || 0 ;
107+ }
108+
80109 _release ( key , resource ) {
81- let pool = this . _pools [ key ] ;
82- if ( ! pool ) {
110+ const pool = this . _pools [ key ] ;
111+
112+ if ( pool ) {
113+ // there exist idle connections for the given key
114+ if ( pool . length >= this . _maxIdle || ! this . _validate ( resource ) ) {
115+ this . _destroy ( resource ) ;
116+ } else {
117+ pool . push ( resource ) ;
118+ }
119+ } else {
83120 // key has been purged, don't put it back, just destroy the resource
84121 this . _destroy ( resource ) ;
85- return ;
86- }
87- if ( pool . length >= this . _maxIdle || ! this . _validate ( resource ) ) {
88- this . _destroy ( resource ) ;
89- } else {
90- pool . push ( resource ) ;
91122 }
123+
124+ resourceReleased ( key , this . _activeResourceCounts ) ;
125+ }
126+ }
127+
128+ /**
129+ * Increment active (checked out of the pool) resource counter.
130+ * @param {string } key the resource group identifier (server address for connections).
131+ * @param {Object.<string, number> } activeResourceCounts the object holding active counts per key.
132+ */
133+ function resourceAcquired ( key , activeResourceCounts ) {
134+ const currentCount = activeResourceCounts [ key ] || 0 ;
135+ activeResourceCounts [ key ] = currentCount + 1 ;
136+ }
137+
138+ /**
139+ * Decrement active (checked out of the pool) resource counter.
140+ * @param {string } key the resource group identifier (server address for connections).
141+ * @param {Object.<string, number> } activeResourceCounts the object holding active counts per key.
142+ */
143+ function resourceReleased ( key , activeResourceCounts ) {
144+ const currentCount = activeResourceCounts [ key ] || 0 ;
145+ const nextCount = currentCount - 1 ;
146+ if ( nextCount > 0 ) {
147+ activeResourceCounts [ key ] = nextCount ;
148+ } else {
149+ delete activeResourceCounts [ key ] ;
92150 }
93151}
94152
0 commit comments