Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request cancellation #67

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var Client = function(config) {
this.queryQueue = [];
this.password = config.password || defaults.password;
this.encoding = 'utf8';
this.processID = null;
this.secretKey = null;
var self = this;
};

Expand Down Expand Up @@ -59,6 +61,11 @@ p.connect = function(callback) {
con.password(md5password);
});

con.once('backendKeyData', function(msg) {
self.processID = msg.processID;
self.secretKey = msg.secretKey;
});

//hook up query handling events to connection
//after the connection initially becomes ready for queries
con.once('readyForQuery', function() {
Expand Down Expand Up @@ -130,6 +137,25 @@ p.connect = function(callback) {

};

p.cancel = function(client, query) {
if (client.activeQuery == query) {
var con = this.connection;

if(this.host && this.host.indexOf('/') === 0) {
con.connect(this.host + '/.s.PGSQL.' + this.port);
} else {
con.connect(this.port, this.host);
}

//once connection is established send cancel message
con.on('connect', function() {
con.cancel(client.processID, client.secretKey);
});
}
else if (client.queryQueue.indexOf(query) != -1)
client.queryQueue.splice(client.queryQueue.indexOf(query), 1);
};

p._pulseQueryQueue = function() {
if(this.readyForQuery===true) {
this.activeQuery = this.queryQueue.shift();
Expand Down
17 changes: 17 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ p.startup = function(config) {
this.stream.write(buffer);
};

p.cancel = function(processID, secretKey) {
var bodyBuffer = this.writer
.addInt16(1234)
.addInt16(5678)
.addInt32(processID)
.addInt32(secretKey)
.addCString('').flush();

var length = bodyBuffer.length + 4;

var buffer = new Writer()
.addInt32(length)
.add(bodyBuffer)
.join();
this.stream.write(buffer);
};

p.password = function(password) {
//0x70 = 'p'
this._send(0x70, this.writer.addCString(password));
Expand Down
10 changes: 10 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ PG.prototype.connect = function(config, callback) {
return pool.acquire(cb);
}

// cancel the query runned by the given client
PG.prototype.cancel = function(config, client, query) {
var c = config;
//allow for no config to be passed
if(typeof c === 'function')
c = defaults;
var cancellingClient = new this.Client(c);
cancellingClient.cancel(client, query);
}

module.exports = new PG(Client);

//lazy require native module...the native module may not have installed
Expand Down