Skip to content

Add support for handling http2 GOAWAY messages and set http2 ping interval #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ module.exports = function (dependencies) {

function Client (options) {
this.config = config(options);
this.healthCheckInterval = setInterval(() => {
if (this.session && !this.session.destroyed) {
this.session.ping((error, duration) => {
if (error) {
logger("No Ping response after " + duration + " ms");
}
logger("Ping response after " + duration + " ms");
})
}
}, this.config.heartBeat).unref();
}

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

this.session.on("goaway", (errorCode, lastStreamId, opaqueData) => {
logger(`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`);
// gracefully stop accepting new streams
if (this.session && !this.session.destroyed) {
this.session.close(() => {
this.session.destroy();
});
}
});

if (logger.enabled) {
this.session.on("connect", () => {
logger("Session connected");
Expand All @@ -53,9 +73,6 @@ module.exports = function (dependencies) {
this.session.on("frameError", (frameType, errorCode, streamId) => {
logger(`Frame error: (frameType: ${frameType}, errorCode ${errorCode}, streamId: ${streamId})`);
});
this.session.on("goaway", (errorCode, lastStreamId, opaqueData) => {
logger(`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`);
});
}
}

Expand Down Expand Up @@ -140,6 +157,9 @@ module.exports = function (dependencies) {
};

Client.prototype.shutdown = function shutdown(callback) {
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval);
}
if (this.session && !this.session.destroyed) {
this.session.shutdown({graceful: true}, () => {
this.session.destroy();
Expand Down