Skip to content

Commit e8be79a

Browse files
committed
added connection pooling for non-cached setups
1 parent 00bf6ef commit e8be79a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ Note that you can also use `cradle.setup` to set a global configuration:
6565
var c = new(cradle.Connection),
6666
cc = new(cradle.Connection)('173.45.66.92');
6767

68+
### Connection pooling ###
69+
70+
Due to cache coherence issues, connection pooling is currently only available for non-caching instances. If caching is enabled, only one client will be used internally.
71+
Otherwise, the number of client connections to be used for handling requests can be set using the `poolsize` variable. The following example creates a cradle connection handle with 32 parallel HTTP connections:
72+
73+
var c = new (cradle.Connection)("localhost", 5984, {
74+
cache : false,
75+
raw : false,
76+
poolsize : 32
77+
});
78+
79+
6880
### creating a database ###
6981

7082
var db = c.database('starwars');

lib/cradle.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cradle.auth = null;
2525
cradle.options = {
2626
cache: true,
2727
raw: false,
28+
poolsize : 1,
2829
timeout: 0,
2930
secure: false,
3031
headers: {}
@@ -61,7 +62,9 @@ cradle.Connection = function Connection(/* variable args */) {
6162
this.host = host || cradle.host;
6263
this.port = port || cradle.port;
6364
this.auth = auth || cradle.auth;
65+
6466
this.options = cradle.merge({}, cradle.options, options);
67+
6568

6669
this.options.secure = this.options.secure || this.options.ssl;
6770

@@ -80,6 +83,14 @@ cradle.Connection = function Connection(/* variable args */) {
8083
}
8184

8285
this.socket = (this.options.secure) ? https : http;
86+
87+
this.agent = new (this.socket).Agent({
88+
host: this.host,
89+
port: this.port
90+
});
91+
92+
this.agent.maxSockets = ((!this.options.cache) ? this.options.poolsize : 1);
93+
8394
};
8495

8596
//
@@ -119,8 +130,7 @@ cradle.Connection.prototype.rawRequest = function (method, path, options, data,
119130
}
120131

121132
request = this.socket.request({
122-
host: this.host,
123-
port: this.port,
133+
agent: this.agent,
124134
method: method.toUpperCase(),
125135
path: path,
126136
headers: headers
@@ -134,6 +144,7 @@ cradle.Connection.prototype.rawRequest = function (method, path, options, data,
134144
promise.emit('response', res);
135145
res.on('data', function (chunk) { promise.emit('data', chunk) });
136146
res.on('end', function () { promise.emit('end') });
147+
console.log(that.agent.sockets.length);
137148
});
138149

139150
if (data) {

0 commit comments

Comments
 (0)