@@ -981,8 +981,12 @@ function Agent(options) {
981981 self . requests = { } ;
982982 self . sockets = { } ;
983983 self . maxSockets = self . options . maxSockets || Agent . defaultMaxSockets ;
984- self . on ( 'free' , function ( socket , host , port ) {
984+ self . on ( 'free' , function ( socket , host , port , localAddress ) {
985985 var name = host + ':' + port ;
986+ if ( localAddress ) {
987+ name += ':' + localAddress ;
988+ }
989+
986990 if ( self . requests [ name ] && self . requests [ name ] . length ) {
987991 self . requests [ name ] . shift ( ) . onSocket ( socket ) ;
988992 if ( self . requests [ name ] . length === 0 ) {
@@ -1005,14 +1009,17 @@ exports.Agent = Agent;
10051009Agent . defaultMaxSockets = 5 ;
10061010
10071011Agent . prototype . defaultPort = 80 ;
1008- Agent . prototype . addRequest = function ( req , host , port ) {
1012+ Agent . prototype . addRequest = function ( req , host , port , localAddress ) {
10091013 var name = host + ':' + port ;
1014+ if ( localAddress ) {
1015+ name += ':' + localAddress ;
1016+ }
10101017 if ( ! this . sockets [ name ] ) {
10111018 this . sockets [ name ] = [ ] ;
10121019 }
10131020 if ( this . sockets [ name ] . length < this . maxSockets ) {
10141021 // If we are under maxSockets create a new one.
1015- req . onSocket ( this . createSocket ( name , host , port ) ) ;
1022+ req . onSocket ( this . createSocket ( name , host , port , localAddress ) ) ;
10161023 } else {
10171024 // We are over limit so we'll add it to the queue.
10181025 if ( ! this . requests [ name ] ) {
@@ -1021,37 +1028,41 @@ Agent.prototype.addRequest = function(req, host, port) {
10211028 this . requests [ name ] . push ( req ) ;
10221029 }
10231030} ;
1024- Agent . prototype . createSocket = function ( name , host , port ) {
1031+ Agent . prototype . createSocket = function ( name , host , port , localAddress ) {
10251032 var self = this ;
1026- var s = self . createConnection ( port , host , self . options ) ;
1033+ var options = util . _extend ( { } , self . options ) ;
1034+ options . port = port ;
1035+ options . host = host ;
1036+ options . localAddress = localAddress ;
1037+ var s = self . createConnection ( options ) ;
10271038 if ( ! self . sockets [ name ] ) {
10281039 self . sockets [ name ] = [ ] ;
10291040 }
10301041 this . sockets [ name ] . push ( s ) ;
10311042 var onFree = function ( ) {
1032- self . emit ( 'free' , s , host , port ) ;
1043+ self . emit ( 'free' , s , host , port , localAddress ) ;
10331044 }
10341045 s . on ( 'free' , onFree ) ;
10351046 var onClose = function ( err ) {
10361047 // This is the only place where sockets get removed from the Agent.
10371048 // If you want to remove a socket from the pool, just close it.
10381049 // All socket errors end in a close event anyway.
1039- self . removeSocket ( s , name , host , port ) ;
1050+ self . removeSocket ( s , name , host , port , localAddress ) ;
10401051 }
10411052 s . on ( 'close' , onClose ) ;
10421053 var onRemove = function ( ) {
10431054 // We need this function for cases like HTTP 'upgrade'
10441055 // (defined by WebSockets) where we need to remove a socket from the pool
10451056 // because it'll be locked up indefinitely
1046- self . removeSocket ( s , name , host , port ) ;
1057+ self . removeSocket ( s , name , host , port , localAddress ) ;
10471058 s . removeListener ( 'close' , onClose ) ;
10481059 s . removeListener ( 'free' , onFree ) ;
10491060 s . removeListener ( 'agentRemove' , onRemove ) ;
10501061 }
10511062 s . on ( 'agentRemove' , onRemove ) ;
10521063 return s ;
10531064} ;
1054- Agent . prototype . removeSocket = function ( s , name , host , port ) {
1065+ Agent . prototype . removeSocket = function ( s , name , host , port , localAddress ) {
10551066 if ( this . sockets [ name ] ) {
10561067 var index = this . sockets [ name ] . indexOf ( s ) ;
10571068 if ( index !== - 1 ) {
@@ -1064,8 +1075,7 @@ Agent.prototype.removeSocket = function(s, name, host, port) {
10641075 }
10651076 if ( this . requests [ name ] && this . requests [ name ] . length ) {
10661077 // If we have pending requests and a socket gets closed a new one
1067- // needs to be created to take over in the pool for the one that closed.
1068- this . createSocket ( name , host , port ) . emit ( 'free' ) ;
1078+ this . createSocket ( name , host , port , localAddress ) . emit ( 'free' ) ;
10691079 }
10701080} ;
10711081
@@ -1144,15 +1154,21 @@ function ClientRequest(options, cb) {
11441154 // If there is an agent we should default to Connection:keep-alive.
11451155 self . _last = false ;
11461156 self . shouldKeepAlive = true ;
1147- self . agent . addRequest ( self , host , port ) ;
1157+ self . agent . addRequest ( self , host , port , options . localAddress ) ;
11481158 } else {
11491159 // No agent, default to Connection:close.
11501160 self . _last = true ;
11511161 self . shouldKeepAlive = false ;
11521162 if ( options . createConnection ) {
1153- var conn = options . createConnection ( port , host , options ) ;
1163+ options . port = port ;
1164+ options . host = host ;
1165+ var conn = options . createConnection ( options ) ;
11541166 } else {
1155- var conn = net . createConnection ( port , host ) ;
1167+ var conn = net . createConnection ( {
1168+ port : port ,
1169+ host : host ,
1170+ localAddress : options . localAddress
1171+ } ) ;
11561172 }
11571173 self . onSocket ( conn ) ;
11581174 }
0 commit comments