Thursday, 8 June 2017

Lifetime of http request

In learning node and working through a few examples, I have a prototype which is working perfectly in test but I am worried that it is working 'by accident'.

The code, (heavily stripped down) boils down to the following

function CheckValue(){
  var req=http.request({/*some options*/},
                       function (resp){
                       resp.on('data',
                               function (s){
                         /* process the response */
                       })});
  /* Tweak a few headers here. */
  req.end();
}

now if I understand the documents correctly - the request is actually sent to the server at the point of req.end() and the callback is not processed until data is received.

So; if there is significant delay between the request being issued and the response being received could the req object be garbage collected? If so would the callback presumably not be called? Or, looking at it from another angle: does node internally maintain any references to http requests until they are complete?

(The code is deliberately written in a fire and forget manner, with req being internally scoped within the function and not saved in any global scope. It would be feasible to store the request in an array and delete the request during resp.on('end',...) but I would rather avoid the complication and overhead.)



via Richard

No comments:

Post a Comment