From c895291324c1a83c598ce23f680b9267c9138355 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Wed, 3 Oct 2018 21:30:52 +0530 Subject: [PATCH] Revert "ELECTRON-758 - Change network request implementation to use electron's API (#509)" (#514) This reverts commit 0c8b322 --- js/main.js | 4 +--- js/mainApiMgr.js | 5 ++-- js/memoryMonitor.js | 50 ++++++--------------------------------- js/preload/preloadMain.js | 14 +++++++++++ 4 files changed, 25 insertions(+), 48 deletions(-) diff --git a/js/main.js b/js/main.js index 40653a04..32aa6e7c 100644 --- a/js/main.js +++ b/js/main.js @@ -26,7 +26,6 @@ const log = require('./log.js'); const logLevels = require('./enums/logLevels.js'); const autoLaunch = require('./autoLaunch'); const { handleCacheFailureCheckOnStartup, handleCacheFailureCheckOnExit} = require('./cacheHandler'); -const { monitorNetworkRequest } = require('./memoryMonitor'); require('electron-dl')(); @@ -200,8 +199,7 @@ app.on('ready', () => { electron.powerMonitor.on('unlock-screen', () => { eventEmitter.emit('sys-unlocked'); }); - // Keeps track of active network request - monitorNetworkRequest(); + checkFirstTimeLaunch() .then(readConfigThenOpenMainWindow) .catch(readConfigThenOpenMainWindow); diff --git a/js/mainApiMgr.js b/js/mainApiMgr.js index a67459e1..27e19077 100644 --- a/js/mainApiMgr.js +++ b/js/mainApiMgr.js @@ -154,8 +154,9 @@ electron.ipcMain.on(apiName, (event, arg) => { case apiCmds.optimizeMemoryConsumption: if (typeof arg.memory === 'object' && typeof arg.cpuUsage === 'object' - && typeof arg.memory.workingSetSize === 'number') { - setPreloadMemoryInfo(arg.memory, arg.cpuUsage); + && typeof arg.memory.workingSetSize === 'number' + && typeof arg.activeRequests === 'number') { + setPreloadMemoryInfo(arg.memory, arg.cpuUsage, arg.activeRequests); } break; case apiCmds.optimizeMemoryRegister: diff --git a/js/memoryMonitor.js b/js/memoryMonitor.js index 9abcb4fe..e4e48b52 100644 --- a/js/memoryMonitor.js +++ b/js/memoryMonitor.js @@ -1,6 +1,5 @@ 'use strict'; -const { session } = require('electron'); const eventEmitter = require('./eventEmitter'); const log = require('./log.js'); @@ -19,7 +18,6 @@ let appMinimizedTimer; let powerMonitorTimer; let preloadMemory; let preloadWindow; -let networkRequestCount = 0; // once a minute setInterval(gatherMemory, 1000 * 60); @@ -51,13 +49,14 @@ function optimizeMemory() { const memoryConsumed = (preloadMemory.memoryInfo && preloadMemory.memoryInfo.workingSetSize / 1024) || 0; const cpuUsagePercentage = preloadMemory.cpuUsage.percentCPUUsage; + const activeNetworkRequest = preloadMemory.activeRequests === 0; if (memoryConsumed > maxMemory && cpuUsagePercentage <= cpuUsageThreshold && !isInMeeting && getIsOnline() && canReload - && networkRequestCount <= 0 + && activeNetworkRequest ) { getConfigField('memoryRefresh') .then((enabled) => { @@ -70,7 +69,7 @@ function optimizeMemory() { memory consumption was ${memoryConsumed} CPU usage percentage was ${preloadMemory.cpuUsage.percentCPUUsage} user was in a meeting? ${isInMeeting} - pending network request on the client was ${networkRequestCount} + pending network request on the client was ${preloadMemory.activeRequests} is network online? ${getIsOnline()}`); mainWindow.reload(); @@ -89,7 +88,7 @@ function optimizeMemory() { memory consumption was ${memoryConsumed} CPU usage percentage was ${preloadMemory.cpuUsage.percentCPUUsage} user was in a meeting? ${isInMeeting} - pending network request on the client was ${networkRequestCount} + pending network request on the client was ${preloadMemory.activeRequests} is network online? ${getIsOnline()}`); } } @@ -107,10 +106,11 @@ function setIsInMeeting(meetingStatus) { * * @param memoryInfo - memory consumption of the preload main script * @param cpuUsage - CPU usage of the preload main script + * @param activeRequests - pending active network requests on the client */ -function setPreloadMemoryInfo(memoryInfo, cpuUsage) { +function setPreloadMemoryInfo(memoryInfo, cpuUsage, activeRequests) { log.send(logLevels.INFO, 'Memory info received from preload process now running optimize memory logic'); - preloadMemory = { memoryInfo, cpuUsage }; + preloadMemory = { memoryInfo, cpuUsage, activeRequests }; optimizeMemory(); } @@ -189,44 +189,8 @@ function requestMemoryInfo() { } } -/** - * Monitors and Keeps track of active network requests - */ -function monitorNetworkRequest() { - let ids = []; - - // network request started - session.defaultSession.webRequest.onSendHeaders((details) => { - networkRequestCount++; - ids.push(details.id); - }); - - // decrease network request count on complete - session.defaultSession.webRequest.onCompleted((details) => { - if (ids.includes(details.id)) { - networkRequestCount--; - ids = ids.filter((value) => value !== details.id); - } - }); - - // decrease network request count on error - session.defaultSession.webRequest.onErrorOccurred((details) => { - if (ids.includes(details.id)) { - networkRequestCount--; - ids = ids.filter((value) => value !== details.id); - } - }); - - // Resets network request on redirect - session.defaultSession.webRequest.onBeforeRedirect(() => { - networkRequestCount = 0; - ids = []; - }); -} - module.exports = { setIsInMeeting, setPreloadMemoryInfo, setPreloadWindow, - monitorNetworkRequest, }; \ No newline at end of file diff --git a/js/preload/preloadMain.js b/js/preload/preloadMain.js index d5db298c..541322df 100644 --- a/js/preload/preloadMain.js +++ b/js/preload/preloadMain.js @@ -366,6 +366,18 @@ function createAPI() { throttledSetLocale(locale); } }, + + /** + * Allows JS to register activeRequests that can be used by electron to + * get the active network request from the client + * + * @param activeRequests + */ + registerActiveRequests: function (activeRequests) { + if (typeof activeRequests === 'function') { + local.activeRequests = activeRequests; + } + }, }; // add support for both ssf and SYM_API name-space. @@ -518,10 +530,12 @@ function createAPI() { if (window.name === 'main') { const memory = process.getProcessMemoryInfo(); const cpuUsage = process.getCPUUsage(); + const activeRequests = local.activeRequests(); local.ipcRenderer.send(apiName, { cmd: apiCmds.optimizeMemoryConsumption, memory, cpuUsage, + activeRequests, }); } });