{"action":"created","issue":{"url":"https://api.github.com/repos/joyent/node/issues/9075","labels_url":"https://api.github.com/repos/joyent/node/issues/9075/labels{/name}","comments_url":"https://api.github.com/repos/joyent/node/issues/9075/comments","events_url":"https://api.github.com/repos/joyent/node/issues/9075/events","html_url":"https://github.com/joyent/node/issues/9075","id":55094975,"number":9075,"title":"setting http.globalAgent.keepAlive does nothing","user":{"login":"andrasq","id":5438027,"avatar_url":"https://avatars.githubusercontent.com/u/5438027?v=3","gravatar_id":"","url":"https://api.github.com/users/andrasq","html_url":"https://github.com/andrasq","followers_url":"https://api.github.com/users/andrasq/followers","following_url":"https://api.github.com/users/andrasq/following{/other_user}","gists_url":"https://api.github.com/users/andrasq/gists{/gist_id}","starred_url":"https://api.github.com/users/andrasq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrasq/subscriptions","organizations_url":"https://api.github.com/users/andrasq/orgs","repos_url":"https://api.github.com/users/andrasq/repos","events_url":"https://api.github.com/users/andrasq/events{/privacy}","received_events_url":"https://api.github.com/users/andrasq/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-21T23:49:17Z","updated_at":"2015-01-22T17:55:20Z","closed_at":null,"body":"this must be an oversight, but to configure keepAlive in the global agent, `http.globalAgent.options.keepAlive` has to be changed.  The code sets `self.keepAlive` from options, but then later uses `this.options.keepAlive` instead.\r\n\r\nSee lines 58 and 80 of lib/_http_agent.js in v0.12"},"comment":{"url":"https://api.github.com/repos/joyent/node/issues/comments/71065592","html_url":"https://github.com/joyent/node/issues/9075#issuecomment-71065592","issue_url":"https://api.github.com/repos/joyent/node/issues/9075","id":71065592,"user":{"login":"andrasq","id":5438027,"avatar_url":"https://avatars.githubusercontent.com/u/5438027?v=3","gravatar_id":"","url":"https://api.github.com/users/andrasq","html_url":"https://github.com/andrasq","followers_url":"https://api.github.com/users/andrasq/followers","following_url":"https://api.github.com/users/andrasq/following{/other_user}","gists_url":"https://api.github.com/users/andrasq/gists{/gist_id}","starred_url":"https://api.github.com/users/andrasq/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrasq/subscriptions","organizations_url":"https://api.github.com/users/andrasq/orgs","repos_url":"https://api.github.com/users/andrasq/repos","events_url":"https://api.github.com/users/andrasq/events{/privacy}","received_events_url":"https://api.github.com/users/andrasq/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T17:55:20Z","updated_at":"2015-01-22T17:55:20Z","body":"The behavior of http.globalAgent is changed by setting public properties on it.  E.g., `http.globalAgent.maxSockets = 1`\r\n\r\nSetting the http.globalAgent.keepAlive property does nothing.  To turn on keepAlive,  `http.globalAgent.options.keepAlive = true` needs to be set.  It should be .keepAlive, not .options.keepAlive.\r\n\r\nThis is due to an oversight in the code; keepAlive is copied from options but is not subsequently used.  See lines 58 and 80 of lib/_http_agent.js in v0.12 (commit f2b378b850b3bc5ac7e30fb7a204ef19ca4f8604)\r\n\r\nSample code:\r\n```\r\nhttp = require('http');\r\n\r\nfunction onRequest(res) {\r\n    res.on('data', function(chunk) { console.log(chunk.toString()) });\r\n    res.on('end', makeCalls);\r\n}\r\n\r\nncalls = 10;\r\nfunction makeCalls( ) {\r\n    options = { host: 'google.com', port: 80, path: '/' };\r\n    if (ncalls-- > 0) http.request(options, onRequest).end();\r\n}\r\n\r\nhttp.globalAgent.keepAlive = true;\r\nmakeCalls();\r\n```\r\n\r\nCount sockets in TIME_WAIT state before running the test:\r\n`netstat -na | grep TIME_WAIT | wc -lc`\r\n`     9    1520`\r\nRun the test:\r\n`node-v0.12 t.js`\r\nCount sockets in TIME_WAIT after running the test:\r\n`netstat -na | grep TIME_WAIT | wc -lc`\r\n`     19    1520`\r\nSame thing, again:\r\n`node-v0.12 t.js ; netstat -na | grep TIME_WAIT | wc -lc`\r\n`     29    2320`\r\n\r\nExpected:  1 socket left unusable in TIME_WAIT\r\nActual:  10 additional sockets left in TIME_WAIT after each set of 10 http.request calls\r\n\r\nModify the test to set .options.keepAlive instead:  change\r\n```\r\nhttp.globalAgent.keepAlive = true;\r\n```\r\nto\r\n```\r\nhttp.globalAgent.options.keepAlive = true;\r\n```\r\nRun the modified test several times, count TIME_WAIT after each run:\r\n`node-v0.12 t.js ; netstat -na | grep TIME_WAIT | wc -lc`\r\n`     10     800`\r\n`node-v0.12 t.js ; netstat -na | grep TIME_WAIT | wc -lc`\r\n`     10     800`\r\n`node-v0.12 t.js ; netstat -na | grep TIME_WAIT | wc -lc`\r\n`     10     800`\r\n\r\nExpected: 1 socket left in TIME_WAIT\r\nActual: 0 additional sockets left in TIME_WAIT\r\n"}}