Add timeout to DNS lookup
This commit is contained in:
parent
44a8049e63
commit
05c09ef769
1 changed files with 14 additions and 7 deletions
|
@ -9,11 +9,14 @@ import type {
|
||||||
} from 'dns';
|
} from 'dns';
|
||||||
import { ipcRenderer, net } from 'electron';
|
import { ipcRenderer, net } from 'electron';
|
||||||
import type { ResolvedHost, ResolvedEndpoint } from 'electron';
|
import type { ResolvedHost, ResolvedEndpoint } from 'electron';
|
||||||
|
import pTimeout from 'p-timeout';
|
||||||
|
|
||||||
import { strictAssert } from './assert';
|
import { strictAssert } from './assert';
|
||||||
import { drop } from './drop';
|
import { drop } from './drop';
|
||||||
import type { DNSFallbackType } from '../types/DNSFallback';
|
import type { DNSFallbackType } from '../types/DNSFallback';
|
||||||
|
import { SECOND } from './durations';
|
||||||
|
|
||||||
|
const LOOKUP_TIMEOUT_MS = 1 * SECOND;
|
||||||
const fallbackAddrs = new Map<string, ReadonlyArray<ResolvedEndpoint>>();
|
const fallbackAddrs = new Map<string, ReadonlyArray<ResolvedEndpoint>>();
|
||||||
|
|
||||||
export function setFallback(dnsFallback: DNSFallbackType): void {
|
export function setFallback(dnsFallback: DNSFallbackType): void {
|
||||||
|
@ -49,15 +52,19 @@ function lookupAll(
|
||||||
try {
|
try {
|
||||||
if (net) {
|
if (net) {
|
||||||
// Main process
|
// Main process
|
||||||
result = await net.resolveHost(hostname, {
|
result = await pTimeout(
|
||||||
queryType,
|
net.resolveHost(hostname, {
|
||||||
});
|
queryType,
|
||||||
|
}),
|
||||||
|
LOOKUP_TIMEOUT_MS,
|
||||||
|
'lookupAll: Electron lookup timed out'
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// Renderer
|
// Renderer
|
||||||
result = await ipcRenderer.invoke(
|
result = await pTimeout(
|
||||||
'net.resolveHost',
|
ipcRenderer.invoke('net.resolveHost', hostname, queryType),
|
||||||
hostname,
|
LOOKUP_TIMEOUT_MS,
|
||||||
queryType
|
'lookupAll: Electron lookup timed out'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue