From 6285ccaaa6f9cf375b9e9f3765a6d725ebaf2da9 Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Mon, 22 Nov 2021 16:27:35 +0200 Subject: [PATCH] Add whenCanResolveDomainName Signed-off-by: Hung-Han (Henry) Chen --- src/common/k8s-api/kube-api.ts | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/common/k8s-api/kube-api.ts b/src/common/k8s-api/kube-api.ts index d06fa13b7b..bbf156d31a 100644 --- a/src/common/k8s-api/kube-api.ts +++ b/src/common/k8s-api/kube-api.ts @@ -40,6 +40,8 @@ import { Agent, AgentOptions } from "https"; import type { Patch } from "rfc6902"; import electron from "electron"; import { ipcMainOn } from "../ipc"; +import { promises as dns } from "dns"; +import retry from "async-retry"; export interface IKubeApiOptions { /** @@ -555,6 +557,10 @@ export class KubeApi { this.watchingNetworkStatus = true; } + private whenCanResolveDomainName(url = "https://k8slens.dev/", retryOptions: Parameters[1]) { + return retry(() => dns.resolve(url), retryOptions); + } + watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void { let errorReceived = false; let timedRetry: NodeJS.Timeout; @@ -595,16 +601,22 @@ export class KubeApi { electron.powerMonitor.once("resume", () => { clearTimeout(timedRetry); timedRetry = setTimeout(() => { - 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 }); - }); - } + const url = "https://k8slens.dev/"; // url to check if domain names are resolvable. + + 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 }); + } 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 }); + }); + } + }).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);