Skip to content

Commit 537a7e3

Browse files
authored
memory leak fix (#104)
1 parent 705a947 commit 537a7e3

File tree

10 files changed

+542
-891
lines changed

10 files changed

+542
-891
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
- node/install:
9696
node-version: << pipeline.parameters.node-version >>
9797
- setup_remote_docker:
98-
version: 19.03.13
98+
version: default
9999
docker_layer_caching: true
100100
# build and push Docker image
101101
- run:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.5.1 (October 16, 2024)
2+
* Fixed memory leak in `Re-assembled message` action, when you have more than 1 000 000 messages
3+
14
## 1.5.0 (May 29, 2024)
25
* Revitalized `Re-assembled message` action - now all messages are stored in memory within the component and saved to the external storage (Maester) only when needed
36
* Added new checkbox `Emit result as array` to `Re-assembled message` action

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"title": "Splitter",
33
"description": "Splits a message into multiple messages.",
4-
"version": "1.5.0",
4+
"version": "1.5.1",
55
"actions": {
66
"split": {
77
"deprecated": true,

lib/utils.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ const getUserAgent = () => {
1919
return `${compName}/${compVersion} maester-client/${maesterClientVersion}`;
2020
};
2121

22+
const logMemoryUsage = (logger) => {
23+
const format = (num) => `${Math.round(num / 1024 / 1024)} MB`;
24+
const {
25+
rss,
26+
heapTotal,
27+
heapUsed,
28+
} = process.memoryUsage();
29+
logger.debug(`Memory used - rss ${format(rss)}, heapTotal ${format(heapTotal)}, heapUsed ${format(heapUsed)}`);
30+
};
31+
2232
class GroupsProcessor {
2333
constructor(context, cfg) {
2434
this.cfg = cfg;
@@ -69,6 +79,8 @@ class GroupsProcessor {
6979
this.groupsStorage[groupUniqueId] ||= {};
7080
const group = this.groupsStorage[groupUniqueId];
7181
group.messages ||= [];
82+
const msgSize = JSON.stringify(messageData || {}).length;
83+
group.groupDataSize = group.groupDataSize ? group.groupDataSize + msgSize : msgSize;
7284
group.messages.push({ [messageId]: messageData || {} });
7385
group.readyAfter = timestamp(new Date()) + timersec;
7486
group.groupSize = groupSize;
@@ -86,11 +98,11 @@ class GroupsProcessor {
8698
const group = this.groupsStorage[groupUniqueId];
8799
const readyBySize = this.groupReadyBySize(group);
88100
const readyByTime = this.cfg.mode === 'groupSize' ? false : this.groupReadyByTime(group);
89-
const readyByOverload = JSON.stringify(this.groupsStorage).length > MAX_LOCAL_STORAGE_SIZE;
101+
const readyByOverload = group.groupDataSize > MAX_LOCAL_STORAGE_SIZE;
90102
if (readyBySize || readyByTime || readyByOverload) {
91103
const groupOriginalId = getOriginalGroupId(groupUniqueId);
92104
if (readyByOverload) {
93-
this.logger.warn(`Local storage is overloaded group "${groupOriginalId}" will be emitted to prevent data loss`);
105+
this.logger.warn(`Local storage is overloaded group "${groupOriginalId}" will be emitted to prevent data loss (${Math.round(group.groupDataSize / 1024 / 1024)} MB used)`);
94106
} else {
95107
this.logger.info(`Group "${groupOriginalId}" is ready by ${readyBySize ? 'Size' : 'Time'}`);
96108
}
@@ -99,7 +111,8 @@ class GroupsProcessor {
99111
delete group.messages;
100112
delete group.readyAfter;
101113
delete group.maesterId;
102-
114+
delete group.groupDataSize;
115+
logMemoryUsage(this.logger);
103116
await this.emitGroup(fixedGroup, groupOriginalId);
104117
this.logger.info(`Message with groupId: ${groupOriginalId} was emitted`);
105118
if (fixedGroup.maesterId) {
@@ -161,7 +174,7 @@ class GroupsProcessor {
161174
this.logger.info('All groups saved to maester, stopping status checker');
162175
this.statusCheckerRunning = false;
163176
} else {
164-
await sleep(10);
177+
await sleep(100);
165178
await this.statusChecker();
166179
}
167180
}

0 commit comments

Comments
 (0)