Skip to content

Commit

Permalink
feat: allow specifying a timeout on a per query base (#3074)
Browse files Browse the repository at this point in the history
  • Loading branch information
srieding authored Jun 19, 2024
1 parent b7d5b3b commit 9baa56e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ class Client extends EventEmitter {
query.callback = query.callback || values
}
} else {
readTimeout = this.connectionParameters.query_timeout
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
query = new Query(config, values, callback)
if (!query.callback) {
result = new this._Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Client.prototype.query = function (config, values, callback) {
config.callback = values
}
} else {
readTimeout = this.connectionParameters.query_timeout
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
query = new NativeQuery(config, values, callback)
if (!query.callback) {
let resolveOut, rejectOut
Expand Down
15 changes: 15 additions & 0 deletions packages/pg/test/integration/client/api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ suite.test('query no timeout', (cb) => {
})
})

suite.test('query with timeout on query basis', (cb) => {
const pool = new pg.Pool()
pool.connect().then((client) => {
client.query(
{text: 'SELECT pg_sleep(20)', query_timeout: 1000},

Check failure on line 109 in packages/pg/test/integration/client/api-tests.js

View workflow job for this annotation

GitHub Actions / lint

Replace `text:·'SELECT·pg_sleep(20)',·query_timeout:·1000` with `·text:·'SELECT·pg_sleep(20)',·query_timeout:·1000·`
assert.calls(function (err, result) {
assert(err)
assert(err.message === 'Query read timeout')
client.release()
pool.end(cb)
})
)
})
})

suite.test('callback API', (done) => {
const client = new helper.Client()
client.query('CREATE TEMP TABLE peep(name text)')
Expand Down

0 comments on commit 9baa56e

Please sign in to comment.