Skip to content

Commit b43327a

Browse files
funkenstrahlendavimacedo
authored andcommitted
Add support for handling http2 GOAWAY messages and set http2 ping interval (#7)
* Add ping interval to native http2 implementation * Gracefully destroy http2stream on GOAWAY frame
1 parent d5dd5c6 commit b43327a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/client.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ module.exports = function (dependencies) {
1919

2020
function Client (options) {
2121
this.config = config(options);
22+
this.healthCheckInterval = setInterval(() => {
23+
if (this.session && !this.session.destroyed) {
24+
this.session.ping((error, duration) => {
25+
if (error) {
26+
logger("No Ping response after " + duration + " ms");
27+
}
28+
logger("Ping response after " + duration + " ms");
29+
})
30+
}
31+
}, this.config.heartBeat).unref();
2232
}
2333

2434
Client.prototype.write = function write (notification, device, count) {
@@ -43,6 +53,16 @@ module.exports = function (dependencies) {
4353
}
4454
});
4555

56+
this.session.on("goaway", (errorCode, lastStreamId, opaqueData) => {
57+
logger(`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`);
58+
// gracefully stop accepting new streams
59+
if (this.session && !this.session.destroyed) {
60+
this.session.close(() => {
61+
this.session.destroy();
62+
});
63+
}
64+
});
65+
4666
if (logger.enabled) {
4767
this.session.on("connect", () => {
4868
logger("Session connected");
@@ -53,9 +73,6 @@ module.exports = function (dependencies) {
5373
this.session.on("frameError", (frameType, errorCode, streamId) => {
5474
logger(`Frame error: (frameType: ${frameType}, errorCode ${errorCode}, streamId: ${streamId})`);
5575
});
56-
this.session.on("goaway", (errorCode, lastStreamId, opaqueData) => {
57-
logger(`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`);
58-
});
5976
}
6077
}
6178

@@ -140,6 +157,9 @@ module.exports = function (dependencies) {
140157
};
141158

142159
Client.prototype.shutdown = function shutdown(callback) {
160+
if (this.healthCheckInterval) {
161+
clearInterval(this.healthCheckInterval);
162+
}
143163
if (this.session && !this.session.destroyed) {
144164
this.session.shutdown({graceful: true}, () => {
145165
this.session.destroy();

0 commit comments

Comments
 (0)