Skip to content

Commit aa63f50

Browse files
committed
remove custom pool code
1 parent 4cb97a2 commit aa63f50

File tree

4 files changed

+1
-262
lines changed

4 files changed

+1
-262
lines changed

lib/client-pool.js

-100
This file was deleted.

lib/native.js

-2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,4 @@ p.handleReadyForQuery = function() {
176176
this.emit('end');
177177
};
178178

179-
var pool = require(__dirname + '/client-pool').init(ctor);
180-
181179
module.exports = ctor;

lib/utils.js

+1-64
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var defaults = require(__dirname + "/defaults");
33
var events = require('events');
44
var sys = require('sys');
55

6+
//compatibility for old nodes
67
if(typeof events.EventEmitter.prototype.once !== 'function') {
78
events.EventEmitter.prototype.once = function (type, listener) {
89
var self = this;
@@ -13,69 +14,6 @@ if(typeof events.EventEmitter.prototype.once !== 'function') {
1314
};
1415
}
1516

16-
var Pool = function(maxSize, createFn) {
17-
events.EventEmitter.call(this);
18-
this.maxSize = maxSize;
19-
this.createFn = createFn;
20-
this.items = [];
21-
this.waits = [];
22-
}
23-
24-
sys.inherits(Pool, events.EventEmitter);
25-
26-
var p = Pool.prototype;
27-
28-
p.checkOut = function(callback) {
29-
if(!this.maxSize) {
30-
return callback(null, this.createFn());
31-
}
32-
var len = 0;
33-
for(var i = 0, len = this.items.length; i < len; i++) {
34-
var item = this.items[i];
35-
if(item.checkedIn) {
36-
return this._pulse(item, callback);
37-
}
38-
}
39-
//check if we can create a new item
40-
if(this.items.length < this.maxSize && this.createFn) {
41-
var result = this.createFn();
42-
var item = {ref: result, checkedIn: true}
43-
this.items.push(item);
44-
if(item.checkedIn) {
45-
return this._pulse(item, callback)
46-
}
47-
}
48-
this.waits.push(callback);
49-
return false; //did not execute sync
50-
}
51-
52-
p.checkIn = function(item) {
53-
//scan current items
54-
for(var i = 0, len = this.items.length; i < len; i++) {
55-
var currentItem = this.items[i];
56-
if(currentItem.ref == item) {
57-
currentItem.checkedIn = true;
58-
this._pulse(currentItem);
59-
return true;
60-
}
61-
}
62-
//add new item
63-
var newItem = {ref: item, checkedIn: true};
64-
this.items.push(newItem);
65-
this._pulse(newItem);
66-
return false;
67-
}
68-
69-
p._pulse = function(item, cb) {
70-
cb = cb || this.waits.shift()
71-
if(cb) {
72-
item.checkedIn = false;
73-
cb(null, item.ref)
74-
return true;
75-
}
76-
return false;
77-
};
78-
7917
var parseConnectionString = function(str) {
8018
//unix socket
8119
if(str.charAt(0) === '/') {
@@ -155,7 +93,6 @@ var getLibpgConString = function(config, callback) {
15593
}
15694

15795
module.exports = {
158-
Pool: Pool,
15996
normalizeConnectionInfo: normalizeConnectionInfo,
16097
//only exported here to make testing of this method possible
16198
//since it contains quite a bit of logic and testing for

test/unit/utils-tests.js

-96
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require(__dirname + '/test-helper');
22
var utils = require(__dirname + "/../../lib/utils");
3-
var Pool = utils.Pool;
43
var defaults = require(__dirname + "/../../lib").defaults;
54

65
//this tests the monkey patching
@@ -21,101 +20,6 @@ test("EventEmitter.once", function() {
2120
assert.equal(callCount, 1);
2221
});
2322

24-
test('an empty pool', function() {
25-
test('with no creation method', function() {
26-
var pool = new Pool(10);
27-
var brian = {name:'brian'};
28-
29-
test('can set and get an item', function() {
30-
pool.checkIn(brian);
31-
var sync = pool.checkOut(assert.calls(function(err, item) {
32-
assert.equal(brian, item)
33-
assert.same(brian, item)
34-
}))
35-
assert.ok(sync, "should have fired sync")
36-
})
37-
38-
test('checkout blocks until item checked back in', function() {
39-
var called = false;
40-
var sync = pool.checkOut(assert.calls(function(err, item) {
41-
called = true;
42-
assert.equal(brian, item)
43-
assert.same(brian, item)
44-
}))
45-
assert.ok(sync === false, "Should not have fired sync")
46-
assert.ok(called === false, "Should not have fired callback yet")
47-
pool.checkIn(brian)
48-
})
49-
50-
})
51-
52-
test('with a creation method', function() {
53-
var customName = "first";
54-
var callCount = 0;
55-
var pool = new Pool(3, function() {
56-
return {name: customName + (++callCount)};
57-
});
58-
59-
test('creates if pool is not at max size', function() {
60-
var sync = pool.checkOut(assert.calls(function(err, item) {
61-
assert.equal(item.name, "first1");
62-
}))
63-
assert.ok(sync, "Should have generated item & called callback in sync")
64-
})
65-
66-
test('creates again if item is checked out', function() {
67-
var sync = pool.checkOut(assert.calls(function(err, item) {
68-
assert.equal(item.name, "first2")
69-
}))
70-
assert.ok(sync, "Should have called in sync again")
71-
})
72-
var external = {name: 'boom'};
73-
test('can add another item', function() {
74-
pool.checkIn(external)
75-
var sync = pool.checkOut(assert.calls(function(err, item) {
76-
assert.equal(item.name, 'boom')
77-
}))
78-
assert.ok(sync, "Should have fired 3rd in sync")
79-
})
80-
81-
test('after pool is full, create is not called again', function() {
82-
var called = false;
83-
var sync = pool.checkOut(assert.calls(function(err, item) {
84-
called = true;
85-
assert.equal(item.name, 'boom')
86-
}))
87-
assert.ok(sync === false, "should not be sync")
88-
assert.ok(called === false, "should not have called callback")
89-
pool.checkIn(external);
90-
})
91-
})
92-
})
93-
94-
test('a pool with size of zero', function() {
95-
var index = 0;
96-
var pool = new Pool(0, function() {
97-
return index++;
98-
})
99-
test('checkin does nothing', function() {
100-
index = 0;
101-
pool.checkIn(301813);
102-
assert.equal(pool.checkOut(assert.calls(function(err, item) {
103-
assert.equal(item, 0);
104-
})));
105-
})
106-
test('always creates a new item', function() {
107-
index = 0;
108-
pool.checkOut(assert.calls(function(err, item) {
109-
assert.equal(item, 0);
110-
}))
111-
pool.checkOut(assert.calls(function(err, item) {
112-
assert.equal(item, 1);
113-
}))
114-
pool.checkOut(assert.calls(function(err, item) {
115-
assert.equal(item, 2);
116-
}))
117-
})
118-
})
11923

12024
test('normalizing connection info', function() {
12125
test('with objects', function() {

0 commit comments

Comments
 (0)