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; this.watchingNetworkStatus = true;
} }
private whenCanResolveDomainName(url = "https://k8slens.dev/", retryOptions: Parameters<typeof retry>[1]) { private whenCanResolveDomainName(url = "k8slens.dev", retryOptions: Parameters<typeof retry>[1]) {
return retry(() => dns.resolve(url), retryOptions); return retry(() => dns.lookup(url), retryOptions);
} }
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void { watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
@ -588,38 +588,25 @@ export class KubeApi<T extends KubeObject> {
electron.powerMonitor.once("suspend", () => { electron.powerMonitor.once("suspend", () => {
logger.info(`[KUBE-API] system suspended, abort watching of ${watchUrl}...`); 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", () => { electron.powerMonitor.once("resume", () => {
clearTimeout(timedRetry); const url = "k8slens.dev"; // url to check if domain names are resolvable.
timedRetry = setTimeout(() => {
const url = "https://k8slens.dev/"; // url to check if domain names are resolvable.
this.whenCanResolveDomainName(url, { retries: 10 }).then(() => { this.whenCanResolveDomainName(url, { retries: 50, maxTimeout: 3000 }).then(() => {
if (this.networkOnline) { logger.info(`[KUBE-API] domain name can be resolved, checking networkOnline:${this.networkOnline.toString?.()}`);
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
if (this.networkOnline) {
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true });
} else {
logger.info(`[KUBE-API] system resumed but network appears to be offline, resume watching when online.`);
electron.ipcMain.once("network:online", () => {
logger.info(`[KUBE-API] network on line, resume watching of ${watchUrl}...`);
this.watch({ ...opts, namespace, callback, watchId, retry: true }); this.watch({ ...opts, namespace, callback, watchId, retry: true });
} else { });
logger.info(`[KUBE-API] system resumed but network appears to be offline, resume watching when online.`); }
electron.ipcMain.once("network:online", () => { }).catch((error) => {
logger.info(`[KUBE-API] network on line, resume watching of ${watchUrl}...`); logger.warn(`[KUBE-API] error resolving domain name ${url}`, error);
this.watch({ ...opts, namespace, callback, watchId, retry: true }); });
});
}
}).catch((error) => {
logger.error(`[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);
}); });
}); });