Skip to content

Commit 34496f2

Browse files
authored
[FSSDK-10180] close http request after getting response to release memory immediately (node) (#927)
* imediately close req and release memory after http call (node) * handle any status code & error case
1 parent 75418da commit 34496f2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/plugins/event_dispatcher/index.node.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018, 2020-2021, Optimizely
2+
* Copyright 2016-2018, 2020-2021, 2024 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,19 +51,26 @@ 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) {
5658
callback(response);
5759
}
60+
reqWrapper.req?.destroy();
61+
reqWrapper.req = undefined;
5862
};
5963

60-
const req = (parsedUrl.protocol === 'http:' ? http : https)
64+
reqWrapper.req = (parsedUrl.protocol === 'http:' ? http : https)
6165
.request(requestOptions, requestCallback as (res: http.IncomingMessage) => void);
6266
// 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;
67+
reqWrapper.req.on('error', function() {
68+
reqWrapper.req?.destroy();
69+
reqWrapper.req = undefined;
70+
});
71+
reqWrapper.req.write(dataString);
72+
reqWrapper.req.end();
73+
return reqWrapper.req;
6774
};
6875

6976
export default {

0 commit comments

Comments
 (0)