Pool.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. var mysql = require('../');
  2. var Connection = require('./Connection');
  3. var EventEmitter = require('events').EventEmitter;
  4. var Util = require('util');
  5. var PoolConnection = require('./PoolConnection');
  6. module.exports = Pool;
  7. Util.inherits(Pool, EventEmitter);
  8. function Pool(options) {
  9. EventEmitter.call(this);
  10. this.config = options.config;
  11. this.config.connectionConfig.pool = this;
  12. this._acquiringConnections = [];
  13. this._allConnections = [];
  14. this._freeConnections = [];
  15. this._connectionQueue = [];
  16. this._closed = false;
  17. }
  18. Pool.prototype.getConnection = function (cb) {
  19. if (this._closed) {
  20. return process.nextTick(function(){
  21. return cb(new Error('Pool is closed.'));
  22. });
  23. }
  24. var connection;
  25. var pool = this;
  26. if (this._freeConnections.length > 0) {
  27. connection = this._freeConnections.shift();
  28. return this.acquireConnection(connection, cb);
  29. }
  30. if (this.config.connectionLimit === 0 || this._allConnections.length < this.config.connectionLimit) {
  31. connection = new PoolConnection(this, { config: this.config.newConnectionConfig() });
  32. this._acquiringConnections.push(connection);
  33. this._allConnections.push(connection);
  34. return connection.connect({timeout: this.config.acquireTimeout}, function onConnect(err) {
  35. spliceConnection(pool._acquiringConnections, connection);
  36. if (pool._closed) {
  37. err = new Error('Pool is closed.');
  38. }
  39. if (err) {
  40. pool._purgeConnection(connection);
  41. cb(err);
  42. return;
  43. }
  44. pool.emit('connection', connection);
  45. cb(null, connection);
  46. });
  47. }
  48. if (!this.config.waitForConnections) {
  49. return process.nextTick(function(){
  50. return cb(new Error('No connections available.'));
  51. });
  52. }
  53. this._enqueueCallback(cb);
  54. };
  55. Pool.prototype.acquireConnection = function acquireConnection(connection, cb) {
  56. if (connection._pool !== this) {
  57. throw new Error('Connection acquired from wrong pool.');
  58. }
  59. var pool = this;
  60. this._acquiringConnections.push(connection);
  61. connection.ping({timeout: this.config.acquireTimeout}, function onPing(err) {
  62. spliceConnection(pool._acquiringConnections, connection);
  63. if (pool._closed) {
  64. err = new Error('Pool is closed.');
  65. }
  66. if (err) {
  67. pool._connectionQueue.unshift(cb);
  68. pool._purgeConnection(connection);
  69. return;
  70. }
  71. cb(null, connection);
  72. });
  73. };
  74. Pool.prototype.releaseConnection = function releaseConnection(connection) {
  75. var cb;
  76. if (this._acquiringConnections.indexOf(connection) !== -1) {
  77. // connection is being acquired
  78. return;
  79. }
  80. if (connection._pool) {
  81. if (connection._pool !== this) {
  82. throw new Error('Connection released to wrong pool');
  83. }
  84. if (connection._purge) {
  85. // purge connection from pool
  86. this._purgeConnection(connection);
  87. return;
  88. } else if (this._freeConnections.indexOf(connection) !== -1) {
  89. // connection already in free connection pool
  90. // this won't catch all double-release cases
  91. throw new Error('Connection already released');
  92. } else {
  93. // add connection to end of free queue
  94. this._freeConnections.push(connection);
  95. }
  96. }
  97. while (this._closed && this._connectionQueue.length) {
  98. // empty the connection queue
  99. cb = this._connectionQueue.shift();
  100. process.nextTick(cb.bind(null, new Error('Pool is closed.')));
  101. }
  102. if (this._connectionQueue.length) {
  103. cb = this._connectionQueue.shift();
  104. this.getConnection(cb);
  105. }
  106. };
  107. Pool.prototype.end = function (cb) {
  108. this._closed = true;
  109. if (typeof cb != "function") {
  110. cb = function (err) {
  111. if (err) throw err;
  112. };
  113. }
  114. var calledBack = false;
  115. var waitingClose = this._allConnections.length;
  116. function onEnd(err) {
  117. if (calledBack) {
  118. return;
  119. }
  120. if (err || --waitingClose === 0) {
  121. calledBack = true;
  122. return cb(err);
  123. }
  124. }
  125. if (waitingClose === 0) {
  126. return process.nextTick(cb);
  127. }
  128. while (this._allConnections.length !== 0) {
  129. this._purgeConnection(this._allConnections[0], onEnd);
  130. }
  131. };
  132. Pool.prototype.query = function (sql, values, cb) {
  133. var query = Connection.createQuery(sql, values, cb);
  134. if (!(typeof sql === 'object' && 'typeCast' in sql)) {
  135. query.typeCast = this.config.connectionConfig.typeCast;
  136. }
  137. if (this.config.connectionConfig.trace) {
  138. // Long stack trace support
  139. query._callSite = new Error;
  140. }
  141. this.getConnection(function (err, conn) {
  142. if (err) {
  143. query.on('error', function () {});
  144. query.end(err);
  145. return;
  146. }
  147. // Release connection based off event
  148. query.once('end', function() {
  149. conn.release();
  150. });
  151. conn.query(query);
  152. });
  153. return query;
  154. };
  155. Pool.prototype._enqueueCallback = function _enqueueCallback(callback) {
  156. if (this.config.queueLimit && this._connectionQueue.length >= this.config.queueLimit) {
  157. process.nextTick(function () {
  158. var err = new Error('Queue limit reached.');
  159. err.code = 'POOL_ENQUEUELIMIT';
  160. callback(err);
  161. });
  162. return;
  163. }
  164. // Bind to domain, as dequeue will likely occur in a different domain
  165. var cb = process.domain
  166. ? process.domain.bind(callback)
  167. : callback;
  168. this._connectionQueue.push(cb);
  169. this.emit('enqueue');
  170. };
  171. Pool.prototype._purgeConnection = function _purgeConnection(connection, callback) {
  172. var cb = callback || function () {};
  173. if (connection.state === 'disconnected') {
  174. connection.destroy();
  175. }
  176. this._removeConnection(connection);
  177. if (connection.state !== 'disconnected' && !connection._protocol._quitSequence) {
  178. connection._realEnd(cb);
  179. return;
  180. }
  181. process.nextTick(cb);
  182. };
  183. Pool.prototype._removeConnection = function(connection) {
  184. connection._pool = null;
  185. // Remove connection from all connections
  186. spliceConnection(this._allConnections, connection);
  187. // Remove connection from free connections
  188. spliceConnection(this._freeConnections, connection);
  189. this.releaseConnection(connection);
  190. };
  191. Pool.prototype.escape = function(value) {
  192. return mysql.escape(value, this.config.connectionConfig.stringifyObjects, this.config.connectionConfig.timezone);
  193. };
  194. Pool.prototype.escapeId = function escapeId(value) {
  195. return mysql.escapeId(value, false);
  196. };
  197. function spliceConnection(array, connection) {
  198. var index;
  199. if ((index = array.indexOf(connection)) !== -1) {
  200. // Remove connection from all connections
  201. array.splice(index, 1);
  202. }
  203. }