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

feat: add ability to set per-request requestTimeout #1530

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions lib/base/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ class ConnectionPool extends EventEmitter {
* @return {Request}
*/

request () {
return new shared.driver.Request(this)
request (conf) {
return new shared.driver.Request(this, conf)
}

/**
Expand All @@ -518,8 +518,8 @@ class ConnectionPool extends EventEmitter {
* @return {Transaction}
*/

transaction () {
return new shared.driver.Transaction(this)
transaction (conf) {
return new shared.driver.Transaction(this, conf)
}

/**
Expand Down
9 changes: 7 additions & 2 deletions lib/base/prepared-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class PreparedStatement extends EventEmitter {
/**
* Creates a new Prepared Statement.
*
* @param {ConnectionPool|Transaction} [holder]
* @param {ConnectionPool|Transaction} [parent]
* @param {{[requestTimeout]: number}} [overrides]
*/

constructor (parent) {
constructor (parent, overrides = {}) {
super()

IDS.add(this, 'PreparedStatement')
Expand All @@ -33,6 +34,10 @@ class PreparedStatement extends EventEmitter {
this._handle = 0
this.prepared = false
this.parameters = {}
this.overrides = {}
if (typeof overrides?.requestTimeout === 'number') {
this.overrides.requestTimeout = overrides.requestTimeout
}
}

get config () {
Expand Down
9 changes: 7 additions & 2 deletions lib/base/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class Request extends EventEmitter {
/**
* Create new Request.
*
* @param {Connection|ConnectionPool|Transaction|PreparedStatement} parent If omitted, global connection is used instead.
* @param {Connection|ConnectionPool|Transaction|PreparedStatement} [parent] If omitted, global connection is used instead.
* @param {{[requestTimeout]: number}} [overrides]
*/

constructor (parent) {
constructor (parent, overrides) {
super()

IDS.add(this, 'Request')
Expand All @@ -41,6 +42,10 @@ class Request extends EventEmitter {
this.parameters = {}
this.stream = null
this.arrayRowMode = null
this.overrides = {}
if (typeof overrides?.requestTimeout === 'number') {
this.overrides.requestTimeout = overrides.requestTimeout
}
}

get paused () {
Expand Down
12 changes: 9 additions & 3 deletions lib/base/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class Transaction extends EventEmitter {
* Create new Transaction.
*
* @param {Connection} [parent] If ommited, global connection is used instead.
* @param {{[requestTimeout]: number}} [overrides]
*/

constructor (parent) {
constructor (parent, overrides = {}) {
super()

IDS.add(this, 'Transaction')
Expand All @@ -35,6 +36,10 @@ class Transaction extends EventEmitter {
this.parent = parent || globalConnection.pool
this.isolationLevel = Transaction.defaultIsolationLevel
this.name = ''
this.overrides = {}
if (typeof overrides?.requestTimeout === 'number') {
this.overrides.requestTimeout = overrides.requestTimeout
}
}

get config () {
Expand Down Expand Up @@ -196,11 +201,12 @@ class Transaction extends EventEmitter {
/**
* Returns new request using this transaction.
*
* @param {{[requestTimout]: number}} [config]
* @return {Request}
*/

request () {
return new shared.driver.Request(this)
request (config) {
return new shared.driver.Request(this, config ?? this.overrides)
}

/**
Expand Down
9 changes: 6 additions & 3 deletions lib/msnodesqlv8/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Request extends BaseRequest {
setImmediate(callback, new RequestError("You can't use table variables for bulk insert.", 'ENAME'))
}

this.parent.acquire(this, (err, connection) => {
this.parent.acquire(this, (err, connection, config) => {
let hasReturned = false
if (!err) {
debug('connection(%d): borrowed to request #%d', IDS.get(connection), IDS.get(this))
Expand Down Expand Up @@ -232,7 +232,10 @@ class Request extends BaseRequest {
objectid = table.path
}

return connection.queryRaw(`if object_id('${objectid.replace(/'/g, '\'\'')}') is null ${table.declare()}`, function (err) {
return connection.queryRaw({
query_str: `if object_id('${objectid.replace(/'/g, '\'\'')}') is null ${table.declare()}`,
query_timeout: (this.overrides.requestTimeout ?? config.requestTimeout) / 1000 // msnodesqlv8 timeouts are in seconds (<1 second not supported),
}, function (err) {
if (err) { return done(err) }
go()
})
Expand Down Expand Up @@ -377,7 +380,7 @@ class Request extends BaseRequest {

const req = connection.queryRaw({
query_str: command,
query_timeout: config.requestTimeout / 1000 // msnodesqlv8 timeouts are in seconds (<1 second not supported)
query_timeout: (this.overrides.requestTimeout ?? config.requestTimeout) / 1000 // msnodesqlv8 timeouts are in seconds (<1 second not supported)
}, params)

this._setCurrentRequest(req)
Expand Down
7 changes: 7 additions & 0 deletions lib/tedious/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ class Request extends BaseRequest {

connection.execBulkLoad(bulk, table.rows)
})
if (typeof this.overrides.requestTimeout === 'number') {
req.setTimeout(this.overrides.requestTimeout)
}
this._setCurrentRequest(req)

connection.execSqlBatch(req)
Expand Down Expand Up @@ -494,6 +497,10 @@ class Request extends BaseRequest {
}
})

if (typeof this.overrides.requestTimeout === 'number') {
req.setTimeout(this.overrides.requestTimeout)
}

this._setCurrentRequest(req)

req.on('columnMetadata', metadata => {
Expand Down