pool.js 5.2 KB

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