On Aug 27, 2014, at 4:40 PM, James Coglan <[email protected]> wrote:
> I'm trying to figure out how HTTP keep-alive works. According to the docs, 
> all requests by default use the global http.Agent (or https.Agent), which 
> pools connections per origin. Consider the following code; it starts a TCP 
> server that provides a basic response to HTTP requests and does not close the 
> socket.
> 

Hello James,

I do not know how well your TCP server code would work in certain scenarios 
(e.g: when there is more than one chunk of request data, which, if I am reading 
your code right, will cause a situation where a response is sent by you before 
the end of the request), but w.r.t http.Agent and keep-alives, you might find 
Issue 4769 an interesting read (and my own comments, perhaps: 
https://github.com/joyent/node/issues/4769#issuecomment-53116476). In your case 
you are sending your third request when the response to the first ends, which 
means (most likely) that after you fired the first and second request, there 
was no request pending, which IIUC causes http.Agent to close open keep-alive 
sockets.

        --ravi


> ```
> var http = require('http'),
>     net  = require('net');
> 
> net.createServer(function(conn) {
>   console.log('[CONNECT]');
> 
>   conn.on('data', function(chunk) {
>     console.log(chunk.toString());
>     conn.write( 'HTTP/1.1 200 OK\r\n' +
>                 'Content-Length: 5\r\n' +
>                 'Connection: keep-alive\r\n' +
>                 '\r\n' +
>                 'Hello');
>   });
> }).listen(4000);
> 
> var origin = 'http://localhost:4000'
> 
> http.get(origin + '/first', function(response) {
>   response.on('data', function() { });
> 
>   response.on('end', function() {
>     http.get(origin + '/third', function(response) { });
>   });
> });
> 
> http.get(origin + '/second', function(response) { });
> ```
> 
> The 'first' and 'second' and requests want to happen in parallel, so I would 
> expect them to use separate TCP connections, and indeed they do. However, the 
> 'third' request, which happens once 'first' is complete, is sent over its own 
> TCP connection rather than reusing the one used for 'first'.
> 
> Is it possible to make sequential requests reuse the TCP connection, and if 
> so how?
> 
> Many thanks,
> James
> 
> -- 
> James Coglan
> http://jcoglan.com

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/DA8DD1E2-5F67-4B02-8DDF-9E034064AD23%40g8o.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to