Skip to content
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

Fix and Add Logging for StateMachine websocket close #537

Merged
Merged
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions src/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export class KeepAlive extends EventEmitter {
*/
private logger: Logger;

/**
* Flag that indicates whether this object is still monitoring.
*/
public isMonitoring?: Boolean;

/**
* Flag that indicates whether recommend_reconnect event has been emitted and stop() has not been called.
*/
public recommendReconnect?: Boolean;

constructor({
clientPingTimeout = 6000,
serverPongTimeout = 4000,
Expand Down Expand Up @@ -97,6 +107,7 @@ export class KeepAlive extends EventEmitter {
}

this.client = client;
this.isMonitoring = true;
this.client.on('outgoing_message', this.setPingTimer, this);
this.setPingTimer();
}
Expand All @@ -109,6 +120,7 @@ export class KeepAlive extends EventEmitter {
public stop(): void {
this.clearPreviousPingTimer();
this.lastPing = this.client = undefined;
this.recommendReconnect = this.isMonitoring = false;
}

/**
Expand Down Expand Up @@ -174,13 +186,13 @@ export class KeepAlive extends EventEmitter {
if (this.client === undefined) {
throw errorWithCode(new Error('no client found'), ErrorCode.KeepAliveInconsistentState);
}
// signal that this pong is done being handled
this.client.off('slack_event', attemptAcknowledgePong);

// no pong received to acknowledge the last ping within the serverPongTimeout
this.logger.debug('pong timer expired, recommend reconnect');
this.recommendReconnect = true;
this.emit('recommend_reconnect');

// signal that this pong is done being handled
this.client.off('slack_event', attemptAcknowledgePong);
}, this.serverPongTimeout);

this.client.on('slack_event', attemptAcknowledgePong, this);
Expand Down
16 changes: 16 additions & 0 deletions src/RTMClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ export class RTMClient extends EventEmitter {
})
.on('websocket open').transitionTo('handshaking')
.state('handshaking') // a state in which to wait until the 'server hello' event
.on('websocket close')
.transitionTo('reconnecting').withCondition(() => this.autoReconnect)
.withAction((_from, _to, context) => {
this.logger.debug(`reconnecting after unexpected close ${context.eventPayload.reason}
${context.eventPayload.code} with isMonitoring set to ${this.keepAlive.isMonitoring}
and recommendReconnect set to ${this.keepAlive.recommendReconnect}`);
})
.transitionTo('disconnected')
.withAction((_from, _to, context) => {
this.logger.debug(`disconnected after unexpected close ${context.eventPayload.reason}
${context.eventPayload.code} with isMonitoring set to ${this.keepAlive.isMonitoring}
and recommendReconnect set to ${this.keepAlive.recommendReconnect}`);
// this transition circumvents the 'disconnecting' state (since the websocket is already closed),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ the attention to detail in the comments!

// so we need to execute its onExit behavior here.
this.teardownWebsocket();
})
.global()
.onStateEnter((state) => {
this.logger.debug(`transitioning to state: connecting:${state}`);
Expand Down