pool_cluster.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. const Pool = require('./pool.js');
  3. const PoolConfig = require('./pool_config.js');
  4. const EventEmitter = require('events').EventEmitter;
  5. /**
  6. * Selector
  7. */
  8. const makeSelector = {
  9. RR() {
  10. let index = 0;
  11. return clusterIds => clusterIds[index++ % clusterIds.length];
  12. },
  13. RANDOM() {
  14. return clusterIds =>
  15. clusterIds[Math.floor(Math.random() * clusterIds.length)];
  16. },
  17. ORDER() {
  18. return clusterIds => clusterIds[0];
  19. }
  20. };
  21. class PoolNamespace {
  22. constructor(cluster, pattern, selector) {
  23. this._cluster = cluster;
  24. this._pattern = pattern;
  25. this._selector = makeSelector[selector]();
  26. }
  27. getConnection(cb) {
  28. const clusterNode = this._getClusterNode();
  29. if (clusterNode === null) {
  30. return cb(new Error('Pool does Not exists.'));
  31. }
  32. return this._cluster._getConnection(clusterNode, (err, connection) => {
  33. if (err) {
  34. return cb(err);
  35. }
  36. if (connection === 'retry') {
  37. return this.getConnection(cb);
  38. }
  39. return cb(null, connection);
  40. });
  41. }
  42. _getClusterNode() {
  43. const foundNodeIds = this._cluster._findNodeIds(this._pattern);
  44. if (foundNodeIds.length === 0) {
  45. return null;
  46. }
  47. const nodeId =
  48. foundNodeIds.length === 1
  49. ? foundNodeIds[0]
  50. : this._selector(foundNodeIds);
  51. return this._cluster._getNode(nodeId);
  52. }
  53. }
  54. class PoolCluster extends EventEmitter {
  55. constructor(config) {
  56. super();
  57. config = config || {};
  58. this._canRetry =
  59. typeof config.canRetry === 'undefined' ? true : config.canRetry;
  60. this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
  61. this._defaultSelector = config.defaultSelector || 'RR';
  62. this._closed = false;
  63. this._lastId = 0;
  64. this._nodes = {};
  65. this._serviceableNodeIds = [];
  66. this._namespaces = {};
  67. this._findCaches = {};
  68. }
  69. of(pattern, selector) {
  70. pattern = pattern || '*';
  71. selector = selector || this._defaultSelector;
  72. selector = selector.toUpperCase();
  73. if (!makeSelector[selector] === 'undefined') {
  74. selector = this._defaultSelector;
  75. }
  76. const key = pattern + selector;
  77. if (typeof this._namespaces[key] === 'undefined') {
  78. this._namespaces[key] = new PoolNamespace(this, pattern, selector);
  79. }
  80. return this._namespaces[key];
  81. }
  82. add(id, config) {
  83. if (typeof id === 'object') {
  84. config = id;
  85. id = `CLUSTER::${++this._lastId}`;
  86. }
  87. if (typeof this._nodes[id] === 'undefined') {
  88. this._nodes[id] = {
  89. id: id,
  90. errorCount: 0,
  91. pool: new Pool({ config: new PoolConfig(config) })
  92. };
  93. this._serviceableNodeIds.push(id);
  94. this._clearFindCaches();
  95. }
  96. }
  97. getConnection(pattern, selector, cb) {
  98. let namespace;
  99. if (typeof pattern === 'function') {
  100. cb = pattern;
  101. namespace = this.of();
  102. } else {
  103. if (typeof selector === 'function') {
  104. cb = selector;
  105. selector = this._defaultSelector;
  106. }
  107. namespace = this.of(pattern, selector);
  108. }
  109. namespace.getConnection(cb);
  110. }
  111. end(callback) {
  112. const cb =
  113. callback !== undefined
  114. ? callback
  115. : err => {
  116. if (err) {
  117. throw err;
  118. }
  119. };
  120. if (this._closed) {
  121. process.nextTick(cb);
  122. return;
  123. }
  124. this._closed = true;
  125. let calledBack = false;
  126. let waitingClose = 0;
  127. const onEnd = err => {
  128. if (!calledBack && (err || --waitingClose <= 0)) {
  129. calledBack = true;
  130. return cb(err);
  131. }
  132. };
  133. for (const id in this._nodes) {
  134. waitingClose++;
  135. this._nodes[id].pool.end();
  136. }
  137. if (waitingClose === 0) {
  138. process.nextTick(onEnd);
  139. }
  140. }
  141. _findNodeIds(pattern) {
  142. if (typeof this._findCaches[pattern] !== 'undefined') {
  143. return this._findCaches[pattern];
  144. }
  145. let foundNodeIds;
  146. if (pattern === '*') {
  147. // all
  148. foundNodeIds = this._serviceableNodeIds;
  149. } else if (this._serviceableNodeIds.indexOf(pattern) !== -1) {
  150. // one
  151. foundNodeIds = [pattern];
  152. } else {
  153. // wild matching
  154. const keyword = pattern.substring(pattern.length - 1, 0);
  155. foundNodeIds = this._serviceableNodeIds.filter(id =>
  156. id.startsWith(keyword)
  157. );
  158. }
  159. this._findCaches[pattern] = foundNodeIds;
  160. return foundNodeIds;
  161. }
  162. _getNode(id) {
  163. return this._nodes[id] || null;
  164. }
  165. _increaseErrorCount(node) {
  166. if (++node.errorCount >= this._removeNodeErrorCount) {
  167. const index = this._serviceableNodeIds.indexOf(node.id);
  168. if (index !== -1) {
  169. this._serviceableNodeIds.splice(index, 1);
  170. delete this._nodes[node.id];
  171. this._clearFindCaches();
  172. node.pool.end();
  173. this.emit('remove', node.id);
  174. }
  175. }
  176. }
  177. _decreaseErrorCount(node) {
  178. if (node.errorCount > 0) {
  179. --node.errorCount;
  180. }
  181. }
  182. _getConnection(node, cb) {
  183. node.pool.getConnection((err, connection) => {
  184. if (err) {
  185. this._increaseErrorCount(node);
  186. if (this._canRetry) {
  187. // REVIEW: this seems wrong?
  188. this.emit('warn', err);
  189. // eslint-disable-next-line no-console
  190. console.warn(`[Error] PoolCluster : ${err}`);
  191. return cb(null, 'retry');
  192. }
  193. return cb(err);
  194. }
  195. this._decreaseErrorCount(node);
  196. connection._clusterId = node.id;
  197. return cb(null, connection);
  198. });
  199. }
  200. _clearFindCaches() {
  201. this._findCaches = {};
  202. }
  203. }
  204. module.exports = PoolCluster;