http: Only send connection:keep-alive if necessary
[platform/upstream/nodejs.git] / lib / _http_client.js
index 63f5ee9..a1dc3be 100644 (file)
@@ -107,9 +107,18 @@ function ClientRequest(options, cb) {
     var conn = self.agent.createConnection({ path: self.socketPath });
     self.onSocket(conn);
   } else if (self.agent) {
-    // If there is an agent we should default to Connection:keep-alive.
-    self._last = false;
-    self.shouldKeepAlive = true;
+    // If there is an agent we should default to Connection:keep-alive,
+    // but only if the Agent will actually reuse the connection!
+    // If it's not a keepAlive agent, and the maxSockets==Infinity, then
+    // there's never a case where this socket will actually be reused
+    var agent = self.agent;
+    if (!agent.keepAlive && !Number.isFinite(agent.maxSockets)) {
+      self._last = true;
+      self.shouldKeepAlive = false;
+    } else {
+      self._last = false;
+      self.shouldKeepAlive = true;
+    }
     self.agent.addRequest(self, options);
   } else {
     // No agent, default to Connection:close.