Close proxy server on app quit (#413)

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala authored and GitHub committed 2020-06-10 17:00:17 +03:00
1 parent e8fcff0037
commit 78eeef2805
4 files changed
+31 -17

No files matched your search

+3 -2
View File
@@ -26,6 +26,7 @@ const promiseIpc = new PromiseIpc({ timeout: 2000 })
let windowManager: WindowManager = null;
let clusterManager: ClusterManager = null;
let proxyServer: proxy.LensProxy = null;
const vmURL = (isDevelopment) ? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}` : formatUrl({
pathname: path.join(__dirname, "index.html"),
protocol: "file",
@@ -57,10 +58,9 @@ async function main() {
// create cluster manager
clusterManager = new ClusterManager(clusterStore.getAllClusterObjects(), port)
// run proxy
try {
proxy.listen(port, clusterManager)
proxyServer = proxy.listen(port, clusterManager)
} catch (error) {
logger.error(`Could not start proxy (127.0.0:${port}): ${error.message}`)
await dialog.showErrorBox("Lens Error", `Could not start proxy (127.0.0:${port}): ${error.message || "unknown error"}`)
@@ -123,5 +123,6 @@ app.on("activate", () => {
app.on("will-quit", async (event) => {
event.preventDefault(); // To allow mixpanel sending to be executed
if (clusterManager) clusterManager.stop()
if (proxyServer) proxyServer.close()
app.exit(0);
})
+12
View File
@@ -17,6 +17,8 @@ export class LensProxy {
protected clusterManager: ClusterManager
protected retryCounters: Map<string, number> = new Map()
protected router: Router
protected proxyServer: http.Server
protected closed = false
constructor(port: number, clusterManager: ClusterManager) {
this.port = port
@@ -27,6 +29,13 @@ export class LensProxy {
public run() {
const proxyServer = this.buildProxyServer();
proxyServer.listen(this.port, "127.0.0.1")
this.proxyServer = proxyServer
}
public close() {
logger.info("Closing proxy server")
this.proxyServer.close()
this.closed = true
}
protected buildProxyServer() {
@@ -70,6 +79,9 @@ export class LensProxy {
}
})
proxy.on("error", (error, req, res, target) => {
if(this.closed) {
return
}
if (target) {
logger.debug("Failed proxy to target: " + JSON.stringify(target))
if (req.method === "GET" && (!res.statusCode || res.statusCode >= 500)) {