Skip to content

Commit 03f1fff

Browse files
committed
imediately close req and release memory after http call (node)
1 parent 75418da commit 03f1fff

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/plugins/event_dispatcher/index.node.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,22 @@ export const dispatchEvent = function(
5151
},
5252
};
5353

54+
const reqWrapper: { req?: http.ClientRequest } = {};
55+
5456
const requestCallback = function(response?: { statusCode: number }): void {
5557
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
58+
reqWrapper.req?.destroy();
5659
callback(response);
5760
}
5861
};
5962

60-
const req = (parsedUrl.protocol === 'http:' ? http : https)
63+
reqWrapper.req = (parsedUrl.protocol === 'http:' ? http : https)
6164
.request(requestOptions, requestCallback as (res: http.IncomingMessage) => void);
6265
// Add no-op error listener to prevent this from throwing
63-
req.on('error', function() {});
64-
req.write(dataString);
65-
req.end();
66-
return req;
66+
reqWrapper.req.on('error', function() {});
67+
reqWrapper.req.write(dataString);
68+
reqWrapper.req.end();
69+
return reqWrapper.req;
6770
};
6871

6972
export default {

0 commit comments

Comments
 (0)