@@ -20,6 +20,9 @@ class TunnelAgent extends Agent {
20
20
21
21
// sockets we can hand out via createConnection
22
22
this . availableSockets = [ ] ;
23
+ this . port = null ;
24
+ this . clientId = options . clientId
25
+ this . portManager = options . portManager || null ;
23
26
24
27
// when a createConnection cannot return a socket, it goes into a queue
25
28
// once a socket is available it is handed out to the next callback
@@ -63,13 +66,14 @@ class TunnelAgent extends Agent {
63
66
} ) ;
64
67
65
68
return new Promise ( ( resolve ) => {
66
- server . listen ( ( ) => {
67
- const port = server . address ( ) . port ;
68
- this . debug ( 'tcp server listening on port: %d' , port ) ;
69
+ const port = this . portManager ? this . portManager . getNextAvailable ( this . options . clientId ) : null ;
70
+ server . listen ( port , ( ) => {
71
+ this . port = server . address ( ) . port
72
+ this . debug ( 'tcp server listening on port: %d (%s)' , this . port , this . clientId ) ;
69
73
70
74
resolve ( {
71
75
// port for lt client tcp connections
72
- port : port ,
76
+ port : this . port ,
73
77
} ) ;
74
78
} ) ;
75
79
} ) ;
@@ -115,6 +119,9 @@ class TunnelAgent extends Agent {
115
119
socket . once ( 'error' , ( err ) => {
116
120
// we do not log these errors, sessions can drop from clients for many reasons
117
121
// these are not actionable errors for our server
122
+ if ( this . portManager ) {
123
+ this . portManager . release ( this . port ) ;
124
+ }
118
125
socket . destroy ( ) ;
119
126
} ) ;
120
127
0 commit comments