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.