Skip to content

Commit

Permalink
Merge pull request #2 from simllll/master
Browse files Browse the repository at this point in the history
memory leak fix
  • Loading branch information
patrickd- authored Jan 17, 2017
2 parents 785aff4 + 5421660 commit 0e173de
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/protocol/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,18 @@ Connection.prototype._insert = function _insert(stream, priority) {
};

Connection.prototype._reprioritize = function _reprioritize(stream, priority) {
this._removePrioritisedStream(stream);
this._insert(stream, priority);
};

Connection.prototype._removePrioritisedStream = function _removePrioritisedStream(stream) {
var bucket = this._streamPriorities[stream._priority];
var index = bucket.indexOf(stream);
assert(index !== -1);
bucket.splice(index, 1);
if (bucket.length === 0) {
delete this._streamPriorities[stream._priority];
}

this._insert(stream, priority);
};

// Creating an *inbound* stream with the given ID. It is called when there's an incoming frame to
Expand All @@ -246,9 +249,18 @@ Connection.prototype.createStream = function createStream() {
var stream = new Stream(this._log, this);
this._allocatePriority(stream);

stream.on('end', this._removeStream.bind(this, stream));

return stream;
};

Connection.prototype._removeStream = function _removeStream(stream) {
this._log.trace('Removing outbound stream.');

delete this._streamIds[stream.id];
this._removePrioritisedStream(stream);
};

// Multiplexing
// ------------

Expand Down Expand Up @@ -290,7 +302,7 @@ priority_loop:
// 2. if there's no frame, skip this stream
// 3. if forwarding this frame would make `streamCount` greater than `streamLimit`, skip
// this stream
// 4. adding stream to the bucket of the next round
// 4. adding stream to the bucket of the next round unless it has ended
// 5. assigning an ID to the frame (allocating an ID to the stream if there isn't already)
// 6. if forwarding a PUSH_PROMISE, allocate ID to the promised stream
// 7. forwarding the frame, changing `streamCount` as appropriate
Expand All @@ -299,6 +311,7 @@ priority_loop:
while (bucket.length > 0) {
for (var index = 0; index < bucket.length; index++) {
var stream = bucket[index];
if(!stream || !stream.upstream) continue;
var frame = stream.upstream.read((this._window > 0) ? this._window : -1);

if (!frame) {
Expand All @@ -308,7 +321,11 @@ priority_loop:
continue;
}

nextBucket.push(stream);
if (!stream._ended) {
nextBucket.push(stream);
} else {
delete this._streamIds[stream.id];
}

if (frame.stream === undefined) {
frame.stream = stream.id || this._allocateId(stream);
Expand Down

0 comments on commit 0e173de

Please sign in to comment.