Skip to content

Commit

Permalink
add auth retry count to ChatClient
Browse files Browse the repository at this point in the history
fixes #316
  • Loading branch information
d-fischer committed Dec 25, 2021
1 parent fe443b4 commit c926e32
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/chat/src/ChatClient.ts
Expand Up @@ -167,6 +167,7 @@ export class ChatClient extends IrcClient {
private _authToken?: AccessToken | null;
private _authVerified = false;
private _authRetryTimer?: Iterator<number, never>;
private _authRetryCount = 0;

private readonly _chatLogger: Logger;

Expand Down Expand Up @@ -571,7 +572,7 @@ export class ChatClient extends IrcClient {
* @param channel The channel that a command without sufficient permissions was executed on.
* @param message The message text.
*/
readonly onAuthenticationFailure: EventBinder<[message: string]> = this.registerEvent();
readonly onAuthenticationFailure: EventBinder<[message: string, retryCount: number]> = this.registerEvent();

/**
* Fires when sending a message fails.
Expand Down Expand Up @@ -789,6 +790,8 @@ export class ChatClient extends IrcClient {

this.addInternalListener(this.onRegister, async () => {
this._authVerified = true;
this._authRetryTimer = undefined;
this._authRetryCount = 0;

const resolvedChannels = await resolveConfigValue(config.channels);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
Expand Down Expand Up @@ -1505,11 +1508,13 @@ export class ChatClient extends IrcClient {
content === 'Invalid NICK'
) {
this._authVerified = false;
this.emit(this.onAuthenticationFailure, content);
if (!this._authRetryTimer) {
this._authRetryTimer = ChatClient._getReauthenticateWaitTime();
this._authRetryCount = 0;
}
const secs = this._authRetryTimer.next().value;
const authRetries = ++this._authRetryCount;
this.emit(this.onAuthenticationFailure, content, authRetries);
if (secs !== 0) {
this._chatLogger.info(`Retrying authentication in ${secs} seconds`);
}
Expand Down

0 comments on commit c926e32

Please sign in to comment.