Name: js-handler/node_modules/restify/node_modules/keep-alive-agent/index.js 
1:
var
2:
  http = require('http'),
3:
  https = require('https'),
4:
  util = require('util')
5:
  ;
6:
 
7:
//----------------------------------------------------------------------------------------
8:
 
9:
function KeepAliveAgent(options)
10:
{
11:
  options = options || {};
12:
  http.Agent.call(this, options);
13:
 
14:
  // Keys are host:port names, values are lists of sockets.
15:
  this.idleSockets = {};
16:
 
17:
  // Replace the 'free' listener set up by the default node Agent above.
18:
  this.removeAllListeners('free');
19:
  this.on('free', KeepAliveAgent.prototype.freeHandler.bind(this));
20:
 
21:
}
22:
util.inherits(KeepAliveAgent, http.Agent);
23:
 
24:
function buildNameKey(host, port, localAddress)
25:
{
26:
  var name = host + ':' + port;
27:
  if (localAddress)
28:
    name += ':' + localAddress;
29:
 
30:
  return name;
31:
}
32:
 
33:
KeepAliveAgent.prototype.freeHandler = function(socket, host, port, localAddress)
34:
{
35:
  var name = buildNameKey(host, port, localAddress);
36:
 
37:
  // If the socket is still useful, return it to the idle pool.
38:
  if (this.isSocketUsable(socket))
39:
  {
40:
    socket._requestCount = socket._requestCount ? socket._requestCount + 1 : 1;
41:
 
42:
    if (!this.idleSockets[name])
43:
      this.idleSockets[name] = [];
44:
 
45:
    this.idleSockets[name].push(socket);
46:
  }
47:
 
48:
  // If we had any pending requests for this name, send the next one off now.
49:
  if (this.requests[name] && this.requests[name].length)
50:
  {
51:
    var nextRequest = this.requests[name].shift();
52:
 
53:
    if (!this.requests[name].length)
54:
      delete this.requests[name];
55:
 
56:
    this.addRequest(nextRequest, host, port, localAddress);
57:
  }
58:
};
59:
 
60:
KeepAliveAgent.prototype.addRequest = function(request, host, port, localAddress)
61:
{
62:
  var name = buildNameKey(host, port, localAddress);
63:
 
64:
  var socket = this.nextIdleSocket(name);
65:
  if (socket)
66:
    request.onSocket(socket);
67:
  else
68:
    return http.Agent.prototype.addRequest.call(this, request, host, port, localAddress);
69:
};
70:
 
71:
KeepAliveAgent.prototype.nextIdleSocket = function(name)
72:
{
73:
  if (!this.idleSockets[name])
74:
    return null;
75:
 
76:
  var socket;
77:
  while(socket = this.idleSockets[name].shift())
78:
  {
79:
    // Check that this socket is still healthy after sitting around on the shelf.
80:
    // This check is the reason this module exists.
81:
    if (this.isSocketUsable(socket))
82:
      return socket;
83:
  }
84:
 
85:
  return null;
86:
};
87:
 
88:
KeepAliveAgent.prototype.isSocketUsable = function(socket)
89:
{
90:
  return !socket.destroyed;
91:
};
92:
 
93:
 
94:
KeepAliveAgent.prototype.removeSocket = function(socket, name, host, port, localAddress)
95:
{
96:
  if (this.idleSockets[name])
97:
  {
98:
    var idx = this.idleSockets[name].indexOf(socket);
99:
    if (idx !== -1)
100:
    {
101:
      this.idleSockets[name].splice(idx, 1);
102:
      if (!this.idleSockets[name].length)
103:
        delete this.idleSockets[name];
104:
    }
105:
  }
106:
 
107:
  http.Agent.prototype.removeSocket.call(this, socket, name, host, port, localAddress);
108:
};
109:
 
110:
//----------------------------------------------------------------------------------------
111:
 
112:
function HTTPSKeepAliveAgent(options)
113:
{
114:
  KeepAliveAgent.call(this, options);
115:
  this.createConnection = https.globalAgent.createConnection;
116:
}
117:
util.inherits(HTTPSKeepAliveAgent, KeepAliveAgent);
118:
 
119:
HTTPSKeepAliveAgent.prototype.defaultPort = 443;
120:
 
121:
HTTPSKeepAliveAgent.prototype.isSocketUsable = function(socket)
122:
{
123:
  // TLS sockets null out their secure pair's ssl field in destroy() and
124:
  // do not set a destroyed flag the way non-secure sockets do.
125:
  return socket.pair && socket.pair.ssl;
126:
};
127:
 
128:
//----------------------------------------------------------------------------------------
129:
 
130:
module.exports = KeepAliveAgent;
131:
KeepAliveAgent.Secure = HTTPSKeepAliveAgent;