srv_polling.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if (!options || !options.srvHost) {
  27. throw new error_1.MongoRuntimeError('Options for SrvPoller must exist and include srvHost');
  28. }
  29. this.srvHost = options.srvHost;
  30. this.srvMaxHosts = options.srvMaxHosts ?? 0;
  31. this.srvServiceName = options.srvServiceName ?? 'mongodb';
  32. this.rescanSrvIntervalMS = 60000;
  33. this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 10000;
  34. this.haMode = false;
  35. this.generation = 0;
  36. this._timeout = undefined;
  37. }
  38. get srvAddress() {
  39. return `_${this.srvServiceName}._tcp.${this.srvHost}`;
  40. }
  41. get intervalMS() {
  42. return this.haMode ? this.heartbeatFrequencyMS : this.rescanSrvIntervalMS;
  43. }
  44. start() {
  45. if (!this._timeout) {
  46. this.schedule();
  47. }
  48. }
  49. stop() {
  50. if (this._timeout) {
  51. (0, timers_1.clearTimeout)(this._timeout);
  52. this.generation += 1;
  53. this._timeout = undefined;
  54. }
  55. }
  56. // TODO(NODE-4994): implement new logging logic for SrvPoller failures
  57. schedule() {
  58. if (this._timeout) {
  59. (0, timers_1.clearTimeout)(this._timeout);
  60. }
  61. this._timeout = (0, timers_1.setTimeout)(() => {
  62. this._poll().catch(() => null);
  63. }, this.intervalMS);
  64. }
  65. success(srvRecords) {
  66. this.haMode = false;
  67. this.schedule();
  68. this.emit(SrvPoller.SRV_RECORD_DISCOVERY, new SrvPollingEvent(srvRecords));
  69. }
  70. failure() {
  71. this.haMode = true;
  72. this.schedule();
  73. }
  74. async _poll() {
  75. const generation = this.generation;
  76. let srvRecords;
  77. try {
  78. srvRecords = await dns.promises.resolveSrv(this.srvAddress);
  79. }
  80. catch (dnsError) {
  81. this.failure();
  82. return;
  83. }
  84. if (generation !== this.generation) {
  85. return;
  86. }
  87. const finalAddresses = [];
  88. for (const record of srvRecords) {
  89. if ((0, utils_1.matchesParentDomain)(record.name, this.srvHost)) {
  90. finalAddresses.push(record);
  91. }
  92. }
  93. if (!finalAddresses.length) {
  94. this.failure();
  95. return;
  96. }
  97. this.success(finalAddresses);
  98. }
  99. }
  100. /** @event */
  101. SrvPoller.SRV_RECORD_DISCOVERY = 'srvRecordDiscovery';
  102. exports.SrvPoller = SrvPoller;
  103. //# sourceMappingURL=srv_polling.js.map