MCPcopy
hub / github.com/brianc/node-postgres / connect

Method connect

packages/pg-pool/index.js:190–238  ·  view source on GitHub ↗
(cb)

Source from the content-addressed store, hash-verified

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)

Callers 15

queryMethod · 0.95
testForTypeCoercionFunction · 0.95
testPoolSizeFunction · 0.95
lifecycle-hooks.jsFile · 0.45
index.jsFile · 0.45
error-handling.jsFile · 0.45
sizing.jsFile · 0.45
events.jsFile · 0.45
idle-timeout.jsFile · 0.45
verify.jsFile · 0.45

Calls 7

_isFullMethod · 0.95
_pulseQueueMethod · 0.95
newClientMethod · 0.95
cbFunction · 0.85
promisifyFunction · 0.85
removeWhereFunction · 0.85
unrefMethod · 0.45

Tested by

no test coverage detected