(cb)
| 188 | } |
| 189 | |
| 190 | connect(cb) { |
| 191 | if (this.ending) { |
| 192 | const err = new Error('Cannot use a pool after calling end on the pool') |
| 193 | return cb ? cb(err) : this.Promise.reject(err) |
| 194 | } |
| 195 | |
| 196 | const response = promisify(this.Promise, cb) |
| 197 | const result = response.result |
| 198 | |
| 199 | // if we don't have to connect a new client, don't do so |
| 200 | if (this._isFull() || this._idle.length) { |
| 201 | // if we have idle clients schedule a pulse immediately |
| 202 | if (this._idle.length) { |
| 203 | process.nextTick(() => this._pulseQueue()) |
| 204 | } |
| 205 | |
| 206 | if (!this.options.connectionTimeoutMillis) { |
| 207 | this._pendingQueue.push(new PendingItem(response.callback)) |
| 208 | return result |
| 209 | } |
| 210 | |
| 211 | const queueCallback = (err, res, done) => { |
| 212 | clearTimeout(tid) |
| 213 | response.callback(err, res, done) |
| 214 | } |
| 215 | |
| 216 | const pendingItem = new PendingItem(queueCallback) |
| 217 | |
| 218 | // set connection timeout on checking out an existing client |
| 219 | const tid = setTimeout(() => { |
| 220 | // remove the callback from pending waiters because |
| 221 | // we're going to call it with a timeout error |
| 222 | removeWhere(this._pendingQueue, (i) => i.callback === queueCallback) |
| 223 | pendingItem.timedOut = true |
| 224 | response.callback(new Error('timeout exceeded when trying to connect')) |
| 225 | }, this.options.connectionTimeoutMillis) |
| 226 | |
| 227 | if (tid.unref) { |
| 228 | tid.unref() |
| 229 | } |
| 230 | |
| 231 | this._pendingQueue.push(pendingItem) |
| 232 | return result |
| 233 | } |
| 234 | |
| 235 | this.newClient(new PendingItem(response.callback)) |
| 236 | |
| 237 | return result |
| 238 | } |
| 239 | |
| 240 | newClient(pendingItem) { |
| 241 | const client = new this.Client(this.options) |
no test coverage detected