Skip to content

[FSSDK-10180] close http request after getting response to release memory immediately (node) #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/plugins/event_dispatcher/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018, 2020-2021, Optimizely
* Copyright 2016-2018, 2020-2021, 2024 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,19 +51,26 @@ export const dispatchEvent = function(
},
};

const reqWrapper: { req?: http.ClientRequest } = {};

const requestCallback = function(response?: { statusCode: number }): void {
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
callback(response);
}
reqWrapper.req?.destroy();
reqWrapper.req = undefined;
};

const req = (parsedUrl.protocol === 'http:' ? http : https)
reqWrapper.req = (parsedUrl.protocol === 'http:' ? http : https)
.request(requestOptions, requestCallback as (res: http.IncomingMessage) => void);
// Add no-op error listener to prevent this from throwing
req.on('error', function() {});
req.write(dataString);
req.end();
return req;
reqWrapper.req.on('error', function() {
reqWrapper.req?.destroy();
reqWrapper.req = undefined;
});
reqWrapper.req.write(dataString);
reqWrapper.req.end();
return reqWrapper.req;
};

export default {
Expand Down
Loading