fix: improved reconnecting
Some checks failed
Tests / test-bun (push) Successful in 1m59s
Tests / test-deno (push) Successful in 2m1s
Tests / test-node (node22) (push) Successful in 2m8s
Tests / test-node (node20) (push) Successful in 2m12s
Tests / test-node (node18) (push) Successful in 2m16s
Tests / test-web (chromium) (push) Successful in 2m11s
Tests / test-web (firefox) (push) Successful in 2m20s
Build and deploy typedoc / build (push) Successful in 6m7s
Tests / lint (push) Successful in 6m55s
Tests / e2e (push) Successful in 7m0s
Tests / e2e-deno (push) Has been cancelled

This commit is contained in:
alina 🌸 2025-01-29 21:16:17 +03:00
parent 6c9f4c897d
commit 3a42c023bf
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 19 additions and 6 deletions

View file

@ -5,7 +5,7 @@ import type { IPacketCodec, ITelegramConnection, TelegramTransport } from './tra
import { FramedReader, FramedWriter } from '@fuman/io'
import { PersistentConnection as FumanPersistentConnection } from '@fuman/net'
import { ConnectionClosedError, PersistentConnection as FumanPersistentConnection } from '@fuman/net'
import { Emitter, timers } from '@fuman/utils'
export interface PersistentConnectionParams {
@ -140,7 +140,7 @@ export abstract class PersistentConnection {
}
}
private async _onClose() {
private _onClose(): void {
this.log.debug('connection closed')
this._updateLogPrefix()
@ -149,9 +149,12 @@ export abstract class PersistentConnection {
this.onClosed()
}
private async _onError(err: Error) {
private _onError(err: Error) {
this._updateLogPrefix()
this.handleError(err)
if (!(err instanceof ConnectionClosedError)) {
this.handleError(err)
}
return 'reconnect' as const
}
@ -231,7 +234,17 @@ export abstract class PersistentConnection {
if (this._writer) {
this._rescheduleInactivity()
await this._writer.write(data)
try {
await this._writer.write(data)
} catch (e: unknown) {
if (e instanceof ConnectionClosedError) {
this.log.debug('connection closed while sending, reconnecting')
this._fuman.reconnect(true)
this._sendOnceConnected.push(data)
} else {
throw e
}
}
} else {
this._sendOnceConnected.push(data)
}

View file

@ -10,8 +10,8 @@ import { Deferred, Emitter, timers, u8 } from '@fuman/utils'
import { tl } from '@mtcute/tl'
import { TlBinaryReader, TlBinaryWriter, TlSerializationCounter } from '@mtcute/tl-runtime'
import Long from 'long'
import { MtArgumentError, MtcuteError, MtTimeoutError } from '../types/index.js'
import { MtArgumentError, MtcuteError, MtTimeoutError } from '../types/index.js'
import { createAesIgeForMessageOld } from '../utils/crypto/mtproto.js'
import {
EarlyTimer,