UBERF-8899: Fix Reconnect performance (#7597)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev
2025-01-07 23:21:27 +07:00
committed by GitHub
parent 1d836b73ab
commit a2cbc2a5fb
12 changed files with 106 additions and 73 deletions
+11 -2
View File
@@ -108,10 +108,12 @@ class Connection implements ClientConnection {
private helloRecieved: boolean = false
onConnect?: (event: ClientConnectEvent, data: any) => Promise<void>
onConnect?: (event: ClientConnectEvent, lastTx: string | undefined, data: any) => Promise<void>
rpcHandler = new RPCHandler()
lastHash?: string
constructor (
private readonly ctx: MeasureContext,
private readonly url: string,
@@ -144,6 +146,11 @@ class Connection implements ClientConnection {
this.scheduleOpen(this.ctx, false)
}
async getLastHash (ctx: MeasureContext): Promise<string | undefined> {
await this.waitOpenConnection(ctx)
return this.lastHash
}
private schedulePing (socketId: number): void {
clearInterval(this.interval)
this.pingResponse = Date.now()
@@ -272,7 +279,7 @@ class Connection implements ClientConnection {
if (resp.id === -1) {
this.delay = 0
if (resp.result?.state === 'upgrading') {
void this.onConnect?.(ClientConnectEvent.Maintenance, resp.result.stats)
void this.onConnect?.(ClientConnectEvent.Maintenance, undefined, resp.result.stats)
this.upgrading = true
this.delay = 3
return
@@ -286,6 +293,7 @@ class Connection implements ClientConnection {
// We need to clear dial timer, since we recieve hello response.
clearTimeout(this.dialTimer)
this.dialTimer = null
this.lastHash = (resp as HelloResponse).lastHash
const serverVersion = helloResp.serverVersion
console.log('Connected to server:', serverVersion)
@@ -315,6 +323,7 @@ class Connection implements ClientConnection {
void this.onConnect?.(
(resp as HelloResponse).reconnect === true ? ClientConnectEvent.Reconnected : ClientConnectEvent.Connected,
(resp as HelloResponse).lastTx,
this.sessionId
)
this.schedulePing(socketId)
+2 -2
View File
@@ -118,10 +118,10 @@ export default async () => {
reject(new Error(`Connection timeout, and no connection established to ${endpoint}`))
}
}, connectTimeout)
newOpt.onConnect = async (event, data) => {
newOpt.onConnect = async (event, lastTx, data) => {
// Any event is fine, it means server is alive.
clearTimeout(connectTO)
await opt?.onConnect?.(event, data)
await opt?.onConnect?.(event, lastTx, data)
resolve()
}
})