Skip to content

Commit

Permalink
More connection stability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Jun 7, 2024
1 parent ecfebcc commit 69e87b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 14 additions & 1 deletion client/2_update_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

import { unreachable } from "../0_deps.ts";
import { getLogger, Logger, Queue, second, ZERO_CHANNEL_ID } from "../1_utilities.ts";
import { getLogger, Logger, Mutex, Queue, second, ZERO_CHANNEL_ID } from "../1_utilities.ts";
import { Api, as, inputPeerToPeer, is, isOfEnum, isOneOf, peerToChatId, ReadObject } from "../2_tl.ts";
import { PersistentTimestampInvalid } from "../3_errors.ts";
import { CHANNEL_DIFFERENCE_LIMIT_BOT, CHANNEL_DIFFERENCE_LIMIT_USER } from "../4_constants.ts";
Expand Down Expand Up @@ -634,10 +634,21 @@ export class UpdateManager {
}
return localState;
}

#recoveringUpdateGap = false;
#recoverUpdateGapMutex = new Mutex();
async recoverUpdateGap(source: string) {
if (this.#c.cdn) {
return;
}
const wasRecoveringUpdateGap = this.#recoveringUpdateGap;
const unlock = await this.#recoverUpdateGapMutex.lock();
if (wasRecoveringUpdateGap) {
this.#LrecoverUpdateGap.debug(`update gap was just recovered [${source}]`);
unlock();
return;
}
this.#recoveringUpdateGap = true;
this.#LrecoverUpdateGap.debug(`recovering from update gap [${source}]`);

this.#c.setConnectionState("updating");
Expand Down Expand Up @@ -695,7 +706,9 @@ export class UpdateManager {
} catch (err) {
this.#LrecoverUpdateGap.error(err);
} finally {
unlock();
this.#c.resetConnectionState();
this.#recoveringUpdateGap = false;
}
}

Expand Down
6 changes: 4 additions & 2 deletions connection/1_connection_tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

import { concat, iterateReader } from "../0_deps.ts";
import { ConnectionError } from "../0_errors.ts";
import { getLogger, Mutex } from "../1_utilities.ts";
import { Connection } from "./0_connection.ts";

Expand Down Expand Up @@ -49,7 +50,7 @@ export class ConnectionTCP implements Connection {

#assertConnected() {
if (!this.connected) {
throw new Error("Connection not open");
throw new ConnectionError("Connection not open");
}
}

Expand Down Expand Up @@ -95,7 +96,7 @@ export class ConnectionTCP implements Connection {

#rejectRead() {
if (this.#nextResolve != null) {
this.#nextResolve[1].reject(new Error("Connection was closed"));
this.#nextResolve[1].reject(new ConnectionError("Connection was closed"));
this.#nextResolve = null;
}
}
Expand Down Expand Up @@ -133,6 +134,7 @@ export class ConnectionTCP implements Connection {
}
if (!this.connected) {
this.stateChangeHandler?.(false);
throw new ConnectionError("Connection was closed");
}
throw err;
}
Expand Down

0 comments on commit 69e87b5

Please sign in to comment.