pool.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict';
  2. const process = require('process');
  3. const mysql = require('../index.js');
  4. const EventEmitter = require('events').EventEmitter;
  5. const PoolConnection = require('./pool_connection.js');
  6. const Queue = require('denque');
  7. const Connection = require('./connection.js');
  8. function spliceConnection(queue, connection) {
  9. const len = queue.length;
  10. for (let i = 0; i < len; i++) {
  11. if (queue.get(i) === connection) {
  12. queue.removeOne(i);
  13. break;
  14. }
  15. }
  16. }
  17. class Pool extends EventEmitter {
  18. constructor(options) {
  19. super();
  20. this.config = options.config;
  21. this.config.connectionConfig.pool = this;
  22. this._allConnections = new Queue();
  23. this._freeConnections = new Queue();
  24. this._connectionQueue = new Queue();
  25. this._closed = false;
  26. }
  27. promise(promiseImpl) {
  28. const PromisePool = require('../promise').PromisePool;
  29. return new PromisePool(this, promiseImpl);
  30. }
  31. getConnection(cb) {
  32. if (this._closed) {
  33. return process.nextTick(() => cb(new Error('Pool is closed.')));
  34. }
  35. let connection;
  36. if (this._freeConnections.length > 0) {
  37. connection = this._freeConnections.shift();
  38. this.emit('acquire', connection);
  39. return process.nextTick(() => cb(null, connection));
  40. }
  41. if (
  42. this.config.connectionLimit === 0 ||
  43. this._allConnections.length < this.config.connectionLimit
  44. ) {
  45. connection = new PoolConnection(this, {
  46. config: this.config.connectionConfig
  47. });
  48. this._allConnections.push(connection);
  49. return connection.connect(err => {
  50. if (this._closed) {
  51. return cb(new Error('Pool is closed.'));
  52. }
  53. if (err) {
  54. return cb(err);
  55. }
  56. this.emit('connection', connection);
  57. this.emit('acquire', connection);
  58. return cb(null, connection);
  59. });
  60. }
  61. if (!this.config.waitForConnections) {
  62. return process.nextTick(() => cb(new Error('No connections available.')));
  63. }
  64. if (
  65. this.config.queueLimit &&
  66. this._connectionQueue.length >= this.config.queueLimit
  67. ) {
  68. return cb(new Error('Queue limit reached.'));
  69. }
  70. this.emit('enqueue');
  71. return this._connectionQueue.push(cb);
  72. }
  73. releaseConnection(connection) {
  74. let cb;
  75. if (!connection._pool) {
  76. // The connection has been removed from the pool and is no longer good.
  77. if (this._connectionQueue.length) {
  78. cb = this._connectionQueue.shift();
  79. process.nextTick(this.getConnection.bind(this, cb));
  80. }
  81. } else if (this._connectionQueue.length) {
  82. cb = this._connectionQueue.shift();
  83. process.nextTick(cb.bind(null, null, connection));
  84. } else {
  85. this._freeConnections.push(connection);
  86. this.emit('release', connection);
  87. }
  88. }
  89. end(cb) {
  90. this._closed = true;
  91. if (typeof cb !== 'function') {
  92. cb = function(err) {
  93. if (err) {
  94. throw err;
  95. }
  96. };
  97. }
  98. let calledBack = false;
  99. let closedConnections = 0;
  100. let connection;
  101. const endCB = function(err) {
  102. if (calledBack) {
  103. return;
  104. }
  105. if (err || ++closedConnections >= this._allConnections.length) {
  106. calledBack = true;
  107. cb(err);
  108. return;
  109. }
  110. }.bind(this);
  111. if (this._allConnections.length === 0) {
  112. endCB();
  113. return;
  114. }
  115. for (let i = 0; i < this._allConnections.length; i++) {
  116. connection = this._allConnections.get(i);
  117. connection._realEnd(endCB);
  118. }
  119. }
  120. query(sql, values, cb) {
  121. const cmdQuery = Connection.createQuery(
  122. sql,
  123. values,
  124. cb,
  125. this.config.connectionConfig
  126. );
  127. if (typeof cmdQuery.namedPlaceholders === 'undefined') {
  128. cmdQuery.namedPlaceholders = this.config.connectionConfig.namedPlaceholders;
  129. }
  130. this.getConnection((err, conn) => {
  131. if (err) {
  132. if (typeof cmdQuery.onResult === 'function') {
  133. cmdQuery.onResult(err);
  134. } else {
  135. cmdQuery.emit('error', err);
  136. }
  137. return;
  138. }
  139. try {
  140. conn.query(cmdQuery).once('end', () => {
  141. conn.release();
  142. });
  143. } catch (e) {
  144. conn.release();
  145. throw e;
  146. }
  147. });
  148. return cmdQuery;
  149. }
  150. execute(sql, values, cb) {
  151. // TODO construct execute command first here and pass it to connection.execute
  152. // so that polymorphic arguments logic is there in one place
  153. if (typeof values === 'function') {
  154. cb = values;
  155. values = [];
  156. }
  157. this.getConnection((err, conn) => {
  158. if (err) {
  159. return cb(err);
  160. }
  161. try {
  162. conn.execute(sql, values, cb).once('end', () => {
  163. conn.release();
  164. });
  165. } catch (e) {
  166. conn.release();
  167. throw e;
  168. }
  169. });
  170. }
  171. _removeConnection(connection) {
  172. // Remove connection from all connections
  173. spliceConnection(this._allConnections, connection);
  174. // Remove connection from free connections
  175. spliceConnection(this._freeConnections, connection);
  176. this.releaseConnection(connection);
  177. }
  178. format(sql, values) {
  179. return mysql.format(
  180. sql,
  181. values,
  182. this.config.connectionConfig.stringifyObjects,
  183. this.config.connectionConfig.timezone
  184. );
  185. }
  186. escape(value) {
  187. return mysql.escape(
  188. value,
  189. this.config.connectionConfig.stringifyObjects,
  190. this.config.connectionConfig.timezone
  191. );
  192. }
  193. escapeId(value) {
  194. return mysql.escapeId(value, false);
  195. }
  196. }
  197. module.exports = Pool;