Skip to content

Commit 8c5e737

Browse files
ihar-tsykalaIhar
andauthored
fix: leak of memory in guardin by stoped policy (#4782)
Co-authored-by: Ihar <[email protected]>
1 parent 56ea4db commit 8c5e737

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

common/src/mq/nats-service.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ export abstract class NatsService {
127127
* @param subject
128128
* @param data
129129
* @param isResponseCallback
130+
* @param externalMessageId
130131
*/
131-
public sendMessage<T>(subject: string, data?: unknown, isResponseCallback: boolean = true): Promise<T> {
132-
const messageId = GenerateUUIDv4();
132+
public sendMessage<T>(subject: string, data?: unknown, isResponseCallback: boolean = true, externalMessageId?: string): Promise<T> {
133+
const messageId = externalMessageId ?? GenerateUUIDv4();
134+
133135
return new Promise(async (resolve, reject) => {
134136
const head = headers();
135137
head.append('messageId', messageId);
@@ -159,14 +161,18 @@ export abstract class NatsService {
159161
* @param data
160162
*/
161163
public sendMessageWithTimeout<T>(subject: string, timeout: number, data?: unknown): Promise<T> {
162-
return Promise.race([
163-
this.sendMessage<T>(subject, data),
164-
new Promise<T>((_, reject) => {
165-
setTimeout(() => {
166-
reject(new Error('Timeout exceed'))
167-
}, timeout)
168-
})
169-
])
164+
const messageId = GenerateUUIDv4();
165+
166+
const messagePromise = this.sendMessage<T>(subject, data, true, messageId);
167+
168+
const timeoutPromise = new Promise<T>((_, reject) => {
169+
setTimeout(() => {
170+
this.responseCallbacksMap.delete(messageId);
171+
reject(new Error('Timeout exceed'));
172+
}, timeout);
173+
});
174+
175+
return Promise.race([messagePromise, timeoutPromise]);
170176
}
171177

172178
/**

guardian-service/src/helpers/guardians.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class GuardiansService extends NatsService {
7474
}),
7575
new Promise<T>((resolve, reject) => {
7676
setTimeout(() => {
77+
this.responseCallbacksMap.delete(messageId);
7778
resolve(null);
7879
}, awaitInterval)
7980
}),

0 commit comments

Comments
 (0)