File tree Expand file tree Collapse file tree 2 files changed +14
-6
lines changed Expand file tree Collapse file tree 2 files changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,8 @@ class Http2Agent extends EventEmitter {
18
18
const name = getConnectionName ( _options )
19
19
let connection = this . connections [ name ]
20
20
21
- if ( ! connection || connection . destroyed || connection . closed ) {
21
+ // Force create a new connection if the connection is destroyed or closed or a new socket object is supplied
22
+ if ( ! connection || connection . destroyed || connection . closed || socket ) {
22
23
const connectionOptions = {
23
24
..._options ,
24
25
port : _options . port || 443 ,
@@ -33,6 +34,9 @@ class Http2Agent extends EventEmitter {
33
34
}
34
35
35
36
connection = http2 . connect ( uri , connectionOptions )
37
+ // Connection is created in an unreferenced state and is referenced when a stream is created
38
+ // This is to prevent the connection from keeping the event loop alive
39
+ connection . unref ( )
36
40
37
41
// Counting semaphore, but since node is single-threaded, this is just a counter
38
42
// Multiple streams can be active on a connection
@@ -78,11 +82,6 @@ class Http2Agent extends EventEmitter {
78
82
this . connections [ name ] = connection
79
83
}
80
84
81
- connection . ref ( )
82
- req . once ( 'close' , ( ) => {
83
- connection . unref ( )
84
- } )
85
-
86
85
return connection
87
86
}
88
87
}
Original file line number Diff line number Diff line change @@ -182,8 +182,17 @@ class Http2Request extends EventEmitter {
182
182
. filter ( ( [ key ] ) => ! connectionHeaders . includes ( key . toLowerCase ( ) ) )
183
183
)
184
184
185
+ // The client was created in an unreferenced state and is referenced when a stream is created
186
+ this . _client . ref ( ) ;
185
187
this . stream = this . _client . request ( this . requestHeaders , { endStream} )
186
188
189
+ const unreferenceFn = ( ) => {
190
+ this . _client . unref ( ) ;
191
+ this . stream . off ( 'close' , unreferenceFn ) ;
192
+ }
193
+
194
+ this . stream . on ( 'close' , unreferenceFn ) ;
195
+
187
196
this . registerListeners ( )
188
197
189
198
this [ kHeadersFlushed ] = true
You can’t perform that action at this time.
0 commit comments