Skip to content

Commit f3c8b97

Browse files
author
Christophe Macabiau
committed
query cancellation
1 parent 6b97ed2 commit f3c8b97

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

lib/client.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var Client = function(config) {
2121
this.queryQueue = [];
2222
this.password = config.password || defaults.password;
2323
this.encoding = 'utf8';
24+
this.processID = null;
25+
this.secretKey = null;
2426
var self = this;
2527
};
2628

@@ -59,6 +61,11 @@ p.connect = function(callback) {
5961
con.password(md5password);
6062
});
6163

64+
con.once('backendKeyData', function(msg) {
65+
self.processID = msg.processID;
66+
self.secretKey = msg.secretKey;
67+
});
68+
6269
//hook up query handling events to connection
6370
//after the connection initially becomes ready for queries
6471
con.once('readyForQuery', function() {
@@ -130,6 +137,25 @@ p.connect = function(callback) {
130137

131138
};
132139

140+
p.cancel = function(client, query) {
141+
if (client.activeQuery == query) {
142+
var con = this.connection;
143+
144+
if(this.host && this.host.indexOf('/') === 0) {
145+
con.connect(this.host + '/.s.PGSQL.' + this.port);
146+
} else {
147+
con.connect(this.port, this.host);
148+
}
149+
150+
//once connection is established send cancel message
151+
con.on('connect', function() {
152+
con.cancel(client.processID, client.secretKey);
153+
});
154+
}
155+
else if (client.queryQueue.indexOf(query) != -1)
156+
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
157+
};
158+
133159
p._pulseQueryQueue = function() {
134160
if(this.readyForQuery===true) {
135161
this.activeQuery = this.queryQueue.shift();

lib/connection.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ p.startup = function(config) {
7373
this.stream.write(buffer);
7474
};
7575

76+
p.cancel = function(processID, secretKey) {
77+
var bodyBuffer = this.writer
78+
.addInt16(1234)
79+
.addInt16(5678)
80+
.addInt32(processID)
81+
.addInt32(secretKey)
82+
.addCString('').flush();
83+
84+
var length = bodyBuffer.length + 4;
85+
86+
var buffer = new Writer()
87+
.addInt32(length)
88+
.add(bodyBuffer)
89+
.join();
90+
this.stream.write(buffer);
91+
};
92+
7693
p.password = function(password) {
7794
//0x70 = 'p'
7895
this._send(0x70, this.writer.addCString(password));

lib/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ PG.prototype.connect = function(config, callback) {
8181
return pool.acquire(cb);
8282
}
8383

84+
// cancel the query runned by the given client
85+
PG.prototype.cancel = function(config, client, query) {
86+
var c = config;
87+
//allow for no config to be passed
88+
if(typeof c === 'function')
89+
c = defaults;
90+
var cancellingClient = new this.Client(c);
91+
cancellingClient.cancel(client, query);
92+
}
93+
8494
module.exports = new PG(Client);
8595

8696
//lazy require native module...the native module may not have installed

0 commit comments

Comments
 (0)