mirror of
https://github.com/finos/SymphonyElectron.git
synced 2025-01-03 12:47:13 -06:00
ELECTRON-758 - Change network request implementation to use electron's API (#509)
This commit is contained in:
parent
6321cc7dbd
commit
0c8b322d63
@ -26,6 +26,7 @@ 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')();
|
||||||
|
|
||||||
@ -199,7 +200,8 @@ 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);
|
||||||
|
@ -154,9 +154,8 @@ 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') {
|
||||||
&& typeof arg.activeRequests === 'number') {
|
setPreloadMemoryInfo(arg.memory, arg.cpuUsage);
|
||||||
setPreloadMemoryInfo(arg.memory, arg.cpuUsage, arg.activeRequests);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case apiCmds.optimizeMemoryRegister:
|
case apiCmds.optimizeMemoryRegister:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
'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');
|
||||||
@ -18,6 +19,7 @@ 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);
|
||||||
@ -49,14 +51,13 @@ 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
|
||||||
&& activeNetworkRequest
|
&& networkRequestCount <= 0
|
||||||
) {
|
) {
|
||||||
getConfigField('memoryRefresh')
|
getConfigField('memoryRefresh')
|
||||||
.then((enabled) => {
|
.then((enabled) => {
|
||||||
@ -69,7 +70,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 ${preloadMemory.activeRequests}
|
pending network request on the client was ${networkRequestCount}
|
||||||
is network online? ${getIsOnline()}`);
|
is network online? ${getIsOnline()}`);
|
||||||
mainWindow.reload();
|
mainWindow.reload();
|
||||||
|
|
||||||
@ -88,7 +89,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 ${preloadMemory.activeRequests}
|
pending network request on the client was ${networkRequestCount}
|
||||||
is network online? ${getIsOnline()}`);
|
is network online? ${getIsOnline()}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,11 +107,10 @@ 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, activeRequests) {
|
function setPreloadMemoryInfo(memoryInfo, cpuUsage) {
|
||||||
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, activeRequests };
|
preloadMemory = { memoryInfo, cpuUsage };
|
||||||
optimizeMemory();
|
optimizeMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,8 +189,44 @@ 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,
|
||||||
};
|
};
|
@ -366,18 +366,6 @@ 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.
|
||||||
@ -530,12 +518,10 @@ 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,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user