Skip to content

Commit

Permalink
perf(NODE-5986): parallelize SRV/TXT resolution (#4012)
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Mar 4, 2024
1 parent f2b3484 commit eab8f23
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ export async function resolveSRVRecord(options: MongoOptions): Promise<HostAddre
throw new MongoAPIError('URI must include hostname, domain name, and tld');
}

// Resolve the SRV record and use the result as the list of hosts to connect to.
// Asynchronously start TXT resolution so that we do not have to wait until
// the SRV record is resolved before starting a second DNS query.
const lookupAddress = options.srvHost;
const txtResolutionPromise = dns.promises.resolveTxt(lookupAddress);
txtResolutionPromise.catch(() => {
/* rejections will be handled later */
});

// Resolve the SRV record and use the result as the list of hosts to connect to.
const addresses = await dns.promises.resolveSrv(
`_${options.srvServiceName}._tcp.${lookupAddress}`
);
Expand All @@ -88,10 +95,10 @@ export async function resolveSRVRecord(options: MongoOptions): Promise<HostAddre

validateLoadBalancedOptions(hostAddresses, options, true);

// Resolve TXT record and add options from there if they exist.
// Use the result of resolving the TXT record and add options from there if they exist.
let record;
try {
record = await dns.promises.resolveTxt(lookupAddress);
record = await txtResolutionPromise;
} catch (error) {
if (error.code !== 'ENODATA' && error.code !== 'ENOTFOUND') {
throw error;
Expand Down

0 comments on commit eab8f23

Please sign in to comment.