From db221204e5b140b2e26727864b268c4c02006081 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Thu, 6 Sep 2018 14:52:04 +0530 Subject: [PATCH] ELECTRON-730 - Add logic to fetch active network requests from the client to validate (#494) --- js/mainApiMgr.js | 7 +++++-- js/memoryMonitor.js | 15 +++++++++++---- js/preload/preloadMain.js | 20 +++++++++++++++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/js/mainApiMgr.js b/js/mainApiMgr.js index 2feec915..27e19077 100644 --- a/js/mainApiMgr.js +++ b/js/mainApiMgr.js @@ -152,8 +152,11 @@ electron.ipcMain.on(apiName, (event, arg) => { break; } case apiCmds.optimizeMemoryConsumption: - if (typeof arg.memory === 'object' && typeof arg.cpuUsage === 'object' && typeof arg.memory.workingSetSize === 'number') { - setPreloadMemoryInfo(arg.memory, arg.cpuUsage); + if (typeof arg.memory === 'object' + && typeof arg.cpuUsage === 'object' + && 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 6c654823..e4e48b52 100644 --- a/js/memoryMonitor.js +++ b/js/memoryMonitor.js @@ -49,12 +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 + && activeNetworkRequest ) { getConfigField('memoryRefresh') .then((enabled) => { @@ -67,6 +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 ${preloadMemory.activeRequests} is network online? ${getIsOnline()}`); mainWindow.reload(); @@ -85,6 +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 ${preloadMemory.activeRequests} is network online? ${getIsOnline()}`); } } @@ -100,12 +104,13 @@ function setIsInMeeting(meetingStatus) { /** * Sets preload memory info and calls optimize memory func * - * @param memoryInfo - * @param cpuUsage + * @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(); } @@ -131,6 +136,7 @@ eventEmitter.on('appMinimized', () => { */ eventEmitter.on('appRestored', () => { log.send(logLevels.INFO, 'Application was restored from minimized state'); + setIsAutoReload(false); if (appMinimizedTimer) { clearTimeout(appMinimizedTimer); } @@ -156,6 +162,7 @@ eventEmitter.on('sys-locked', () => { */ eventEmitter.on('sys-unlocked', () => { log.send(logLevels.INFO, 'System screen was unlocked'); + setIsAutoReload(false); if (powerMonitorTimer) { clearTimeout(powerMonitorTimer); } diff --git a/js/preload/preloadMain.js b/js/preload/preloadMain.js index 4610035d..541322df 100644 --- a/js/preload/preloadMain.js +++ b/js/preload/preloadMain.js @@ -365,7 +365,19 @@ function createAPI() { if (typeof locale === 'string') { 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: memory, - cpuUsage: cpuUsage + memory, + cpuUsage, + activeRequests, }); } });