1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add whenCanResolveDomainName to replace setTimeout

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-11-22 17:04:43 +02:00
parent 86c50afed7
commit 5e7a49a707
No known key found for this signature in database
GPG Key ID: 54B44603D251B788

View File

@ -557,8 +557,8 @@ export class KubeApi<T extends KubeObject> {
this.watchingNetworkStatus = true;
}
private whenCanResolveDomainName(url = "https://k8slens.dev/", retryOptions: Parameters<typeof retry>[1]) {
return retry(() => dns.resolve(url), retryOptions);
private whenCanResolveDomainName(url = "k8slens.dev", retryOptions: Parameters<typeof retry>[1]) {
return retry(() => dns.lookup(url), retryOptions);
}
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
@ -588,22 +588,12 @@ export class KubeApi<T extends KubeObject> {
electron.powerMonitor.once("suspend", () => {
logger.info(`[KUBE-API] system suspended, abort watching of ${watchUrl}...`);
try {
if (opts.abortController) {
opts.abortController.abort?.();
} else {
abort?.();
}
} catch (error) {
logger.error(`[KUBE-API] error aborting watch (${watchId})`, error);
}
electron.powerMonitor.once("resume", () => {
clearTimeout(timedRetry);
timedRetry = setTimeout(() => {
const url = "https://k8slens.dev/"; // url to check if domain names are resolvable.
const url = "k8slens.dev"; // url to check if domain names are resolvable.
this.whenCanResolveDomainName(url, { retries: 50, maxTimeout: 3000 }).then(() => {
logger.info(`[KUBE-API] domain name can be resolved, checking networkOnline:${this.networkOnline.toString?.()}`);
this.whenCanResolveDomainName(url, { retries: 10 }).then(() => {
if (this.networkOnline) {
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true });
@ -615,11 +605,8 @@ export class KubeApi<T extends KubeObject> {
});
}
}).catch((error) => {
logger.error(`[KUBE-API] error resolving domain name ${url}`, error);
logger.warn(`[KUBE-API] error resolving domain name ${url}`, error);
});
// 3000 is a naive value, we assume that after 3 seconds the system is ready
// to start watching again. (Network interface/DNS is ready etc.)
}, 3000);
});
});