Skip to content

Commit 74ca562

Browse files
author
x
committed
Submit queries immediately rather that waiting for no reason
1 parent 41f8712 commit 74ca562

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

lib/client.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var Client = function(config) {
2727
ssl: this.connectionParameters.ssl
2828
});
2929
this.queryQueue = [];
30+
this.sentQueryQueue = [];
31+
this.sendImmediately = true;
3032
this.binary = c.binary || defaults.binary;
3133
this.encoding = 'utf8';
3234
this.processID = null;
@@ -154,6 +156,7 @@ Client.prototype.connect = function(callback) {
154156
var activeQuery = self.activeQuery;
155157
self.activeQuery = null;
156158
self.readyForQuery = true;
159+
self.handshakeDone = true;
157160
self._pulseQueryQueue();
158161
if(activeQuery) {
159162
activeQuery.handleReadyForQuery();
@@ -279,20 +282,32 @@ Client.prototype.escapeLiteral = function(str) {
279282
};
280283

281284
Client.prototype._pulseQueryQueue = function() {
285+
if(!this.handshakeDone)
286+
return;
287+
288+
while((this.sendImmediately && !this.blocked) || (this.activeQuery === null && this.sentQueryQueue.length === 0)) {
289+
var query = this.queryQueue.shift();
290+
if(!query)
291+
break;
292+
293+
query.submit(this.connection);
294+
this.blocked = query.blocking;
295+
this.sentQueryQueue.push(query);
296+
}
297+
282298
if(this.readyForQuery===true) {
283-
this.activeQuery = this.queryQueue.shift();
299+
this.activeQuery = this.sentQueryQueue.shift();
284300
if(this.activeQuery) {
285301
this.readyForQuery = false;
286302
this.hasExecuted = true;
287-
this.activeQuery.submit(this.connection);
288303
} else if(this.hasExecuted) {
289304
this.activeQuery = null;
290305
this.emit('drain');
291306
}
292307
}
293308
};
294309

295-
Client.prototype._copy = function (text, stream) {
310+
Client.prototype._copy = function (text, stream, blocking) {
296311
var config = {};
297312
config.text = text;
298313
config.stream = stream;
@@ -304,14 +319,14 @@ Client.prototype._copy = function (text, stream) {
304319
}
305320
};
306321
var query = new Query(config);
322+
query.blocking = blocking;
307323
this.queryQueue.push(query);
308324
this._pulseQueryQueue();
309325
return config.stream;
310-
311326
};
312327

313328
Client.prototype.copyFrom = function (text) {
314-
return this._copy(text, new CopyFromStream());
329+
return this._copy(text, new CopyFromStream(), true);
315330
};
316331

317332
Client.prototype.copyTo = function (text) {

test/unit/client/simple-query-tests.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('executing query', function() {
3131

3232
test("multiple in the queue", function() {
3333
var client = helper.client();
34+
client.sendImmediately = false;
3435
var connection = client.connection;
3536
var queries = connection.queries;
3637
client.query('one');
@@ -59,6 +60,38 @@ test('executing query', function() {
5960
assert.equal(queries[2], 'three');
6061
});
6162
});
63+
64+
test("multiple in the queue, sending immediately", function() {
65+
var client = helper.client();
66+
client.sendImmediately = true;
67+
var connection = client.connection;
68+
var queries = connection.queries;
69+
client.query('one');
70+
client.query('two');
71+
client.query('three');
72+
assert.empty(queries);
73+
74+
test("after one ready for query",function() {
75+
connection.emit('readyForQuery');
76+
assert.lengthIs(queries, 3);
77+
assert.equal(queries[0], "one");
78+
});
79+
80+
test('after two ready for query', function() {
81+
connection.emit('readyForQuery');
82+
assert.lengthIs(queries, 3);
83+
});
84+
85+
test("after a bunch more", function() {
86+
connection.emit('readyForQuery');
87+
connection.emit('readyForQuery');
88+
connection.emit('readyForQuery');
89+
assert.lengthIs(queries, 3);
90+
assert.equal(queries[0], "one");
91+
assert.equal(queries[1], 'two');
92+
assert.equal(queries[2], 'three');
93+
});
94+
});
6295
});
6396

6497
test("query event binding and flow", function() {

0 commit comments

Comments
 (0)