GramJs: Avoid duplicated reconnects
This commit is contained in:
parent
df97f085e1
commit
342ca92553
@ -144,7 +144,7 @@ class TelegramClient {
|
|||||||
this._exportedSenderReleaseTimeouts = {};
|
this._exportedSenderReleaseTimeouts = {};
|
||||||
this._additionalDcsDisabled = args.additionalDcsDisabled;
|
this._additionalDcsDisabled = args.additionalDcsDisabled;
|
||||||
this._loopStarted = false;
|
this._loopStarted = false;
|
||||||
this._reconnecting = false;
|
this._isSwitchingDc = false;
|
||||||
this._destroyed = false;
|
this._destroyed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +176,7 @@ class TelegramClient {
|
|||||||
// set defaults vars
|
// set defaults vars
|
||||||
this._sender.userDisconnected = false;
|
this._sender.userDisconnected = false;
|
||||||
this._sender._user_connected = false;
|
this._sender._user_connected = false;
|
||||||
this._sender._reconnecting = false;
|
this._sender.isReconnecting = false;
|
||||||
this._sender._disconnected = true;
|
this._sender._disconnected = true;
|
||||||
|
|
||||||
const connection = new this._connection(
|
const connection = new this._connection(
|
||||||
@ -202,7 +202,7 @@ class TelegramClient {
|
|||||||
this._updateLoop();
|
this._updateLoop();
|
||||||
this._loopStarted = true;
|
this._loopStarted = true;
|
||||||
}
|
}
|
||||||
this._reconnecting = false;
|
this._isSwitchingDc = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _initSession() {
|
async _initSession() {
|
||||||
@ -217,7 +217,7 @@ class TelegramClient {
|
|||||||
async _updateLoop() {
|
async _updateLoop() {
|
||||||
while (!this._destroyed) {
|
while (!this._destroyed) {
|
||||||
await Helpers.sleep(PING_INTERVAL);
|
await Helpers.sleep(PING_INTERVAL);
|
||||||
if (this._reconnecting) {
|
if (this._sender.isReconnecting || this._isSwitchingDc) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,7 +231,8 @@ class TelegramClient {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.warn(err);
|
console.warn(err);
|
||||||
if (this._reconnecting) {
|
|
||||||
|
if (this._sender.isReconnecting || this._isSwitchingDc) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,7 +305,7 @@ class TelegramClient {
|
|||||||
// so it's not valid anymore. Set to None to force recreating it.
|
// so it's not valid anymore. Set to None to force recreating it.
|
||||||
await this._sender.authKey.setKey(undefined);
|
await this._sender.authKey.setKey(undefined);
|
||||||
this.session.setAuthKey(undefined);
|
this.session.setAuthKey(undefined);
|
||||||
this._reconnecting = true;
|
this._isSwitchingDc = true;
|
||||||
await this.disconnect();
|
await this.disconnect();
|
||||||
return this.connect();
|
return this.connect();
|
||||||
}
|
}
|
||||||
@ -1063,7 +1064,7 @@ async function attempts(cb, times, pause) {
|
|||||||
for (let i = 0; i < times; i++) {
|
for (let i = 0; i < times; i++) {
|
||||||
try {
|
try {
|
||||||
// We need to `return await` here so it can be caught locally
|
// 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();
|
return await cb();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (i === times - 1) {
|
if (i === times - 1) {
|
||||||
|
|||||||
@ -100,7 +100,7 @@ class MTProtoSender {
|
|||||||
* pending futures should be cancelled.
|
* pending futures should be cancelled.
|
||||||
*/
|
*/
|
||||||
this._user_connected = false;
|
this._user_connected = false;
|
||||||
this._reconnecting = false;
|
this.isReconnecting = false;
|
||||||
this._disconnected = true;
|
this._disconnected = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -297,7 +297,7 @@ class MTProtoSender {
|
|||||||
this._log.debug('Already have an auth key ...');
|
this._log.debug('Already have an auth key ...');
|
||||||
}
|
}
|
||||||
this._user_connected = true;
|
this._user_connected = true;
|
||||||
this._reconnecting = false;
|
this.isReconnecting = false;
|
||||||
|
|
||||||
this._log.debug('Starting send loop');
|
this._log.debug('Starting send loop');
|
||||||
this._send_loop_handle = this._sendLoop();
|
this._send_loop_handle = this._sendLoop();
|
||||||
@ -338,7 +338,7 @@ class MTProtoSender {
|
|||||||
async _sendLoop() {
|
async _sendLoop() {
|
||||||
this._send_queue = new MessagePacker(this._state, this._log);
|
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) {
|
if (this._pending_ack.size) {
|
||||||
const ack = new RequestState(new MsgsAck({ msgIds: Array(...this._pending_ack) }));
|
const ack = new RequestState(new MsgsAck({ msgIds: Array(...this._pending_ack) }));
|
||||||
this._send_queue.append(ack);
|
this._send_queue.append(ack);
|
||||||
@ -348,13 +348,13 @@ class MTProtoSender {
|
|||||||
}
|
}
|
||||||
this._pending_ack.clear();
|
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?
|
// TODO Wait for the connection send queue to be empty?
|
||||||
// This means that while it's not empty we can wait for
|
// This means that while it's not empty we can wait for
|
||||||
// more messages to be added to the send queue.
|
// more messages to be added to the send queue.
|
||||||
const res = await this._send_queue.get();
|
const res = await this._send_queue.get();
|
||||||
|
|
||||||
if (this._reconnecting) {
|
if (this.isReconnecting) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,7 +395,7 @@ class MTProtoSender {
|
|||||||
let body;
|
let body;
|
||||||
let message;
|
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...');
|
||||||
this._log.debug('Receiving items from the network...');
|
this._log.debug('Receiving items from the network...');
|
||||||
try {
|
try {
|
||||||
@ -842,8 +842,8 @@ class MTProtoSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reconnect() {
|
reconnect() {
|
||||||
if (this._user_connected && !this._reconnecting) {
|
if (this._user_connected && !this.isReconnecting) {
|
||||||
this._reconnecting = true;
|
this.isReconnecting = true;
|
||||||
// TODO Should we set this?
|
// TODO Should we set this?
|
||||||
// this._user_connected = false
|
// this._user_connected = false
|
||||||
// we want to wait a second between each reconnect try to not flood the server with reconnects
|
// 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);
|
await this.connect(newConnection, true);
|
||||||
|
|
||||||
this._reconnecting = false;
|
this.isReconnecting = false;
|
||||||
// uncomment this if you want to resend
|
// uncomment this if you want to resend
|
||||||
// this._send_queue.extend(Object.values(this._pending_state))
|
// this._send_queue.extend(Object.values(this._pending_state))
|
||||||
for (const state of Object.values(this._pending_state)) {
|
for (const state of Object.values(this._pending_state)) {
|
||||||
|
|||||||
@ -153,6 +153,10 @@ function onUpdateConnectionState(update: ApiUpdateConnectionState) {
|
|||||||
const { connectionState } = update;
|
const { connectionState } = update;
|
||||||
const global = getGlobal();
|
const global = getGlobal();
|
||||||
|
|
||||||
|
if (connectionState === global.connectionState) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setGlobal({
|
setGlobal({
|
||||||
...global,
|
...global,
|
||||||
connectionState,
|
connectionState,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user