From: Jérôme Benoit Date: Tue, 28 Sep 2021 20:56:01 +0000 (+0200) Subject: Plug one more memory leak in the OCPP stack in case of WS closed. X-Git-Tag: v1.1.15~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6198eef3947754c7ad85050cb56014794f60c846;p=e-mobility-charging-stations-simulator.git Plug one more memory leak in the OCPP stack in case of WS closed. Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 18c73918..9f00f9fd 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -26,7 +26,7 @@ import tar from 'tar'; export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService { public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record): Promise { - let response; + let response: Record; const methodName = `handleRequest${commandName}`; // Call if (typeof this[methodName] === 'function') { diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index ffe9f920..1bc8e70c 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -37,21 +37,29 @@ export default abstract class OCPPRequestService { if (this.chargingStation.isWebSocketConnectionOpened()) { // Yes: Send Message const beginId = PerformanceStatistics.beginMeasure(commandName); + // FIXME: Handle sending error this.chargingStation.wsConnection.send(messageToSend); PerformanceStatistics.endMeasure(commandName, beginId); } else if (!skipBufferingOnError) { // Buffer it this.chargingStation.addToMessageQueue(messageToSend); - // Reject it but keep the request in the cache - return reject(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams?.details ?? {})); + const ocppError = new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {}); + if (messageType === MessageType.CALL_MESSAGE) { + // Reject it but keep the request in the cache + return reject(ocppError); + } + return rejectCallback(ocppError); + } else { + // Reject it + return rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})); } // Response? if (messageType !== MessageType.CALL_MESSAGE) { // Yes: send Ok - resolve(commandName); + resolve({ commandName, commandParams }); } else { // Send timeout - setTimeout(() => rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_SOCKET_TIMEOUT); + setTimeout(() => rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_SOCKET_TIMEOUT); } /** @@ -67,7 +75,7 @@ export default abstract class OCPPRequestService { // Send the response await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload); self.chargingStation.requests.delete(messageId); - resolve(payload); + resolve({ commandName, payload, requestPayload }); } /** @@ -91,7 +99,7 @@ export default abstract class OCPPRequestService { throw error; } - private buildMessageToSend(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, + private buildMessageToSend(messageId: string, commandParams: Record, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, responseCallback: (payload: Record | string, requestPayload: Record) => Promise, rejectCallback: (error: OCPPError) => void): string { let messageToSend: string; // Type of message @@ -99,7 +107,7 @@ export default abstract class OCPPRequestService { // Request case MessageType.CALL_MESSAGE: // Build request - this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandName, commandParams as Record]); + this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandName, commandParams]); messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]); break; // Response