fix(node): reconnect stale gateway ws
This commit is contained in:
parent
9688454a30
commit
eabf9a09dd
@ -211,7 +211,22 @@ public actor GatewayChannelActor {
|
||||
}
|
||||
|
||||
public func connect() async throws {
|
||||
if self.connected, self.task?.state == .running { return }
|
||||
if self.connected, self.task?.state == .running {
|
||||
let staleStatus = self.staleConnectionStatus()
|
||||
if staleStatus.isStale {
|
||||
if let deltaMs = staleStatus.deltaMs {
|
||||
self.logger.error(
|
||||
"gateway ws stale; reconnecting deltaMs=\(Int(deltaMs)) thresholdMs=\(Int(staleStatus.thresholdMs))")
|
||||
} else {
|
||||
self.logger.error(
|
||||
"gateway ws stale; reconnecting lastTick=missing thresholdMs=\(Int(staleStatus.thresholdMs))")
|
||||
}
|
||||
self.connected = false
|
||||
self.task?.cancel(with: .goingAway, reason: nil)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
if self.isConnecting {
|
||||
try await withCheckedThrowingContinuation { cont in
|
||||
self.connectWaiters.append(cont)
|
||||
@ -328,8 +343,8 @@ public actor GatewayChannelActor {
|
||||
} else if let password = self.password {
|
||||
params["auth"] = ProtoAnyCodable(["password": ProtoAnyCodable(password)])
|
||||
}
|
||||
let signedAtMs = Int(Date().timeIntervalSince1970 * 1000)
|
||||
let connectNonce = try await self.waitForConnectChallenge()
|
||||
let signedAtMs = Int(Date().timeIntervalSince1970 * 1000)
|
||||
let scopesValue = scopes.joined(separator: ",")
|
||||
var payloadParts = [
|
||||
connectNonce == nil ? "v1" : "v2",
|
||||
@ -554,6 +569,15 @@ public actor GatewayChannelActor {
|
||||
}
|
||||
}
|
||||
|
||||
private func staleConnectionStatus() -> (isStale: Bool, deltaMs: Double?, thresholdMs: Double) {
|
||||
let thresholdMs = self.tickIntervalMs * 2
|
||||
guard let lastTick else {
|
||||
return (true, nil, thresholdMs)
|
||||
}
|
||||
let deltaMs = Date().timeIntervalSince(lastTick) * 1000
|
||||
return (deltaMs > thresholdMs, deltaMs, thresholdMs)
|
||||
}
|
||||
|
||||
private func scheduleReconnect() async {
|
||||
guard self.shouldReconnect else { return }
|
||||
let delay = self.backoffMs / 1000
|
||||
|
||||
@ -107,7 +107,6 @@ public actor GatewayNodeSession {
|
||||
|
||||
do {
|
||||
try await channel.connect()
|
||||
await onConnected()
|
||||
} catch {
|
||||
await onDisconnected(error.localizedDescription)
|
||||
throw error
|
||||
|
||||
Loading…
Reference in New Issue
Block a user