From 342ca92553abea3db22af5b540cabcee0ec9eb6c Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 24 Jan 2022 04:41:37 +0100 Subject: [PATCH] GramJs: Avoid duplicated reconnects --- src/lib/gramjs/client/TelegramClient.js | 15 ++++++++------- src/lib/gramjs/network/MTProtoSender.js | 18 +++++++++--------- src/modules/actions/apiUpdaters/initial.ts | 4 ++++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/lib/gramjs/client/TelegramClient.js b/src/lib/gramjs/client/TelegramClient.js index 21708081..8378d943 100644 --- a/src/lib/gramjs/client/TelegramClient.js +++ b/src/lib/gramjs/client/TelegramClient.js @@ -144,7 +144,7 @@ class TelegramClient { this._exportedSenderReleaseTimeouts = {}; this._additionalDcsDisabled = args.additionalDcsDisabled; this._loopStarted = false; - this._reconnecting = false; + this._isSwitchingDc = false; this._destroyed = false; } @@ -176,7 +176,7 @@ class TelegramClient { // set defaults vars this._sender.userDisconnected = false; this._sender._user_connected = false; - this._sender._reconnecting = false; + this._sender.isReconnecting = false; this._sender._disconnected = true; const connection = new this._connection( @@ -202,7 +202,7 @@ class TelegramClient { this._updateLoop(); this._loopStarted = true; } - this._reconnecting = false; + this._isSwitchingDc = false; } async _initSession() { @@ -217,7 +217,7 @@ class TelegramClient { async _updateLoop() { while (!this._destroyed) { await Helpers.sleep(PING_INTERVAL); - if (this._reconnecting) { + if (this._sender.isReconnecting || this._isSwitchingDc) { continue; } @@ -231,7 +231,8 @@ class TelegramClient { } catch (err) { // eslint-disable-next-line no-console console.warn(err); - if (this._reconnecting) { + + if (this._sender.isReconnecting || this._isSwitchingDc) { continue; } @@ -304,7 +305,7 @@ class TelegramClient { // so it's not valid anymore. Set to None to force recreating it. await this._sender.authKey.setKey(undefined); this.session.setAuthKey(undefined); - this._reconnecting = true; + this._isSwitchingDc = true; await this.disconnect(); return this.connect(); } @@ -1063,7 +1064,7 @@ async function attempts(cb, times, pause) { for (let i = 0; i < times; i++) { try { // We need to `return await` here so it can be caught locally - // eslint-disable-next-line no-return-await + // eslint-disable-next-line @typescript-eslint/return-await return await cb(); } catch (err) { if (i === times - 1) { diff --git a/src/lib/gramjs/network/MTProtoSender.js b/src/lib/gramjs/network/MTProtoSender.js index 636dd4f1..e1275c04 100644 --- a/src/lib/gramjs/network/MTProtoSender.js +++ b/src/lib/gramjs/network/MTProtoSender.js @@ -100,7 +100,7 @@ class MTProtoSender { * pending futures should be cancelled. */ this._user_connected = false; - this._reconnecting = false; + this.isReconnecting = false; this._disconnected = true; /** @@ -297,7 +297,7 @@ class MTProtoSender { this._log.debug('Already have an auth key ...'); } this._user_connected = true; - this._reconnecting = false; + this.isReconnecting = false; this._log.debug('Starting send loop'); this._send_loop_handle = this._sendLoop(); @@ -338,7 +338,7 @@ class MTProtoSender { async _sendLoop() { this._send_queue = new MessagePacker(this._state, this._log); - while (this._user_connected && !this._reconnecting) { + while (this._user_connected && !this.isReconnecting) { if (this._pending_ack.size) { const ack = new RequestState(new MsgsAck({ msgIds: Array(...this._pending_ack) })); this._send_queue.append(ack); @@ -348,13 +348,13 @@ class MTProtoSender { } this._pending_ack.clear(); } - this._log.debug(`Waiting for messages to send...${this._reconnecting}`); + this._log.debug(`Waiting for messages to send...${this.isReconnecting}`); // TODO Wait for the connection send queue to be empty? // This means that while it's not empty we can wait for // more messages to be added to the send queue. const res = await this._send_queue.get(); - if (this._reconnecting) { + if (this.isReconnecting) { return; } @@ -395,7 +395,7 @@ class MTProtoSender { let body; let message; - while (this._user_connected && !this._reconnecting) { + while (this._user_connected && !this.isReconnecting) { // this._log.debug('Receiving items from the network...'); this._log.debug('Receiving items from the network...'); try { @@ -842,8 +842,8 @@ class MTProtoSender { } reconnect() { - if (this._user_connected && !this._reconnecting) { - this._reconnecting = true; + if (this._user_connected && !this.isReconnecting) { + this.isReconnecting = true; // TODO Should we set this? // this._user_connected = false // we want to wait a second between each reconnect try to not flood the server with reconnects @@ -877,7 +877,7 @@ class MTProtoSender { ); await this.connect(newConnection, true); - this._reconnecting = false; + this.isReconnecting = false; // uncomment this if you want to resend // this._send_queue.extend(Object.values(this._pending_state)) for (const state of Object.values(this._pending_state)) { diff --git a/src/modules/actions/apiUpdaters/initial.ts b/src/modules/actions/apiUpdaters/initial.ts index d87794b6..13fe7a1c 100644 --- a/src/modules/actions/apiUpdaters/initial.ts +++ b/src/modules/actions/apiUpdaters/initial.ts @@ -153,6 +153,10 @@ function onUpdateConnectionState(update: ApiUpdateConnectionState) { const { connectionState } = update; const global = getGlobal(); + if (connectionState === global.connectionState) { + return; + } + setGlobal({ ...global, connectionState,