Skip to content

Commit

Permalink
http2: reuse ._onTimeout() in Http2Session and Http2Stream classes
Browse files Browse the repository at this point in the history
PR-URL: #33354
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
rickyes authored and codebytere committed Jul 8, 2020
1 parent 7df79f4 commit bcdf4c8
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1449,27 +1449,7 @@ class Http2Session extends EventEmitter {
}

_onTimeout() {
// If the session is destroyed, this should never actually be invoked,
// but just in case...
if (this.destroyed)
return;
// This checks whether a write is currently in progress and also whether
// that write is actually sending data across the write. The kHandle
// stored `chunksSentSinceLastWrite` is only updated when a timeout event
// happens, meaning that if a write is ongoing it should never equal the
// newly fetched, updated value.
if (this[kState].writeQueueSize > 0) {
const handle = this[kHandle];
const chunksSentSinceLastWrite = handle !== undefined ?
handle.chunksSentSinceLastWrite : null;
if (chunksSentSinceLastWrite !== null &&
chunksSentSinceLastWrite !== handle.updateChunksSent()) {
this[kUpdateTimer]();
return;
}
}

this.emit('timeout');
callTimeout(this);
}

ref() {
Expand Down Expand Up @@ -1894,25 +1874,7 @@ class Http2Stream extends Duplex {
}

_onTimeout() {
if (this.destroyed)
return;
// This checks whether a write is currently in progress and also whether
// that write is actually sending data across the write. The kHandle
// stored `chunksSentSinceLastWrite` is only updated when a timeout event
// happens, meaning that if a write is ongoing it should never equal the
// newly fetched, updated value.
if (this[kState].writeQueueSize > 0) {
const handle = this[kSession][kHandle];
const chunksSentSinceLastWrite = handle !== undefined ?
handle.chunksSentSinceLastWrite : null;
if (chunksSentSinceLastWrite !== null &&
chunksSentSinceLastWrite !== handle.updateChunksSent()) {
this[kUpdateTimer]();
return;
}
}

this.emit('timeout');
callTimeout(this, kSession);
}

// True if the HEADERS frame has been sent
Expand Down Expand Up @@ -2190,6 +2152,30 @@ class Http2Stream extends Duplex {
}
}

function callTimeout(self, kSession) {
// If the session is destroyed, this should never actually be invoked,
// but just in case...
if (self.destroyed)
return;
// This checks whether a write is currently in progress and also whether
// that write is actually sending data across the write. The kHandle
// stored `chunksSentSinceLastWrite` is only updated when a timeout event
// happens, meaning that if a write is ongoing it should never equal the
// newly fetched, updated value.
if (self[kState].writeQueueSize > 0) {
const handle = kSession ? self[kSession][kHandle] : self[kHandle];
const chunksSentSinceLastWrite = handle !== undefined ?
handle.chunksSentSinceLastWrite : null;
if (chunksSentSinceLastWrite !== null &&
chunksSentSinceLastWrite !== handle.updateChunksSent()) {
self[kUpdateTimer]();
return;
}
}

self.emit('timeout');
}

function callStreamClose(stream) {
stream.close();
}
Expand Down

0 comments on commit bcdf4c8

Please sign in to comment.