util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getConnectionName = exports.weightSrvRecords = exports.groupSrvRecords = exports.getUniqueHostnamesFromOptions = exports.normalizeNodeOptions = exports.nodeKeyToRedisOptions = exports.getNodeKey = void 0;
  4. const utils_1 = require("../utils");
  5. const net_1 = require("net");
  6. function getNodeKey(node) {
  7. node.port = node.port || 6379;
  8. node.host = node.host || "127.0.0.1";
  9. return node.host + ":" + node.port;
  10. }
  11. exports.getNodeKey = getNodeKey;
  12. function nodeKeyToRedisOptions(nodeKey) {
  13. const portIndex = nodeKey.lastIndexOf(":");
  14. if (portIndex === -1) {
  15. throw new Error(`Invalid node key ${nodeKey}`);
  16. }
  17. return {
  18. host: nodeKey.slice(0, portIndex),
  19. port: Number(nodeKey.slice(portIndex + 1)),
  20. };
  21. }
  22. exports.nodeKeyToRedisOptions = nodeKeyToRedisOptions;
  23. function normalizeNodeOptions(nodes) {
  24. return nodes.map((node) => {
  25. const options = {};
  26. if (typeof node === "object") {
  27. Object.assign(options, node);
  28. }
  29. else if (typeof node === "string") {
  30. Object.assign(options, (0, utils_1.parseURL)(node));
  31. }
  32. else if (typeof node === "number") {
  33. options.port = node;
  34. }
  35. else {
  36. throw new Error("Invalid argument " + node);
  37. }
  38. if (typeof options.port === "string") {
  39. options.port = parseInt(options.port, 10);
  40. }
  41. // Cluster mode only support db 0
  42. delete options.db;
  43. if (!options.port) {
  44. options.port = 6379;
  45. }
  46. if (!options.host) {
  47. options.host = "127.0.0.1";
  48. }
  49. return (0, utils_1.resolveTLSProfile)(options);
  50. });
  51. }
  52. exports.normalizeNodeOptions = normalizeNodeOptions;
  53. function getUniqueHostnamesFromOptions(nodes) {
  54. const uniqueHostsMap = {};
  55. nodes.forEach((node) => {
  56. uniqueHostsMap[node.host] = true;
  57. });
  58. return Object.keys(uniqueHostsMap).filter((host) => !(0, net_1.isIP)(host));
  59. }
  60. exports.getUniqueHostnamesFromOptions = getUniqueHostnamesFromOptions;
  61. function groupSrvRecords(records) {
  62. const recordsByPriority = {};
  63. for (const record of records) {
  64. if (!recordsByPriority.hasOwnProperty(record.priority)) {
  65. recordsByPriority[record.priority] = {
  66. totalWeight: record.weight,
  67. records: [record],
  68. };
  69. }
  70. else {
  71. recordsByPriority[record.priority].totalWeight += record.weight;
  72. recordsByPriority[record.priority].records.push(record);
  73. }
  74. }
  75. return recordsByPriority;
  76. }
  77. exports.groupSrvRecords = groupSrvRecords;
  78. function weightSrvRecords(recordsGroup) {
  79. if (recordsGroup.records.length === 1) {
  80. recordsGroup.totalWeight = 0;
  81. return recordsGroup.records.shift();
  82. }
  83. // + `recordsGroup.records.length` to support `weight` 0
  84. const random = Math.floor(Math.random() * (recordsGroup.totalWeight + recordsGroup.records.length));
  85. let total = 0;
  86. for (const [i, record] of recordsGroup.records.entries()) {
  87. total += 1 + record.weight;
  88. if (total > random) {
  89. recordsGroup.totalWeight -= record.weight;
  90. recordsGroup.records.splice(i, 1);
  91. return record;
  92. }
  93. }
  94. }
  95. exports.weightSrvRecords = weightSrvRecords;
  96. function getConnectionName(component, nodeConnectionName) {
  97. const prefix = `ioredis-cluster(${component})`;
  98. return nodeConnectionName ? `${prefix}:${nodeConnectionName}` : prefix;
  99. }
  100. exports.getConnectionName = getConnectionName;