@@ -214,15 +214,18 @@ Connection.prototype._insert = function _insert(stream, priority) {
214
214
} ;
215
215
216
216
Connection . prototype . _reprioritize = function _reprioritize ( stream , priority ) {
217
+ this . _removePrioritisedStream ( stream ) ;
218
+ this . _insert ( stream , priority ) ;
219
+ } ;
220
+
221
+ Connection . prototype . _removePrioritisedStream = function _removePrioritisedStream ( stream ) {
217
222
var bucket = this . _streamPriorities [ stream . _priority ] ;
218
223
var index = bucket . indexOf ( stream ) ;
219
224
assert ( index !== - 1 ) ;
220
225
bucket . splice ( index , 1 ) ;
221
226
if ( bucket . length === 0 ) {
222
227
delete this . _streamPriorities [ stream . _priority ] ;
223
228
}
224
-
225
- this . _insert ( stream , priority ) ;
226
229
} ;
227
230
228
231
// Creating an *inbound* stream with the given ID. It is called when there's an incoming frame to
@@ -246,9 +249,18 @@ Connection.prototype.createStream = function createStream() {
246
249
var stream = new Stream ( this . _log , this ) ;
247
250
this . _allocatePriority ( stream ) ;
248
251
252
+ stream . on ( 'end' , this . _removeStream . bind ( this , stream ) ) ;
253
+
249
254
return stream ;
250
255
} ;
251
256
257
+ Connection . prototype . _removeStream = function _removeStream ( stream ) {
258
+ this . _log . trace ( 'Removing outbound stream.' ) ;
259
+
260
+ delete this . _streamIds [ stream . id ] ;
261
+ this . _removePrioritisedStream ( stream ) ;
262
+ } ;
263
+
252
264
// Multiplexing
253
265
// ------------
254
266
@@ -290,7 +302,7 @@ priority_loop:
290
302
// 2. if there's no frame, skip this stream
291
303
// 3. if forwarding this frame would make `streamCount` greater than `streamLimit`, skip
292
304
// this stream
293
- // 4. adding stream to the bucket of the next round
305
+ // 4. adding stream to the bucket of the next round unless it has ended
294
306
// 5. assigning an ID to the frame (allocating an ID to the stream if there isn't already)
295
307
// 6. if forwarding a PUSH_PROMISE, allocate ID to the promised stream
296
308
// 7. forwarding the frame, changing `streamCount` as appropriate
@@ -299,6 +311,7 @@ priority_loop:
299
311
while ( bucket . length > 0 ) {
300
312
for ( var index = 0 ; index < bucket . length ; index ++ ) {
301
313
var stream = bucket [ index ] ;
314
+ if ( ! stream || ! stream . upstream ) continue ;
302
315
var frame = stream . upstream . read ( ( this . _window > 0 ) ? this . _window : - 1 ) ;
303
316
304
317
if ( ! frame ) {
@@ -308,7 +321,11 @@ priority_loop:
308
321
continue ;
309
322
}
310
323
311
- nextBucket . push ( stream ) ;
324
+ if ( ! stream . _ended ) {
325
+ nextBucket . push ( stream ) ;
326
+ } else {
327
+ delete this . _streamIds [ stream . id ] ;
328
+ }
312
329
313
330
if ( frame . stream === undefined ) {
314
331
frame . stream = stream . id || this . _allocateId ( stream ) ;
0 commit comments