Skip to content

Commit

Permalink
Add reconnect retries option. (default to infinity)
Browse files Browse the repository at this point in the history
  • Loading branch information
painor committed Oct 30, 2024
1 parent 3d77d52 commit 4dac9e9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gramjs/Version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = "2.26.1";
export const version = "2.26.3";
1 change: 1 addition & 0 deletions gramjs/client/TelegramClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ export class TelegramClient extends TelegramBaseClient {
securityChecks: this._securityChecks,
autoReconnectCallback: this._handleReconnect.bind(this),
_exportedSenderPromises: this._exportedSenderPromises,
reconnectRetries: this._reconnectRetries,
});
}

Expand Down
11 changes: 10 additions & 1 deletion gramjs/client/telegramBaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ export interface TelegramClientParams {
*/
requestRetries?: number;
/**
* How many times the reconnection should retry, either on the initial connection or when Telegram disconnects us.<br/>
* How many times the connection should retry, either on the initial connection or when Telegram disconnects us.<br/>
* May be set to a negative or undefined value for infinite retries, but this is not recommended, since the program can get stuck in an infinite loop.<br/>
* defaults to 5
*/
connectionRetries?: number;
/**
* How many times to reconnect before giving up. This happens after the initial connection is finished<br/>
* defaults to infinity
*/
reconnectRetries?: number;
/**
* Experimental proxy to be used for the connection. (only supports MTProxies)
*/
Expand Down Expand Up @@ -172,6 +177,8 @@ export abstract class TelegramBaseClient {
/** @hidden */
public _connectionRetries: number;
/** @hidden */
public _reconnectRetries: number;
/** @hidden */
public _retryDelay: number;
/** @hidden */
public _timeout: number;
Expand Down Expand Up @@ -270,6 +277,7 @@ export abstract class TelegramBaseClient {
this._requestRetries = clientParams.requestRetries!;
this._downloadRetries = clientParams.downloadRetries!;
this._connectionRetries = clientParams.connectionRetries!;
this._reconnectRetries = clientParams.reconnectRetries!;
this._retryDelay = clientParams.retryDelay || 0;
this._timeout = clientParams.timeout!;
this._autoReconnect = clientParams.autoReconnect!;
Expand Down Expand Up @@ -570,6 +578,7 @@ export abstract class TelegramBaseClient {
client: this as unknown as TelegramClient,
securityChecks: this._securityChecks,
_exportedSenderPromises: this._exportedSenderPromises,
reconnectRetries: this._reconnectRetries,
});
}

Expand Down
6 changes: 6 additions & 0 deletions gramjs/client/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export async function invoke<R extends Api.AnyRequest>(
);
}

if (sender.userDisconnected) {
throw new Error(
"Cannot send requests while disconnected. Please reconnect."
);
}

await client._connectedDeferred.promise;

await request.resolve(client, utils);
Expand Down
18 changes: 18 additions & 0 deletions gramjs/network/MTProtoSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import MsgsAck = Api.MsgsAck;
interface DEFAULT_OPTIONS {
logger: any;
retries: number;
reconnectRetries: number;
delay: number;
autoReconnect: boolean;
connectTimeout: any;
Expand All @@ -59,6 +60,7 @@ interface DEFAULT_OPTIONS {
export class MTProtoSender {
static DEFAULT_OPTIONS = {
logger: null,
reconnectRetries: Infinity,
retries: Infinity,
delay: 2000,
autoReconnect: true,
Expand All @@ -75,6 +77,8 @@ export class MTProtoSender {
private readonly _log: Logger;
private _dcId: number;
private readonly _retries: number;
private _reconnectRetries: number;
private _currentRetries: number;
private readonly _delay: number;
private _connectTimeout: null;
private _autoReconnect: boolean;
Expand Down Expand Up @@ -126,6 +130,8 @@ export class MTProtoSender {
this._log = args.logger;
this._dcId = args.dcId;
this._retries = args.retries;
this._currentRetries = 0;
this._reconnectRetries = args.reconnectRetries;
this._delay = args.delay;
this._autoReconnect = args.autoReconnect;
this._connectTimeout = args.connectTimeout;
Expand Down Expand Up @@ -543,6 +549,16 @@ export class MTProtoSender {
try {
body = await this._connection!.recv();
} catch (e) {
if (this._currentRetries > this._reconnectRetries) {
for (const state of this._pendingState.values()) {
state.reject(
"Maximum reconnection retries reached. Aborting!"
);
}
this.userDisconnected = true;
return;
}

/** when the server disconnects us we want to reconnect */
if (!this.userDisconnected) {
this._log.warn("Connection closed while receiving data");
Expand Down Expand Up @@ -625,6 +641,7 @@ export class MTProtoSender {
}
}
}
this._currentRetries = 0;
}

this._recvLoopHandle = undefined;
Expand Down Expand Up @@ -982,6 +999,7 @@ export class MTProtoSender {
reconnect() {
if (this._userConnected && !this.isReconnecting) {
this.isReconnecting = true;
this._currentRetries++;
if (this._isMainSender) {
this._log.debug("Reconnecting all senders");
for (const promise of this._exportedSenderPromises.values()) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "telegram",
"version": "2.26.2",
"version": "2.26.4",
"description": "NodeJS/Browser MTProto API Telegram client library,",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -70,4 +70,4 @@
"node-localstorage": "^2.2.1",
"socks": "^2.6.2"
}
}
}

0 comments on commit 4dac9e9

Please sign in to comment.