Skip to content

Commit 92c22c8

Browse files
committed
Add ability to cancel active query
1 parent 3f6760c commit 92c22c8

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

packages/pg/lib/client.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -378,23 +378,25 @@ Client.prototype.getStartupConf = function () {
378378
return data
379379
}
380380

381-
Client.prototype.cancel = function (client, query) {
382-
if (client.activeQuery === query) {
383-
var con = this.connection
381+
Client.prototype.cancelActiveQuery = function () {
382+
var con = new Connection({
383+
ssl: this.connectionParameters.ssl
384+
})
384385

385-
if (this.host && this.host.indexOf('/') === 0) {
386-
con.connect(this.host + '/.s.PGSQL.' + this.port)
387-
} else {
388-
con.connect(this.port, this.host)
389-
}
386+
if (this.host && this.host.indexOf('/') === 0) {
387+
con.connect(this.host + '/.s.PGSQL.' + this.port)
388+
} else {
389+
con.connect(this.port, this.host)
390+
}
390391

392+
return new Promise((resolve, reject) => {
391393
// once connection is established send cancel message
392-
con.on('connect', function () {
393-
con.cancel(client.processID, client.secretKey)
394+
con.on('connect', () => {
395+
con.cancel(this.processID, this.secretKey)
394396
})
395-
} else if (client.queryQueue.indexOf(query) !== -1) {
396-
client.queryQueue.splice(client.queryQueue.indexOf(query), 1)
397-
}
397+
con.once('end', resolve)
398+
con.once('error', reject)
399+
})
398400
}
399401

400402
Client.prototype.setTypeParser = function (oid, format, parseFn) {

packages/pg/lib/native/client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ Client.prototype.cancel = function (query) {
286286
}
287287
}
288288

289+
Client.prototype.cancelActiveQuery = function () {
290+
return new Promise((resolve, reject) => {
291+
this.native.cancel((err) => err ? reject(err) : resolve())
292+
})
293+
}
294+
289295
Client.prototype.setTypeParser = function (oid, format, parseFn) {
290296
return this._types.setTypeParser(oid, format, parseFn)
291297
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict"
2+
var helper = require('./../test-helper')
3+
var assert = require('assert')
4+
5+
const suite = new helper.Suite()
6+
7+
suite.testAsync('All queries should return a result array', async () => {
8+
const pool = new helper.pg.Pool()
9+
const client = await pool.connect()
10+
const cancledPromise = client.query('SELECT pg_sleep(100000)')
11+
.then(() => {
12+
throw new Error('this should not resolve because query is cancled')
13+
})
14+
.catch(err => {
15+
return err
16+
})
17+
await client.cancelActiveQuery()
18+
const err = await cancledPromise
19+
assert(err instanceof Error)
20+
// make sure client is still usable
21+
const { rows } = await client.query('SELECT 1 as "foo"')
22+
assert.strictEqual(rows.length, 1)
23+
assert.deepStrictEqual(rows[0], { foo: 1 })
24+
await client.end()
25+
})

0 commit comments

Comments
 (0)