Track Json WS Requests memory leaks
MODIFY
src/server/ocpp/json/JsonOCPPServer.ts
src/server/ocpp/json/JsonOCPPServer.ts
@@ -21,6 +21,7 @@ import Utils from '../../../utils/Utils';
import WSConnection from './web-socket/WSConnection';
import WSWrapper from './web-socket/WSWrapper';
import global from '../../../types/GlobalType';
+import sizeof from 'object-sizeof';
const MODULE_NAME = 'JsonOCPPServer';
@@ -674,21 +675,39 @@ export default class JsonOCPPServer extends OCPPServer {
private monitorWSConnections() {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
- setInterval(async () => {
+ setTimeout(async () => {
+ try {
+ // Log size of WS Json Connections (track leak)
+ let sizeOfCurrentRequestsBytes = 0, numberOfCurrentRequests = 0;
+ for (const jsonWSConnection of Array.from(this.jsonWSConnections.values())) {
+ const currentOcppRequests = jsonWSConnection.getCurrentOcppRequests();
+ sizeOfCurrentRequestsBytes += sizeof(currentOcppRequests);
+ numberOfCurrentRequests += Object.keys(currentOcppRequests).length;
+ }
+ // Log Stats on number of WS Connections
await Logging.logDebug({
tenantID: Constants.DEFAULT_TENANT_ID,
action: ServerAction.WS_SERVER_CONNECTION, module: MODULE_NAME, method: 'monitorWSConnections',
message: `${this.jsonWSConnections.size} WS connections, ${this.jsonRestWSConnections.size} REST connections, ${this.runningWSMessages} Messages, ${Object.keys(this.runningWSRequestsMessages).length} Requests, ${this.waitingWSMessages} queued WS Message(s)`,
+ detailedMessages: [
+ `${numberOfCurrentRequests} JSON WS Requests cached`,
+ `${sizeOfCurrentRequestsBytes / 1000} kB used in JSON WS cache`
+ ]
});
if (this.isDebug()) {
Logging.logConsoleDebug('=====================================');
Logging.logConsoleDebug(`** ${this.jsonWSConnections.size} JSON Connection(s)`);
+ Logging.logConsoleDebug(`** ${numberOfCurrentRequests} JSON WS Requests in cache with a size of ${sizeOfCurrentRequestsBytes / 1000} kB`);
Logging.logConsoleDebug(`** ${this.jsonRestWSConnections.size} REST Connection(s)`);
Logging.logConsoleDebug(`** ${Object.keys(this.runningWSRequestsMessages).length} running WS Requests`);
Logging.logConsoleDebug(`** ${this.runningWSMessages} running WS Messages (Requests + Responses)`);
Logging.logConsoleDebug(`** ${this.waitingWSMessages} queued WS Message(s)`);
Logging.logConsoleDebug('=====================================');
}
+ } finally {
+ // Relaunch it
+ this.monitorWSConnections();
+ }
}, Configuration.getChargingStationConfig().monitoringIntervalOCPPJSecs * 1000);
}

MODIFY
src/server/ocpp/json/web-socket/WSConnection.ts
src/server/ocpp/json/web-socket/WSConnection.ts
@@ -308,6 +308,10 @@ export default abstract class WSConnection {
return `${this.getTenantID()}~${this.getChargingStationID()}`;
}
+ public getCurrentOcppRequests(): Record<string, OCPPRequest> {
+ return this.ocppRequests;
+ }
+
private checkMandatoryFieldsInRequest() {
// Check URL: remove starting and trailing '/'
if (this.url.endsWith('/')) {

Track Json WS Requests memory leaks