Skip to content

Commit 385106a

Browse files
committed
Added ability to purge item in pool
We need purging of item in the connection pool since we need a way of removing connections that are no longer online or in other ways have been removed from a cluster.
1 parent 3155753 commit 385106a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/v1/internal/pool.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,31 @@ class Pool {
4242
acquire(key) {
4343
let resource;
4444
let pool = this._pools[key] || [];
45-
while( pool.length ) {
45+
while (pool.length) {
4646
resource = pool.pop();
4747

48-
if( this._validate(resource) ) {
48+
if (this._validate(resource)) {
4949
return resource;
5050
}
5151
}
5252

5353
return this._create(this._release);
5454
}
5555

56+
purge(key) {
57+
let resource;
58+
let pool = this._pools[key] || [];
59+
while (pool.length) {
60+
resource = pool.pop();
61+
this._destroy(resource)
62+
}
63+
delete this._pools[key]
64+
}
65+
66+
has(key) {
67+
return (key in this._pools);
68+
}
69+
5670
_release(key, resource) {
5771
let pool = this._pools[key];
5872
if (!pool) {

test/internal/pool.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,38 @@ describe('Pool', function() {
120120
expect( destroyed[0].id ).toBe( r0.id );
121121
expect( destroyed[1].id ).toBe( r1.id );
122122
});
123+
124+
125+
it('purges keys', function() {
126+
// Given a pool that allocates
127+
var counter = 0;
128+
var key1 = "bolt://localhost:7687";
129+
var key2 = "bolt://localhost:7688";
130+
var pool = new Pool( function (release) { return new Resource(counter++, release) },
131+
function (res) {res.destroyed = true; return true}
132+
);
133+
134+
// When
135+
var r0 = pool.acquire(key1);
136+
var r1 = pool.acquire(key2);
137+
r0.close(key1);
138+
r1.close(key2);
139+
expect(pool.has(key1)).toBe(true);
140+
expect(pool.has(key2)).toBe(true);
141+
pool.purge(key1);
142+
expect(pool.has(key1)).toBe(false);
143+
expect(pool.has(key2)).toBe(true);
144+
145+
var r2 = pool.acquire(key1);
146+
var r3 = pool.acquire(key2);
147+
148+
// Then
149+
expect( r0.id ).toBe( 0 );
150+
expect( r0.destroyed ).toBe( true );
151+
expect( r1.id ).toBe( 1 );
152+
expect( r2.id ).toBe( 2 );
153+
expect( r3.id ).toBe( 1 );
154+
});
123155
});
124156

125157
function Resource( id, release) {

0 commit comments

Comments
 (0)