Skip to content

Commit 15a5a4a

Browse files
committed
http: Only send connection:keep-alive if necessary
In cases where the Agent has maxSockets=Infinity, and keepAlive=false, there's no case where we won't immediately close the connection after the response is completed. Since we're going to close it anyway, send a `connection:close` header rather than a `connection:keep-alive` header. Still send the `connection:keep-alive` if the agent will actually reuse the socket, however. Closes #5838
1 parent 689e5c9 commit 15a5a4a

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

lib/_http_client.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,18 @@ function ClientRequest(options, cb) {
107107
var conn = self.agent.createConnection({ path: self.socketPath });
108108
self.onSocket(conn);
109109
} else if (self.agent) {
110-
// If there is an agent we should default to Connection:keep-alive.
111-
self._last = false;
112-
self.shouldKeepAlive = true;
110+
// If there is an agent we should default to Connection:keep-alive,
111+
// but only if the Agent will actually reuse the connection!
112+
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
113+
// there's never a case where this socket will actually be reused
114+
var agent = self.agent;
115+
if (!agent.keepAlive && !Number.isFinite(agent.maxSockets)) {
116+
self._last = true;
117+
self.shouldKeepAlive = false;
118+
} else {
119+
self._last = false;
120+
self.shouldKeepAlive = true;
121+
}
113122
self.agent.addRequest(self, options);
114123
} else {
115124
// No agent, default to Connection:close.

test/simple/test-http-raw-headers.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ http.createServer(function(req, res) {
3434
'x-BaR',
3535
'yoyoyo',
3636
'Connection',
37-
'keep-alive'
37+
'close'
3838
];
3939
var expectHeaders = {
4040
host: 'localhost:' + common.PORT,
4141
'transfer-encoding': 'CHUNKED',
4242
'x-bar': 'yoyoyo',
43-
connection: 'keep-alive'
43+
connection: 'close'
4444
};
4545

4646
var expectRawTrailers = [
@@ -77,7 +77,7 @@ http.createServer(function(req, res) {
7777
'Date',
7878
'Tue, 06 Aug 2013 01:31:54 GMT',
7979
'Connection',
80-
'keep-alive',
80+
'close',
8181
'Transfer-Encoding',
8282
'chunked'
8383
];
@@ -96,13 +96,13 @@ http.createServer(function(req, res) {
9696
'Date',
9797
null,
9898
'Connection',
99-
'keep-alive',
99+
'close',
100100
'Transfer-Encoding',
101101
'chunked'
102102
];
103103
var expectHeaders = {
104104
date: null,
105-
connection: 'keep-alive',
105+
connection: 'close',
106106
'transfer-encoding': 'chunked'
107107
};
108108
res.rawHeaders[1] = null;

test/simple/test-http-should-keep-alive.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var SHOULD_KEEP_ALIVE = [
4242
];
4343
var requests = 0;
4444
var responses = 0;
45+
http.globalAgent.maxSockets = 5;
4546

4647
var server = net.createServer(function(socket) {
4748
socket.write(SERVER_RESPONSES[requests]);

0 commit comments

Comments
 (0)