Skip to content

Commit

Permalink
[FSSDK-10180] close http request after getting response to release me…
Browse files Browse the repository at this point in the history
…mory immediately (node) (#927)

* imediately close req and release memory after http call (node)

* handle any status code & error case
  • Loading branch information
pulak-opti authored May 13, 2024
1 parent 75418da commit 34496f2
Showing 1 changed file with 13 additions and 6 deletions.
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

0 comments on commit 34496f2

Please sign in to comment.