(pendingItem)
| 238 | } |
| 239 | |
| 240 | newClient(pendingItem) { |
| 241 | const client = new this.Client(this.options) |
| 242 | this._clients.push(client) |
| 243 | const idleListener = makeIdleListener(this, client) |
| 244 | |
| 245 | this.log('checking client timeout') |
| 246 | |
| 247 | // connection timeout logic |
| 248 | let tid |
| 249 | let timeoutHit = false |
| 250 | if (this.options.connectionTimeoutMillis) { |
| 251 | tid = setTimeout(() => { |
| 252 | if (client.connection) { |
| 253 | this.log('ending client due to timeout') |
| 254 | timeoutHit = true |
| 255 | client.connection.stream.destroy() |
| 256 | } else if (!client.isConnected()) { |
| 257 | this.log('ending client due to timeout') |
| 258 | timeoutHit = true |
| 259 | // force kill the node driver, and let libpq do its teardown |
| 260 | client.end() |
| 261 | } |
| 262 | }, this.options.connectionTimeoutMillis) |
| 263 | } |
| 264 | |
| 265 | this.log('connecting new client') |
| 266 | client.connect((err) => { |
| 267 | if (tid) { |
| 268 | clearTimeout(tid) |
| 269 | } |
| 270 | client.on('error', idleListener) |
| 271 | if (err) { |
| 272 | this.log('client failed to connect', err) |
| 273 | // remove the dead client from our list of clients |
| 274 | this._clients = this._clients.filter((c) => c !== client) |
| 275 | if (timeoutHit) { |
| 276 | err = new Error('Connection terminated due to connection timeout', { cause: err }) |
| 277 | } |
| 278 | |
| 279 | // this client won’t be released, so move on immediately |
| 280 | this._pulseQueue() |
| 281 | |
| 282 | if (!pendingItem.timedOut) { |
| 283 | pendingItem.callback(err, undefined, NOOP) |
| 284 | } |
| 285 | } else { |
| 286 | this.log('new client connected') |
| 287 | |
| 288 | if (this.options.onConnect) { |
| 289 | this._promiseTry(() => this.options.onConnect(client)).then( |
| 290 | () => { |
| 291 | this._afterConnect(client, pendingItem, idleListener) |
| 292 | }, |
| 293 | (hookErr) => { |
| 294 | this._clients = this._clients.filter((c) => c !== client) |
| 295 | client.end(() => { |
| 296 | this._pulseQueue() |
| 297 | if (!pendingItem.timedOut) { |
no test coverage detected