PoolNamespace.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var Connection = require('./Connection');
  2. var PoolSelector = require('./PoolSelector');
  3. module.exports = PoolNamespace;
  4. /**
  5. * PoolNamespace
  6. * @constructor
  7. * @param {PoolCluster} cluster The parent cluster for the namespace
  8. * @param {string} pattern The selection pattern to use
  9. * @param {string} selector The selector name to use
  10. * @public
  11. */
  12. function PoolNamespace(cluster, pattern, selector) {
  13. this._cluster = cluster;
  14. this._pattern = pattern;
  15. this._selector = new PoolSelector[selector]();
  16. }
  17. PoolNamespace.prototype.getConnection = function(cb) {
  18. var clusterNode = this._getClusterNode();
  19. var cluster = this._cluster;
  20. var namespace = this;
  21. if (clusterNode === null) {
  22. var err = null;
  23. if (this._cluster._findNodeIds(this._pattern, true).length !== 0) {
  24. err = new Error('Pool does not have online node.');
  25. err.code = 'POOL_NONEONLINE';
  26. } else {
  27. err = new Error('Pool does not exist.');
  28. err.code = 'POOL_NOEXIST';
  29. }
  30. cb(err);
  31. return;
  32. }
  33. cluster._getConnection(clusterNode, function(err, connection) {
  34. var retry = err && cluster._canRetry
  35. && cluster._findNodeIds(namespace._pattern).length !== 0;
  36. if (retry) {
  37. namespace.getConnection(cb);
  38. return;
  39. }
  40. if (err) {
  41. cb(err);
  42. return;
  43. }
  44. cb(null, connection);
  45. });
  46. };
  47. PoolNamespace.prototype.query = function (sql, values, cb) {
  48. var cluster = this._cluster;
  49. var clusterNode = this._getClusterNode();
  50. var query = Connection.createQuery(sql, values, cb);
  51. var namespace = this;
  52. if (clusterNode === null) {
  53. var err = null;
  54. if (this._cluster._findNodeIds(this._pattern, true).length !== 0) {
  55. err = new Error('Pool does not have online node.');
  56. err.code = 'POOL_NONEONLINE';
  57. } else {
  58. err = new Error('Pool does not exist.');
  59. err.code = 'POOL_NOEXIST';
  60. }
  61. process.nextTick(function () {
  62. query.on('error', function () {});
  63. query.end(err);
  64. });
  65. return query;
  66. }
  67. if (!(typeof sql === 'object' && 'typeCast' in sql)) {
  68. query.typeCast = clusterNode.pool.config.connectionConfig.typeCast;
  69. }
  70. if (clusterNode.pool.config.connectionConfig.trace) {
  71. // Long stack trace support
  72. query._callSite = new Error();
  73. }
  74. cluster._getConnection(clusterNode, function (err, conn) {
  75. var retry = err && cluster._canRetry
  76. && cluster._findNodeIds(namespace._pattern).length !== 0;
  77. if (retry) {
  78. namespace.query(query);
  79. return;
  80. }
  81. if (err) {
  82. query.on('error', function () {});
  83. query.end(err);
  84. return;
  85. }
  86. // Release connection based off event
  87. query.once('end', function() {
  88. conn.release();
  89. });
  90. conn.query(query);
  91. });
  92. return query;
  93. };
  94. PoolNamespace.prototype._getClusterNode = function _getClusterNode() {
  95. var foundNodeIds = this._cluster._findNodeIds(this._pattern);
  96. var nodeId;
  97. switch (foundNodeIds.length) {
  98. case 0:
  99. nodeId = null;
  100. break;
  101. case 1:
  102. nodeId = foundNodeIds[0];
  103. break;
  104. default:
  105. nodeId = this._selector(foundNodeIds);
  106. break;
  107. }
  108. return nodeId !== null
  109. ? this._cluster._getNode(nodeId)
  110. : null;
  111. };