PoolCluster.js 5.5 KB

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