pool.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. 'use strict';
  2. const process = require('process');
  3. const SqlString = require('sql-escaper');
  4. const EventEmitter = require('events').EventEmitter;
  5. const PoolConnection = require('../pool_connection.js');
  6. const Queue = require('denque');
  7. const BaseConnection = require('./connection.js');
  8. const Errors = require('../constants/errors.js');
  9. // Source: https://github.com/go-sql-driver/mysql/blob/76c00e35a8d48f8f70f0e7dffe584692bd3fa612/packets.go#L598-L613
  10. function isReadOnlyError(err) {
  11. if (!err || !err.errno) {
  12. return false;
  13. }
  14. // 1792: ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION
  15. // 1290: ER_OPTION_PREVENTS_STATEMENT (returned by Aurora during failover)
  16. // 1836: ER_READ_ONLY_MODE
  17. return (
  18. err.errno === Errors.ER_OPTION_PREVENTS_STATEMENT ||
  19. err.errno === Errors.ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION ||
  20. err.errno === Errors.ER_READ_ONLY_MODE
  21. );
  22. }
  23. function spliceConnection(queue, connection) {
  24. const len = queue.length;
  25. for (let i = 0; i < len; i++) {
  26. if (queue.get(i) === connection) {
  27. queue.removeOne(i);
  28. break;
  29. }
  30. }
  31. }
  32. class BasePool extends EventEmitter {
  33. constructor(options) {
  34. super();
  35. this.config = options.config;
  36. this.config.connectionConfig.pool = this;
  37. this._allConnections = new Queue();
  38. this._freeConnections = new Queue();
  39. this._connectionQueue = new Queue();
  40. this._closed = false;
  41. if (this.config.maxIdle < this.config.connectionLimit) {
  42. // create idle connection timeout automatically release job
  43. this._removeIdleTimeoutConnections();
  44. }
  45. }
  46. getConnection(cb) {
  47. if (this._closed) {
  48. return process.nextTick(() => cb(new Error('Pool is closed.')));
  49. }
  50. let connection;
  51. if (this._freeConnections.length > 0) {
  52. connection = this._freeConnections.pop();
  53. this.emit('acquire', connection);
  54. return process.nextTick(() => cb(null, connection));
  55. }
  56. if (
  57. this.config.connectionLimit === 0 ||
  58. this._allConnections.length < this.config.connectionLimit
  59. ) {
  60. connection = new PoolConnection(this, {
  61. config: this.config.connectionConfig,
  62. });
  63. this._allConnections.push(connection);
  64. return connection.connect((err) => {
  65. if (this._closed) {
  66. return cb(new Error('Pool is closed.'));
  67. }
  68. if (err) {
  69. return cb(err);
  70. }
  71. this.emit('connection', connection);
  72. this.emit('acquire', connection);
  73. return cb(null, connection);
  74. });
  75. }
  76. if (!this.config.waitForConnections) {
  77. return process.nextTick(() => cb(new Error('No connections available.')));
  78. }
  79. if (
  80. this.config.queueLimit &&
  81. this._connectionQueue.length >= this.config.queueLimit
  82. ) {
  83. return cb(new Error('Queue limit reached.'));
  84. }
  85. this.emit('enqueue');
  86. return this._connectionQueue.push(cb);
  87. }
  88. releaseConnection(connection) {
  89. let cb;
  90. if (!connection._pool) {
  91. // The connection has been removed from the pool and is no longer good.
  92. if (this._connectionQueue.length) {
  93. cb = this._connectionQueue.shift();
  94. process.nextTick(this.getConnection.bind(this, cb));
  95. }
  96. } else if (this._connectionQueue.length) {
  97. cb = this._connectionQueue.shift();
  98. process.nextTick(cb.bind(null, null, connection));
  99. } else {
  100. this._freeConnections.push(connection);
  101. this.emit('release', connection);
  102. }
  103. }
  104. [Symbol.dispose]() {
  105. if (!this._closed) {
  106. this.end();
  107. }
  108. }
  109. end(cb) {
  110. this._closed = true;
  111. clearTimeout(this._removeIdleTimeoutConnectionsTimer);
  112. if (typeof cb !== 'function') {
  113. cb = function (err) {
  114. if (err) {
  115. throw err;
  116. }
  117. };
  118. }
  119. let calledBack = false;
  120. let closedConnections = 0;
  121. let connection;
  122. const endCB = function (err) {
  123. if (calledBack) {
  124. return;
  125. }
  126. if (err || ++closedConnections >= this._allConnections.length) {
  127. calledBack = true;
  128. cb(err);
  129. return;
  130. }
  131. }.bind(this);
  132. if (this._allConnections.length === 0) {
  133. endCB();
  134. return;
  135. }
  136. for (let i = 0; i < this._allConnections.length; i++) {
  137. connection = this._allConnections.get(i);
  138. connection._realEnd(endCB);
  139. }
  140. }
  141. query(sql, values, cb) {
  142. const cmdQuery = BaseConnection.createQuery(
  143. sql,
  144. values,
  145. cb,
  146. this.config.connectionConfig
  147. );
  148. if (typeof cmdQuery.namedPlaceholders === 'undefined') {
  149. cmdQuery.namedPlaceholders =
  150. this.config.connectionConfig.namedPlaceholders;
  151. }
  152. this.getConnection((err, conn) => {
  153. if (err) {
  154. if (typeof cmdQuery.onResult === 'function') {
  155. cmdQuery.onResult(err);
  156. } else {
  157. cmdQuery.emit('error', err);
  158. }
  159. return;
  160. }
  161. try {
  162. let queryError = null;
  163. const origOnResult = cmdQuery.onResult;
  164. if (origOnResult) {
  165. cmdQuery.onResult = function (err, rows, fields) {
  166. queryError = err || null;
  167. origOnResult(err, rows, fields);
  168. };
  169. } else {
  170. cmdQuery.once('error', (err) => {
  171. queryError = err;
  172. });
  173. }
  174. conn.query(cmdQuery).once('end', () => {
  175. if (isReadOnlyError(queryError)) {
  176. conn.destroy();
  177. } else {
  178. conn.release();
  179. }
  180. });
  181. } catch (e) {
  182. conn.release();
  183. throw e;
  184. }
  185. });
  186. return cmdQuery;
  187. }
  188. execute(sql, values, cb) {
  189. // TODO construct execute command first here and pass it to connection.execute
  190. // so that polymorphic arguments logic is there in one place
  191. if (typeof values === 'function') {
  192. cb = values;
  193. values = [];
  194. }
  195. this.getConnection((err, conn) => {
  196. if (err) {
  197. return cb(err);
  198. }
  199. try {
  200. conn
  201. .execute(sql, values, (err, rows, fields) => {
  202. if (isReadOnlyError(err)) {
  203. conn.destroy();
  204. }
  205. cb(err, rows, fields);
  206. })
  207. .once('end', () => {
  208. conn.release();
  209. });
  210. } catch (e) {
  211. conn.release();
  212. return cb(e);
  213. }
  214. });
  215. }
  216. _removeConnection(connection) {
  217. // Remove connection from all connections
  218. spliceConnection(this._allConnections, connection);
  219. // Remove connection from free connections
  220. spliceConnection(this._freeConnections, connection);
  221. this.releaseConnection(connection);
  222. }
  223. _removeIdleTimeoutConnections() {
  224. if (this._removeIdleTimeoutConnectionsTimer) {
  225. clearTimeout(this._removeIdleTimeoutConnectionsTimer);
  226. }
  227. this._removeIdleTimeoutConnectionsTimer = setTimeout(() => {
  228. try {
  229. while (
  230. this._freeConnections.length > this.config.maxIdle ||
  231. (this._freeConnections.length > 0 &&
  232. Date.now() - this._freeConnections.get(0).lastActiveTime >
  233. this.config.idleTimeout)
  234. ) {
  235. if (this.config.connectionConfig.gracefulEnd) {
  236. this._freeConnections.get(0).end();
  237. } else {
  238. this._freeConnections.get(0).destroy();
  239. }
  240. }
  241. } finally {
  242. this._removeIdleTimeoutConnections();
  243. }
  244. }, 1000);
  245. }
  246. format(sql, values) {
  247. return SqlString.format(
  248. sql,
  249. values,
  250. this.config.connectionConfig.stringifyObjects,
  251. this.config.connectionConfig.timezone
  252. );
  253. }
  254. escape(value) {
  255. return SqlString.escape(
  256. value,
  257. this.config.connectionConfig.stringifyObjects,
  258. this.config.connectionConfig.timezone
  259. );
  260. }
  261. escapeId(value) {
  262. return SqlString.escapeId(value, false);
  263. }
  264. }
  265. module.exports = BasePool;