Ver Fonte

Add reconnect retries option. (default to infinity)

Painor há 6 meses atrás
pai
commit
4dac9e9e97

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "2.26.1";
+export const version = "2.26.3";

+ 1 - 0
gramjs/client/TelegramClient.ts

@@ -1417,6 +1417,7 @@ export class TelegramClient extends TelegramBaseClient {
                 securityChecks: this._securityChecks,
                 autoReconnectCallback: this._handleReconnect.bind(this),
                 _exportedSenderPromises: this._exportedSenderPromises,
+                reconnectRetries: this._reconnectRetries,
             });
         }
 

+ 10 - 1
gramjs/client/telegramBaseClient.ts

@@ -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)
      */
@@ -172,6 +177,8 @@ export abstract class TelegramBaseClient {
     /** @hidden */
     public _connectionRetries: number;
     /** @hidden */
+    public _reconnectRetries: number;
+    /** @hidden */
     public _retryDelay: number;
     /** @hidden */
     public _timeout: number;
@@ -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!;
@@ -570,6 +578,7 @@ export abstract class TelegramBaseClient {
             client: this as unknown as TelegramClient,
             securityChecks: this._securityChecks,
             _exportedSenderPromises: this._exportedSenderPromises,
+            reconnectRetries: this._reconnectRetries,
         });
     }
 

+ 6 - 0
gramjs/client/users.ts

@@ -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);

+ 18 - 0
gramjs/network/MTProtoSender.ts

@@ -41,6 +41,7 @@ import MsgsAck = Api.MsgsAck;
 interface DEFAULT_OPTIONS {
     logger: any;
     retries: number;
+    reconnectRetries: number;
     delay: number;
     autoReconnect: boolean;
     connectTimeout: any;
@@ -59,6 +60,7 @@ interface DEFAULT_OPTIONS {
 export class MTProtoSender {
     static DEFAULT_OPTIONS = {
         logger: null,
+        reconnectRetries: Infinity,
         retries: Infinity,
         delay: 2000,
         autoReconnect: true,
@@ -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;
@@ -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;
@@ -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");
@@ -625,6 +641,7 @@ export class MTProtoSender {
                     }
                 }
             }
+            this._currentRetries = 0;
         }
 
         this._recvLoopHandle = undefined;
@@ -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()) {

+ 2 - 2
package-lock.json

@@ -1,12 +1,12 @@
 {
   "name": "telegram",
-  "version": "2.26.2",
+  "version": "2.26.4",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "telegram",
-      "version": "2.26.2",
+      "version": "2.26.4",
       "license": "MIT",
       "dependencies": {
         "@cryptography/aes": "^0.1.1",

+ 2 - 2
package.json

@@ -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",
@@ -70,4 +70,4 @@
     "node-localstorage": "^2.2.1",
     "socks": "^2.6.2"
   }
-}
+}