samples/JavaScript/http.js
// concerned with managing a connection pool.
concerned with managing a connection pool.
concerned with managing a connection pool .
function Agent(options) {
  var self = this;
  self.options = options || {};
  self.requests = {};
  self.sockets = {};
  self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
  self.on('free', function(socket, host, port, localAddress) {
    var name = host + ':' + port;
    if (localAddress) {
      name += ':' + localAddress;
    }
    if (self.requests[name] && self.requests[name].length) {
      self.requests[name].shift().onSocket(socket);
      if (self.requests[name].length === 0) {
        delete self.requests[name];
      }
    } else {
      socket.destroy();
    }
  });
  self.createConnection = net.createConnection;
}
function Agent ( options ) { var self = this ; self . options = options || { } ; self . requests = { } ; self . sockets = { } ; self . maxSockets = self . options . maxSockets || Agent . defaultMaxSockets ; self . on ( 'free' , function ( socket , host , port , localAddress ) { var name = host + ':' + port ; if ( localAddress ) { name += ':' + localAddress ; } if ( self . requests [ name ] && self . requests [ name ] . length ) { self . requests [ name ] . shift ( ) . onSocket ( socket ) ; if ( self . requests [ name ] . length === 0 ) { delete self . requests [ name ] ; } } else { socket . destroy ( ) ; } } ) ; self . createConnection = net . createConnection ; }
concerned with managing a connection pool.
concerned with managing a connection pool .
// don't leak // If there are no pending requests just destroy the // socket and it will get removed from the pool. This // gets us out of timeout issues and allows us to // default to Connection:keep-alive.