index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. 'use strict'
  2. const EventEmitter = require('events').EventEmitter
  3. const NOOP = function () {}
  4. const removeWhere = (list, predicate) => {
  5. const i = list.findIndex(predicate)
  6. return i === -1 ? undefined : list.splice(i, 1)[0]
  7. }
  8. class IdleItem {
  9. constructor(client, idleListener, timeoutId) {
  10. this.client = client
  11. this.idleListener = idleListener
  12. this.timeoutId = timeoutId
  13. }
  14. }
  15. class PendingItem {
  16. constructor(callback) {
  17. this.callback = callback
  18. }
  19. }
  20. function throwOnDoubleRelease() {
  21. throw new Error('Release called on client which has already been released to the pool.')
  22. }
  23. function promisify(Promise, callback) {
  24. if (callback) {
  25. return { callback: callback, result: undefined }
  26. }
  27. let rej
  28. let res
  29. const cb = function (err, client) {
  30. err ? rej(err) : res(client)
  31. }
  32. const result = new Promise(function (resolve, reject) {
  33. res = resolve
  34. rej = reject
  35. })
  36. return { callback: cb, result: result }
  37. }
  38. function makeIdleListener(pool, client) {
  39. return function idleListener(err) {
  40. err.client = client
  41. client.removeListener('error', idleListener)
  42. client.on('error', () => {
  43. pool.log('additional client error after disconnection due to error', err)
  44. })
  45. pool._remove(client)
  46. // TODO - document that once the pool emits an error
  47. // the client has already been closed & purged and is unusable
  48. pool.emit('error', err, client)
  49. }
  50. }
  51. class Pool extends EventEmitter {
  52. constructor(options, Client) {
  53. super()
  54. this.options = Object.assign({}, options)
  55. if (options != null && 'password' in options) {
  56. // "hiding" the password so it doesn't show up in stack traces
  57. // or if the client is console.logged
  58. Object.defineProperty(this.options, 'password', {
  59. configurable: true,
  60. enumerable: false,
  61. writable: true,
  62. value: options.password,
  63. })
  64. }
  65. if (options != null && options.ssl && options.ssl.key) {
  66. // "hiding" the ssl->key so it doesn't show up in stack traces
  67. // or if the client is console.logged
  68. Object.defineProperty(this.options.ssl, 'key', {
  69. enumerable: false,
  70. })
  71. }
  72. this.options.max = this.options.max || this.options.poolSize || 10
  73. this.options.maxUses = this.options.maxUses || Infinity
  74. this.options.allowExitOnIdle = this.options.allowExitOnIdle || false
  75. this.options.maxLifetimeSeconds = this.options.maxLifetimeSeconds || 0
  76. this.log = this.options.log || function () {}
  77. this.Client = this.options.Client || Client || require('pg').Client
  78. this.Promise = this.options.Promise || global.Promise
  79. if (typeof this.options.idleTimeoutMillis === 'undefined') {
  80. this.options.idleTimeoutMillis = 10000
  81. }
  82. this._clients = []
  83. this._idle = []
  84. this._expired = new WeakSet()
  85. this._pendingQueue = []
  86. this._endCallback = undefined
  87. this.ending = false
  88. this.ended = false
  89. }
  90. _isFull() {
  91. return this._clients.length >= this.options.max
  92. }
  93. _pulseQueue() {
  94. this.log('pulse queue')
  95. if (this.ended) {
  96. this.log('pulse queue ended')
  97. return
  98. }
  99. if (this.ending) {
  100. this.log('pulse queue on ending')
  101. if (this._idle.length) {
  102. this._idle.slice().map((item) => {
  103. this._remove(item.client)
  104. })
  105. }
  106. if (!this._clients.length) {
  107. this.ended = true
  108. this._endCallback()
  109. }
  110. return
  111. }
  112. // if we don't have any waiting, do nothing
  113. if (!this._pendingQueue.length) {
  114. this.log('no queued requests')
  115. return
  116. }
  117. // if we don't have any idle clients and we have no more room do nothing
  118. if (!this._idle.length && this._isFull()) {
  119. return
  120. }
  121. const pendingItem = this._pendingQueue.shift()
  122. if (this._idle.length) {
  123. const idleItem = this._idle.pop()
  124. clearTimeout(idleItem.timeoutId)
  125. const client = idleItem.client
  126. client.ref && client.ref()
  127. const idleListener = idleItem.idleListener
  128. return this._acquireClient(client, pendingItem, idleListener, false)
  129. }
  130. if (!this._isFull()) {
  131. return this.newClient(pendingItem)
  132. }
  133. throw new Error('unexpected condition')
  134. }
  135. _remove(client) {
  136. const removed = removeWhere(this._idle, (item) => item.client === client)
  137. if (removed !== undefined) {
  138. clearTimeout(removed.timeoutId)
  139. }
  140. this._clients = this._clients.filter((c) => c !== client)
  141. client.end()
  142. this.emit('remove', client)
  143. }
  144. connect(cb) {
  145. if (this.ending) {
  146. const err = new Error('Cannot use a pool after calling end on the pool')
  147. return cb ? cb(err) : this.Promise.reject(err)
  148. }
  149. const response = promisify(this.Promise, cb)
  150. const result = response.result
  151. // if we don't have to connect a new client, don't do so
  152. if (this._isFull() || this._idle.length) {
  153. // if we have idle clients schedule a pulse immediately
  154. if (this._idle.length) {
  155. process.nextTick(() => this._pulseQueue())
  156. }
  157. if (!this.options.connectionTimeoutMillis) {
  158. this._pendingQueue.push(new PendingItem(response.callback))
  159. return result
  160. }
  161. const queueCallback = (err, res, done) => {
  162. clearTimeout(tid)
  163. response.callback(err, res, done)
  164. }
  165. const pendingItem = new PendingItem(queueCallback)
  166. // set connection timeout on checking out an existing client
  167. const tid = setTimeout(() => {
  168. // remove the callback from pending waiters because
  169. // we're going to call it with a timeout error
  170. removeWhere(this._pendingQueue, (i) => i.callback === queueCallback)
  171. pendingItem.timedOut = true
  172. response.callback(new Error('timeout exceeded when trying to connect'))
  173. }, this.options.connectionTimeoutMillis)
  174. this._pendingQueue.push(pendingItem)
  175. return result
  176. }
  177. this.newClient(new PendingItem(response.callback))
  178. return result
  179. }
  180. newClient(pendingItem) {
  181. const client = new this.Client(this.options)
  182. this._clients.push(client)
  183. const idleListener = makeIdleListener(this, client)
  184. this.log('checking client timeout')
  185. // connection timeout logic
  186. let tid
  187. let timeoutHit = false
  188. if (this.options.connectionTimeoutMillis) {
  189. tid = setTimeout(() => {
  190. this.log('ending client due to timeout')
  191. timeoutHit = true
  192. // force kill the node driver, and let libpq do its teardown
  193. client.connection ? client.connection.stream.destroy() : client.end()
  194. }, this.options.connectionTimeoutMillis)
  195. }
  196. this.log('connecting new client')
  197. client.connect((err) => {
  198. if (tid) {
  199. clearTimeout(tid)
  200. }
  201. client.on('error', idleListener)
  202. if (err) {
  203. this.log('client failed to connect', err)
  204. // remove the dead client from our list of clients
  205. this._clients = this._clients.filter((c) => c !== client)
  206. if (timeoutHit) {
  207. err.message = 'Connection terminated due to connection timeout'
  208. }
  209. // this client won’t be released, so move on immediately
  210. this._pulseQueue()
  211. if (!pendingItem.timedOut) {
  212. pendingItem.callback(err, undefined, NOOP)
  213. }
  214. } else {
  215. this.log('new client connected')
  216. if (this.options.maxLifetimeSeconds !== 0) {
  217. setTimeout(() => {
  218. this.log('ending client due to expired lifetime')
  219. this._expired.add(client)
  220. const idleIndex = this._idle.findIndex((idleItem) => idleItem.client === client)
  221. if (idleIndex !== -1) {
  222. this._acquireClient(
  223. client,
  224. new PendingItem((err, client, clientRelease) => clientRelease()),
  225. idleListener,
  226. false
  227. )
  228. }
  229. }, this.options.maxLifetimeSeconds * 1000)
  230. }
  231. return this._acquireClient(client, pendingItem, idleListener, true)
  232. }
  233. })
  234. }
  235. // acquire a client for a pending work item
  236. _acquireClient(client, pendingItem, idleListener, isNew) {
  237. if (isNew) {
  238. this.emit('connect', client)
  239. }
  240. this.emit('acquire', client)
  241. client.release = this._releaseOnce(client, idleListener)
  242. client.removeListener('error', idleListener)
  243. if (!pendingItem.timedOut) {
  244. if (isNew && this.options.verify) {
  245. this.options.verify(client, (err) => {
  246. if (err) {
  247. client.release(err)
  248. return pendingItem.callback(err, undefined, NOOP)
  249. }
  250. pendingItem.callback(undefined, client, client.release)
  251. })
  252. } else {
  253. pendingItem.callback(undefined, client, client.release)
  254. }
  255. } else {
  256. if (isNew && this.options.verify) {
  257. this.options.verify(client, client.release)
  258. } else {
  259. client.release()
  260. }
  261. }
  262. }
  263. // returns a function that wraps _release and throws if called more than once
  264. _releaseOnce(client, idleListener) {
  265. let released = false
  266. return (err) => {
  267. if (released) {
  268. throwOnDoubleRelease()
  269. }
  270. released = true
  271. this._release(client, idleListener, err)
  272. }
  273. }
  274. // release a client back to the poll, include an error
  275. // to remove it from the pool
  276. _release(client, idleListener, err) {
  277. client.on('error', idleListener)
  278. client._poolUseCount = (client._poolUseCount || 0) + 1
  279. // TODO(bmc): expose a proper, public interface _queryable and _ending
  280. if (err || this.ending || !client._queryable || client._ending || client._poolUseCount >= this.options.maxUses) {
  281. if (client._poolUseCount >= this.options.maxUses) {
  282. this.log('remove expended client')
  283. }
  284. this._remove(client)
  285. this._pulseQueue()
  286. return
  287. }
  288. const isExpired = this._expired.has(client)
  289. if (isExpired) {
  290. this.log('remove expired client')
  291. this._expired.delete(client)
  292. this._remove(client)
  293. this._pulseQueue()
  294. return
  295. }
  296. // idle timeout
  297. let tid
  298. if (this.options.idleTimeoutMillis) {
  299. tid = setTimeout(() => {
  300. this.log('remove idle client')
  301. this._remove(client)
  302. }, this.options.idleTimeoutMillis)
  303. if (this.options.allowExitOnIdle) {
  304. // allow Node to exit if this is all that's left
  305. tid.unref()
  306. }
  307. }
  308. if (this.options.allowExitOnIdle) {
  309. client.unref()
  310. }
  311. this._idle.push(new IdleItem(client, idleListener, tid))
  312. this._pulseQueue()
  313. }
  314. query(text, values, cb) {
  315. // guard clause against passing a function as the first parameter
  316. if (typeof text === 'function') {
  317. const response = promisify(this.Promise, text)
  318. setImmediate(function () {
  319. return response.callback(new Error('Passing a function as the first parameter to pool.query is not supported'))
  320. })
  321. return response.result
  322. }
  323. // allow plain text query without values
  324. if (typeof values === 'function') {
  325. cb = values
  326. values = undefined
  327. }
  328. const response = promisify(this.Promise, cb)
  329. cb = response.callback
  330. this.connect((err, client) => {
  331. if (err) {
  332. return cb(err)
  333. }
  334. let clientReleased = false
  335. const onError = (err) => {
  336. if (clientReleased) {
  337. return
  338. }
  339. clientReleased = true
  340. client.release(err)
  341. cb(err)
  342. }
  343. client.once('error', onError)
  344. this.log('dispatching query')
  345. client.query(text, values, (err, res) => {
  346. this.log('query dispatched')
  347. client.removeListener('error', onError)
  348. if (clientReleased) {
  349. return
  350. }
  351. clientReleased = true
  352. client.release(err)
  353. if (err) {
  354. return cb(err)
  355. } else {
  356. return cb(undefined, res)
  357. }
  358. })
  359. })
  360. return response.result
  361. }
  362. end(cb) {
  363. this.log('ending')
  364. if (this.ending) {
  365. const err = new Error('Called end on pool more than once')
  366. return cb ? cb(err) : this.Promise.reject(err)
  367. }
  368. this.ending = true
  369. const promised = promisify(this.Promise, cb)
  370. this._endCallback = promised.callback
  371. this._pulseQueue()
  372. return promised.result
  373. }
  374. get waitingCount() {
  375. return this._pendingQueue.length
  376. }
  377. get idleCount() {
  378. return this._idle.length
  379. }
  380. get expiredCount() {
  381. return this._clients.reduce((acc, client) => acc + (this._expired.has(client) ? 1 : 0), 0)
  382. }
  383. get totalCount() {
  384. return this._clients.length
  385. }
  386. }
  387. module.exports = Pool