Revert "ELECTRON-758 - Change network request implementation to use electron's API (#509)" (#514)

This reverts commit 0c8b322
This commit is contained in:
Kiran Niranjan 2018-10-03 21:30:52 +05:30 committed by Vishwas Shashidhar
parent 4e25fddbe6
commit c895291324
4 changed files with 25 additions and 48 deletions

View File

@ -26,7 +26,6 @@ const log = require('./log.js');
const logLevels = require('./enums/logLevels.js'); const logLevels = require('./enums/logLevels.js');
const autoLaunch = require('./autoLaunch'); const autoLaunch = require('./autoLaunch');
const { handleCacheFailureCheckOnStartup, handleCacheFailureCheckOnExit} = require('./cacheHandler'); const { handleCacheFailureCheckOnStartup, handleCacheFailureCheckOnExit} = require('./cacheHandler');
const { monitorNetworkRequest } = require('./memoryMonitor');
require('electron-dl')(); require('electron-dl')();
@ -200,8 +199,7 @@ app.on('ready', () => {
electron.powerMonitor.on('unlock-screen', () => { electron.powerMonitor.on('unlock-screen', () => {
eventEmitter.emit('sys-unlocked'); eventEmitter.emit('sys-unlocked');
}); });
// Keeps track of active network request
monitorNetworkRequest();
checkFirstTimeLaunch() checkFirstTimeLaunch()
.then(readConfigThenOpenMainWindow) .then(readConfigThenOpenMainWindow)
.catch(readConfigThenOpenMainWindow); .catch(readConfigThenOpenMainWindow);

View File

@ -154,8 +154,9 @@ electron.ipcMain.on(apiName, (event, arg) => {
case apiCmds.optimizeMemoryConsumption: case apiCmds.optimizeMemoryConsumption:
if (typeof arg.memory === 'object' if (typeof arg.memory === 'object'
&& typeof arg.cpuUsage === 'object' && typeof arg.cpuUsage === 'object'
&& typeof arg.memory.workingSetSize === 'number') { && typeof arg.memory.workingSetSize === 'number'
setPreloadMemoryInfo(arg.memory, arg.cpuUsage); && typeof arg.activeRequests === 'number') {
setPreloadMemoryInfo(arg.memory, arg.cpuUsage, arg.activeRequests);
} }
break; break;
case apiCmds.optimizeMemoryRegister: case apiCmds.optimizeMemoryRegister:

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { session } = require('electron');
const eventEmitter = require('./eventEmitter'); const eventEmitter = require('./eventEmitter');
const log = require('./log.js'); const log = require('./log.js');
@ -19,7 +18,6 @@ let appMinimizedTimer;
let powerMonitorTimer; let powerMonitorTimer;
let preloadMemory; let preloadMemory;
let preloadWindow; let preloadWindow;
let networkRequestCount = 0;
// once a minute // once a minute
setInterval(gatherMemory, 1000 * 60); setInterval(gatherMemory, 1000 * 60);
@ -51,13 +49,14 @@ function optimizeMemory() {
const memoryConsumed = (preloadMemory.memoryInfo && preloadMemory.memoryInfo.workingSetSize / 1024) || 0; const memoryConsumed = (preloadMemory.memoryInfo && preloadMemory.memoryInfo.workingSetSize / 1024) || 0;
const cpuUsagePercentage = preloadMemory.cpuUsage.percentCPUUsage; const cpuUsagePercentage = preloadMemory.cpuUsage.percentCPUUsage;
const activeNetworkRequest = preloadMemory.activeRequests === 0;
if (memoryConsumed > maxMemory if (memoryConsumed > maxMemory
&& cpuUsagePercentage <= cpuUsageThreshold && cpuUsagePercentage <= cpuUsageThreshold
&& !isInMeeting && !isInMeeting
&& getIsOnline() && getIsOnline()
&& canReload && canReload
&& networkRequestCount <= 0 && activeNetworkRequest
) { ) {
getConfigField('memoryRefresh') getConfigField('memoryRefresh')
.then((enabled) => { .then((enabled) => {
@ -70,7 +69,7 @@ function optimizeMemory() {
memory consumption was ${memoryConsumed} memory consumption was ${memoryConsumed}
CPU usage percentage was ${preloadMemory.cpuUsage.percentCPUUsage} CPU usage percentage was ${preloadMemory.cpuUsage.percentCPUUsage}
user was in a meeting? ${isInMeeting} 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()}`); is network online? ${getIsOnline()}`);
mainWindow.reload(); mainWindow.reload();
@ -89,7 +88,7 @@ function optimizeMemory() {
memory consumption was ${memoryConsumed} memory consumption was ${memoryConsumed}
CPU usage percentage was ${preloadMemory.cpuUsage.percentCPUUsage} CPU usage percentage was ${preloadMemory.cpuUsage.percentCPUUsage}
user was in a meeting? ${isInMeeting} 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()}`); is network online? ${getIsOnline()}`);
} }
} }
@ -107,10 +106,11 @@ function setIsInMeeting(meetingStatus) {
* *
* @param memoryInfo - memory consumption of the preload main script * @param memoryInfo - memory consumption of the preload main script
* @param cpuUsage - CPU usage 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'); log.send(logLevels.INFO, 'Memory info received from preload process now running optimize memory logic');
preloadMemory = { memoryInfo, cpuUsage }; preloadMemory = { memoryInfo, cpuUsage, activeRequests };
optimizeMemory(); 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 = { module.exports = {
setIsInMeeting, setIsInMeeting,
setPreloadMemoryInfo, setPreloadMemoryInfo,
setPreloadWindow, setPreloadWindow,
monitorNetworkRequest,
}; };

View File

@ -366,6 +366,18 @@ function createAPI() {
throttledSetLocale(locale); 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. // add support for both ssf and SYM_API name-space.
@ -518,10 +530,12 @@ function createAPI() {
if (window.name === 'main') { if (window.name === 'main') {
const memory = process.getProcessMemoryInfo(); const memory = process.getProcessMemoryInfo();
const cpuUsage = process.getCPUUsage(); const cpuUsage = process.getCPUUsage();
const activeRequests = local.activeRequests();
local.ipcRenderer.send(apiName, { local.ipcRenderer.send(apiName, {
cmd: apiCmds.optimizeMemoryConsumption, cmd: apiCmds.optimizeMemoryConsumption,
memory, memory,
cpuUsage, cpuUsage,
activeRequests,
}); });
} }
}); });