UNPKG

@globalworldwide/grpc-resolvers

Version:
65 lines 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FixedResolver = void 0; const well_known_channel_options_js_1 = require("./well-known-channel-options.js"); const logging_js_1 = require("./internal/logging.js"); const logger = (0, logging_js_1.makeLogger)('fixed-resolver'); class FixedResolver { static getDefaultAuthority(target) { return target.path; } #minTimeBetweenResolutionsMS; authorityMap; #target; #listener; // #channelOptions: grpc.ChannelOptions; #lastResolveTimeMS = 0; #timeoutId = undefined; constructor(target, listener, channelOptions) { this.#target = target; this.#listener = listener; // this.#channelOptions = channelOptions; this.authorityMap = channelOptions[well_known_channel_options_js_1.WellKnownChannelOptions.authorityMap] ?? {}; this.#minTimeBetweenResolutionsMS = channelOptions[well_known_channel_options_js_1.WellKnownChannelOptions.minTimeBetweenResolutionsMS] ?? 1000; } updateResolution() { // If the service is not actually running, then grpc-js starts calling updateResolution // as fast as we answer. In order to stop that from turning into a busy loop, we never // resolve more than once every 1000ms (configurable via channel optiona) if (!this.#timeoutId) { const nowMS = Date.now(); const delayMS = Math.max(0, this.#minTimeBetweenResolutionsMS - (nowMS - this.#lastResolveTimeMS)); this.#timeoutId = setTimeout(this.onTimeout.bind(this), delayMS); } } destroy() { clearTimeout(this.#timeoutId); this.#timeoutId = undefined; this.#lastResolveTimeMS = 0; } getHostPort(authority) { const hostPort = this.authorityMap[authority]; let pathSplit = hostPort?.split(':') ?? []; let host = pathSplit[0]; if (!host) { host = '127.0.0.1'; } let port = Number(pathSplit[1]); if (!Number.isInteger(port) || port === 0) { port = 443; } return [host, port]; } onTimeout() { this.#lastResolveTimeMS = Date.now(); this.#timeoutId = undefined; const authority = FixedResolver.getDefaultAuthority(this.#target); const [host, port] = this.getHostPort(authority); const endpoint = { addresses: [{ host, port }] }; logger.debug?.(`mapped ${this.#target.path} to ${host}:${port}`); this.#listener.onSuccessfulResolution([endpoint], null, null, null, {}); } } exports.FixedResolver = FixedResolver; //# sourceMappingURL=fixed-resolver.js.map