Skip to content

Commit 83d4499

Browse files
update
1 parent b3dfb73 commit 83d4499

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/ConfigurationClientManager.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./A
88
import { isBrowser, isWebWorker } from "./requestTracing/utils.js";
99
import * as RequestTracing from "./requestTracing/constants.js";
1010
import { shuffleList } from "./common/utils.js";
11+
import { Resolver } from "dns/promises";
1112

1213
const TCP_ORIGIN_KEY_NAME = "_origin._tcp";
1314
const ALT_KEY_NAME = "_alt";
@@ -17,8 +18,8 @@ const ID_KEY_NAME = "Id";
1718
const SECRET_KEY_NAME = "Secret";
1819
const TRUSTED_DOMAIN_LABELS = [".azconfig.", ".appconfig."];
1920
const FALLBACK_CLIENT_EXPIRE_INTERVAL = 60 * 60 * 1000; // 1 hour in milliseconds
20-
const MINIMAL_CLIENT_REFRESH_INTERVAL = 30 * 1000; // 30 seconds in milliseconds
21-
const DNS_RESOLVER_TIMEOUT = 1_000; // 1 second in milliseconds, in most cases, dns resolution should be within 200 milliseconds
21+
const MINIMAL_CLIENT_REFRESH_INTERVAL = 30_000; // 30 seconds in milliseconds
22+
const DNS_RESOLVER_TIMEOUT = 3_000; // 3 seconds in milliseconds, in most cases, dns resolution should be within 200 milliseconds
2223
const DNS_RESOLVER_TRIES = 2;
2324
const MAX_ALTNATIVE_SRV_COUNT = 10;
2425

@@ -173,31 +174,24 @@ export class ConfigurationClientManager {
173174
}
174175

175176
/**
176-
* Query SRV records and return target hosts.
177+
* Queries SRV records for the given host and returns the target hosts.
177178
*/
178179
async #querySrvTargetHost(host: string): Promise<string[]> {
179180
const results: string[] = [];
180181

181182
try {
182-
const resolver = new this.#dns.Resolver({timeout: DNS_RESOLVER_TIMEOUT, tries: DNS_RESOLVER_TRIES});
183-
// Look up SRV records for the origin host
184-
const originRecords = await resolver.resolveSrv(`${TCP_ORIGIN_KEY_NAME}.${host}`);
185-
if (originRecords.length === 0) {
186-
return results;
187-
}
188-
189-
// Add the first origin record to results
183+
// https://nodejs.org/api/dns.html#dnspromisesresolvesrvhostname
184+
const resolver = new Resolver({timeout: DNS_RESOLVER_TIMEOUT, tries: DNS_RESOLVER_TRIES});
185+
// On success, resolveSrv() returns an array of SrvRecord
186+
// On failure, resolveSrv() throws an error with code 'ENOTFOUND'.
187+
const originRecords = await resolver.resolveSrv(`${TCP_ORIGIN_KEY_NAME}.${host}`); // look up SRV records for the origin host
190188
const originHost = originRecords[0].name;
191-
results.push(originHost);
189+
results.push(originHost); // add the first origin record to results
192190

193-
// Look up SRV records for alternate hosts
194191
let index = 0;
195192
while (index < MAX_ALTNATIVE_SRV_COUNT) {
196-
const currentAlt = `${ALT_KEY_NAME}${index}`;
193+
const currentAlt = `${ALT_KEY_NAME}${index}`; // look up SRV records for alternate hosts
197194
const altRecords = await resolver.resolveSrv(`${currentAlt}.${TCP_KEY_NAME}.${originHost}`);
198-
if (altRecords.length === 0) {
199-
break; // No more alternate records, exit loop
200-
}
201195

202196
altRecords.forEach(record => {
203197
const altHost = record.name;
@@ -209,7 +203,8 @@ export class ConfigurationClientManager {
209203
}
210204
} catch (err) {
211205
if (err.code === "ENOTFOUND") {
212-
return results; // No more SRV records found, return results
206+
// No more SRV records found, return results.
207+
return results;
213208
} else {
214209
throw new Error(`Failed to lookup SRV records: ${err.message}`);
215210
}

0 commit comments

Comments
 (0)