pool.js 9.3 KB

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