UNPKG

agora-rest-client

Version:
65 lines (64 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DomainPool = void 0; const types_1 = require("../types"); const log_1 = require("../log"); const logger = (0, log_1.getLogger)('pool'); class DomainPool { constructor(area) { this.domainSuffixList = []; this.regionPrefixList = []; this.currentRegionPrefixList = []; this.domainUpdateTs = -1; this.domainArea = area; switch (this.domainArea) { case types_1.DomainArea.US: this.domainSuffixList = ['agora.io', 'sd-rtn.com']; this.regionPrefixList = ['api-us-west-1', 'api-us-east-1']; break; case types_1.DomainArea.EU: this.domainSuffixList = ['agora.io', 'sd-rtn.com']; this.regionPrefixList = ['api-eu-west-1', 'api-eu-central-1']; break; case types_1.DomainArea.AP: this.domainSuffixList = ['agora.io', 'sd-rtn.com']; this.regionPrefixList = ['api-ap-southeast-1', 'api-ap-northeast-1']; break; case types_1.DomainArea.CN: this.domainSuffixList = ['sd-rtn.com', 'agora.io']; this.regionPrefixList = ['api-cn-north-1', 'api-cn-east-2']; break; default: break; } this.currentRegionPrefixList = [...this.regionPrefixList]; this.currentDomain = this.domainSuffixList[0]; } get currentRegion() { return this.currentRegionPrefixList[0]; } get currentUrl() { return `https://${this.currentRegion}.${this.currentDomain}`; } get allDomains() { return [...this.domainSuffixList]; } nextRegion() { this.currentRegionPrefixList.shift(); if (this.currentRegionPrefixList.length === 0) { this.currentRegionPrefixList = [...this.regionPrefixList]; } } domainNeedUpdate() { // cache last domain result for 30 secs return Date.now() - this.domainUpdateTs > 30 * 1000; } selectDomain(domain) { if (this.domainSuffixList.includes(domain)) { this.currentDomain = domain; this.domainUpdateTs = Date.now(); logger.info(`domain updated to ${domain}`); } } } exports.DomainPool = DomainPool;