Skip to content

Commit 78cabbd

Browse files
committed
Extended pool to handle multiple keys
The connection pool now handles connections to multiple URI, this work is a precursor to handling multiple connections in the driver.
1 parent 9fe1294 commit 78cabbd

File tree

4 files changed

+61
-28
lines changed

4 files changed

+61
-28
lines changed

src/v1/driver.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Driver {
6969
let conn = connect(this._url, this._config);
7070
conn.initialize(this._userAgent, this._token, streamObserver);
7171
conn._id = sessionId;
72-
conn._release = () => release(conn);
72+
conn._release = () => release(this._url, conn);
7373

7474
this._openSessions[sessionId] = conn;
7575
return conn;
@@ -108,7 +108,7 @@ class Driver {
108108
* @return {Session} new session.
109109
*/
110110
session() {
111-
let conn = this._pool.acquire();
111+
let conn = this._pool.acquire(this._url);
112112
return new Session( conn, (cb) => {
113113
// This gets called on Session#close(), and is where we return
114114
// the pooled 'connection' instance.

src/v1/internal/connector.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ class Connection {
162162
* @constructor
163163
* @param channel - channel with a 'write' function and a 'onmessage'
164164
* callback property
165+
* @param url - url to connect to
165166
*/
166-
constructor (channel) {
167+
constructor (channel, url) {
167168
/**
168169
* An ordered queue of observers, each exchange response (zero or more
169170
* RECORD messages followed by a SUCCESS message) we recieve will be routed
170171
* to the next pending observer.
171172
*/
173+
this.url = url;
172174
this._pendingObservers = [];
173175
this._currentObserver = undefined;
174176
this._ch = channel;
@@ -441,7 +443,7 @@ function connect( url, config = {}) {
441443
trust : config.trust || (hasFeature("trust_on_first_use") ? "TRUST_ON_FIRST_USE" : "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES"),
442444
trustedCertificates : config.trustedCertificates || [],
443445
knownHosts : config.knownHosts
444-
}));
446+
}), url);
445447
}
446448

447449
export default {

src/v1/internal/pool.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ class Pool {
3535
this._destroy = destroy;
3636
this._validate = validate;
3737
this._maxIdle = maxIdle;
38-
this._pool = [];
38+
this._pools = {};
3939
this._release = this._release.bind(this);
4040
}
4141

42-
acquire() {
42+
acquire(key) {
4343
let resource;
44-
while( this._pool.length ) {
45-
resource = this._pool.pop();
44+
let pool = this._pools[key] || [];
45+
while( pool.length ) {
46+
resource = pool.pop();
4647

4748
if( this._validate(resource) ) {
4849
return resource;
@@ -52,11 +53,16 @@ class Pool {
5253
return this._create(this._release);
5354
}
5455

55-
_release(resource) {
56-
if( this._pool.length >= this._maxIdle || !this._validate(resource) ) {
56+
_release(key, resource) {
57+
let pool = this._pools[key];
58+
if (!pool) {
59+
pool = [];
60+
this._pools[key] = pool;
61+
}
62+
if( pool.length >= this._maxIdle || !this._validate(resource) ) {
5763
this._destroy(resource);
5864
} else {
59-
this._pool.push(resource);
65+
pool.push(resource);
6066
}
6167
}
6268
}

test/internal/pool.test.js

+42-17
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ describe('Pool', function() {
2323
it('allocates if pool is empty', function() {
2424
// Given
2525
var counter = 0;
26+
var key = "bolt://localhost:7687";
2627
var pool = new Pool( function (release) { return new Resource(counter++, release) } );
2728

2829
// When
29-
var r0 = pool.acquire();
30-
var r1 = pool.acquire();
30+
var r0 = pool.acquire(key);
31+
var r1 = pool.acquire(key);
3132

3233
// Then
3334
expect( r0.id ).toBe( 0 );
@@ -37,22 +38,45 @@ describe('Pool', function() {
3738
it('pools if resources are returned', function() {
3839
// Given a pool that allocates
3940
var counter = 0;
41+
var key = "bolt://localhost:7687";
4042
var pool = new Pool( function (release) { return new Resource(counter++, release) } );
4143

4244
// When
43-
var r0 = pool.acquire();
44-
r0.close();
45-
var r1 = pool.acquire();
45+
var r0 = pool.acquire(key);
46+
r0.close(key);
47+
var r1 = pool.acquire(key);
4648

4749
// Then
4850
expect( r0.id ).toBe( 0 );
4951
expect( r1.id ).toBe( 0 );
5052
});
5153

54+
it('handles multiple keys', function() {
55+
// Given a pool that allocates
56+
var counter = 0;
57+
var key1 = "bolt://localhost:7687";
58+
var key2 = "bolt://localhost:7688";
59+
var pool = new Pool( function (release) { return new Resource(counter++, release) } );
60+
61+
// When
62+
var r0 = pool.acquire(key1);
63+
var r1 = pool.acquire(key2);
64+
r0.close(key1);
65+
var r2 = pool.acquire(key1);
66+
var r3 = pool.acquire(key2);
67+
68+
// Then
69+
expect( r0.id ).toBe( 0 );
70+
expect( r1.id ).toBe( 1 );
71+
expect( r2.id ).toBe( 0 );
72+
expect( r3.id ).toBe( 2 );
73+
});
74+
5275
it('frees if pool reaches max size', function() {
5376
// Given a pool that tracks destroyed resources
5477
var counter = 0,
5578
destroyed = [];
79+
var key = "bolt://localhost:7687";
5680
var pool = new Pool(
5781
function (release) { return new Resource(counter++, release) },
5882
function (resource) { destroyed.push(resource); },
@@ -61,12 +85,12 @@ describe('Pool', function() {
6185
);
6286

6387
// When
64-
var r0 = pool.acquire();
65-
var r1 = pool.acquire();
66-
var r2 = pool.acquire();
67-
r0.close();
68-
r1.close();
69-
r2.close();
88+
var r0 = pool.acquire(key);
89+
var r1 = pool.acquire(key);
90+
var r2 = pool.acquire(key);
91+
r0.close(key);
92+
r1.close(key);
93+
r2.close(key);
7094

7195
// Then
7296
expect( destroyed.length ).toBe( 1 );
@@ -77,6 +101,7 @@ describe('Pool', function() {
77101
// Given a pool that allocates
78102
var counter = 0,
79103
destroyed = [];
104+
var key = "bolt://localhost:7687";
80105
var pool = new Pool(
81106
function (release) { return new Resource(counter++, release) },
82107
function (resource) { destroyed.push(resource); },
@@ -85,10 +110,10 @@ describe('Pool', function() {
85110
);
86111

87112
// When
88-
var r0 = pool.acquire();
89-
var r1 = pool.acquire();
90-
r0.close();
91-
r1.close();
113+
var r0 = pool.acquire(key);
114+
var r1 = pool.acquire(key);
115+
r0.close(key);
116+
r1.close(key);
92117

93118
// Then
94119
expect( destroyed.length ).toBe( 2 );
@@ -97,8 +122,8 @@ describe('Pool', function() {
97122
});
98123
});
99124

100-
function Resource( id, release ) {
125+
function Resource( id, release) {
101126
var self = this;
102127
this.id = id;
103-
this.close = function() { release(self); };
128+
this.close = function(key) { release(key, self); };
104129
}

0 commit comments

Comments
 (0)