Skip to content

Commit 9e81bd5

Browse files
authored
Update connection.js
1 parent 2c4c310 commit 9e81bd5

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

lib/protocol/connection.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,18 @@ Connection.prototype._insert = function _insert(stream, priority) {
214214
};
215215

216216
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) {
217222
var bucket = this._streamPriorities[stream._priority];
218223
var index = bucket.indexOf(stream);
219224
assert(index !== -1);
220225
bucket.splice(index, 1);
221226
if (bucket.length === 0) {
222227
delete this._streamPriorities[stream._priority];
223228
}
224-
225-
this._insert(stream, priority);
226229
};
227230

228231
// 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() {
246249
var stream = new Stream(this._log, this);
247250
this._allocatePriority(stream);
248251

252+
stream.on('end', this._removeStream.bind(this, stream));
253+
249254
return stream;
250255
};
251256

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+
252264
// Multiplexing
253265
// ------------
254266

@@ -290,7 +302,7 @@ priority_loop:
290302
// 2. if there's no frame, skip this stream
291303
// 3. if forwarding this frame would make `streamCount` greater than `streamLimit`, skip
292304
// 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
294306
// 5. assigning an ID to the frame (allocating an ID to the stream if there isn't already)
295307
// 6. if forwarding a PUSH_PROMISE, allocate ID to the promised stream
296308
// 7. forwarding the frame, changing `streamCount` as appropriate
@@ -308,7 +320,11 @@ priority_loop:
308320
continue;
309321
}
310322

311-
nextBucket.push(stream);
323+
if (!stream._ended) {
324+
nextBucket.push(stream);
325+
} else {
326+
delete this._streamIds[stream.id];
327+
}
312328

313329
if (frame.stream === undefined) {
314330
frame.stream = stream.id || this._allocateId(stream);

0 commit comments

Comments
 (0)