srv_polling.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SrvPoller = exports.SrvPollingEvent = void 0;
  4. const dns = require("dns");
  5. const timers_1 = require("timers");
  6. const error_1 = require("../error");
  7. const mongo_types_1 = require("../mongo_types");
  8. const utils_1 = require("../utils");
  9. /**
  10. * @internal
  11. * @category Event
  12. */
  13. class SrvPollingEvent {
  14. constructor(srvRecords) {
  15. this.srvRecords = srvRecords;
  16. }
  17. hostnames() {
  18. return new Set(this.srvRecords.map(r => utils_1.HostAddress.fromSrvRecord(r).toString()));
  19. }
  20. }
  21. exports.SrvPollingEvent = SrvPollingEvent;
  22. /** @internal */
  23. class SrvPoller extends mongo_types_1.TypedEventEmitter {
  24. constructor(options) {
  25. super();
  26. this.on('error', utils_1.noop);
  27. if (!options || !options.srvHost) {
  28. throw new error_1.MongoRuntimeError('Options for SrvPoller must exist and include srvHost');
  29. }
  30. this.srvHost = options.srvHost;
  31. this.srvMaxHosts = options.srvMaxHosts ?? 0;
  32. this.srvServiceName = options.srvServiceName ?? 'mongodb';
  33. this.rescanSrvIntervalMS = 60000;
  34. this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 10000;
  35. this.haMode = false;
  36. this.generation = 0;
  37. this._timeout = undefined;
  38. }
  39. get srvAddress() {
  40. return `_${this.srvServiceName}._tcp.${this.srvHost}`;
  41. }
  42. get intervalMS() {
  43. return this.haMode ? this.heartbeatFrequencyMS : this.rescanSrvIntervalMS;
  44. }
  45. start() {
  46. if (!this._timeout) {
  47. this.schedule();
  48. }
  49. }
  50. stop() {
  51. if (this._timeout) {
  52. (0, timers_1.clearTimeout)(this._timeout);
  53. this.generation += 1;
  54. this._timeout = undefined;
  55. }
  56. }
  57. // TODO(NODE-4994): implement new logging logic for SrvPoller failures
  58. schedule() {
  59. if (this._timeout) {
  60. (0, timers_1.clearTimeout)(this._timeout);
  61. }
  62. this._timeout = (0, timers_1.setTimeout)(() => {
  63. this._poll().then(undefined, utils_1.squashError);
  64. }, this.intervalMS);
  65. }
  66. success(srvRecords) {
  67. this.haMode = false;
  68. this.schedule();
  69. this.emit(SrvPoller.SRV_RECORD_DISCOVERY, new SrvPollingEvent(srvRecords));
  70. }
  71. failure() {
  72. this.haMode = true;
  73. this.schedule();
  74. }
  75. async _poll() {
  76. const generation = this.generation;
  77. let srvRecords;
  78. try {
  79. srvRecords = await dns.promises.resolveSrv(this.srvAddress);
  80. }
  81. catch {
  82. this.failure();
  83. return;
  84. }
  85. if (generation !== this.generation) {
  86. return;
  87. }
  88. const finalAddresses = [];
  89. for (const record of srvRecords) {
  90. try {
  91. (0, utils_1.checkParentDomainMatch)(record.name, this.srvHost);
  92. finalAddresses.push(record);
  93. }
  94. catch (error) {
  95. (0, utils_1.squashError)(error);
  96. }
  97. }
  98. if (!finalAddresses.length) {
  99. this.failure();
  100. return;
  101. }
  102. this.success(finalAddresses);
  103. }
  104. }
  105. exports.SrvPoller = SrvPoller;
  106. /** @event */
  107. SrvPoller.SRV_RECORD_DISCOVERY = 'srvRecordDiscovery';
  108. //# sourceMappingURL=srv_polling.js.map