Skip to content

Commit ce7c0d4

Browse files
fix: reconnect after receiving a goaway
1 parent 1022f46 commit ce7c0d4

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

lib/client.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module.exports = function (dependencies) {
3535

3636
function Client(options) {
3737
this.isDestroyed = false;
38+
this.isSessionReady = false;
39+
this.isManageChannelSessionReady = false;
3840
this.config = config(options);
3941
this.logger = defaultLogger;
4042
this.errorLogger = defaultErrorLogger;
@@ -191,6 +193,7 @@ module.exports = function (dependencies) {
191193
if (path.includes('/1/apps/')) {
192194
// Connect manageChannelsSession.
193195
if (
196+
!this.isManageChannelSessionReady ||
194197
!this.manageChannelsSession ||
195198
this.manageChannelsSession.closed ||
196199
this.manageChannelsSession.destroyed
@@ -244,7 +247,7 @@ module.exports = function (dependencies) {
244247
}
245248
} else {
246249
// Connect to standard session.
247-
if (!this.session || this.session.closed || this.session.destroyed) {
250+
if (!this.isSessionReady || !this.session || this.session.closed || this.session.destroyed) {
248251
try {
249252
await this.connect();
250253
} catch (error) {
@@ -378,27 +381,31 @@ module.exports = function (dependencies) {
378381
this.config
379382
));
380383

381-
if (this.logger.enabled) {
382-
this.session.on('connect', () => {
384+
this.session.on('connect', () => {
385+
this.isSessionReady = true;
386+
if (this.logger.enabled) {
383387
this.logger('Session connected');
384-
});
385-
}
388+
}
389+
});
386390

387391
this.session.on('close', () => {
392+
this.isSessionReady = false;
388393
if (this.errorLogger.enabled) {
389394
this.errorLogger('Session closed');
390395
}
391396
this.destroySession(session);
392397
});
393398

394399
this.session.on('error', error => {
400+
this.isSessionReady = false;
395401
if (this.errorLogger.enabled) {
396402
this.errorLogger(`Session error: ${error}`);
397403
}
398404
this.closeAndDestroySession(session);
399405
});
400406

401407
this.session.on('goaway', (errorCode, lastStreamId, opaqueData) => {
408+
this.isSessionReady = false;
402409
if (this.errorLogger.enabled) {
403410
this.errorLogger(
404411
`GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`
@@ -408,6 +415,7 @@ module.exports = function (dependencies) {
408415
});
409416

410417
this.session.on('frameError', (frameType, errorCode, streamId) => {
418+
this.isSessionReady = false;
411419
// This is a frame error not associate with any request(stream).
412420
if (this.errorLogger.enabled) {
413421
this.errorLogger(
@@ -457,34 +465,39 @@ module.exports = function (dependencies) {
457465
config
458466
));
459467

460-
if (this.logger.enabled) {
461-
this.manageChannelsSession.on('connect', () => {
468+
this.manageChannelsSession.on('connect', () => {
469+
this.isManageChannelSessionReady = true;
470+
if (this.logger.enabled) {
462471
this.logger('ManageChannelsSession connected');
463-
});
464-
}
472+
}
473+
});
465474

466475
this.manageChannelsSession.on('close', () => {
476+
this.isManageChannelSessionReady = false;
467477
if (this.errorLogger.enabled) {
468478
this.errorLogger('ManageChannelsSession closed');
469479
}
470480
this.destroySession(session);
471481
});
472482

473483
this.manageChannelsSession.on('socketError', error => {
484+
this.isManageChannelSessionReady = false;
474485
if (this.errorLogger.enabled) {
475486
this.errorLogger(`ManageChannelsSession Socket error: ${error}`);
476487
}
477488
this.closeAndDestroySession(session);
478489
});
479490

480491
this.manageChannelsSession.on('error', error => {
492+
this.isManageChannelSessionReady = false;
481493
if (this.errorLogger.enabled) {
482494
this.errorLogger(`ManageChannelsSession error: ${error}`);
483495
}
484496
this.closeAndDestroySession(session);
485497
});
486498

487499
this.manageChannelsSession.on('goaway', (errorCode, lastStreamId, opaqueData) => {
500+
this.isManageChannelSessionReady = false;
488501
if (this.errorLogger.enabled) {
489502
this.errorLogger(
490503
`ManageChannelsSession GOAWAY received: (errorCode ${errorCode}, lastStreamId: ${lastStreamId}, opaqueData: ${opaqueData})`
@@ -494,6 +507,7 @@ module.exports = function (dependencies) {
494507
});
495508

496509
this.manageChannelsSession.on('frameError', (frameType, errorCode, streamId) => {
510+
this.isManageChannelSessionReady = false;
497511
// This is a frame error not associate with any request(stream).
498512
if (this.errorLogger.enabled) {
499513
this.errorLogger(

0 commit comments

Comments
 (0)