fix: 优化连接稳定性 - ping阈值从>2降为>1缩短断连检测窗口 - 单节点连接增加8秒超时保护防止TCP超时堆积

This commit is contained in:
wenzuhuai
2026-05-14 20:45:53 +08:00
parent 59800438b3
commit d4dfd627a3

View File

@@ -305,12 +305,31 @@ class ConnectionHandler: ChannelInboundHandler {
}
do {
try await connectToServer(s: s)
// 8
// iOS TCP 30-75/
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await self.connectToServer(s: s)
}
group.addTask {
try await Task.sleep(nanoseconds: 8_000_000_000)
throw NatsError.ConnectError.timeout
}
do {
try await group.next()!
} catch {
group.cancelAll()
throw error
}
group.cancelAll()
}
} catch is CancellationError {
throw CancellationError()
} catch let error as NatsError.ConnectError {
if case .invalidConfig(_) = error {
throw error
}
logger.debug("error connecting to server: \(error)")
logger.debug("error connecting to server (8s timeout): \(error)")
lastErr = error
continue
} catch {
@@ -847,7 +866,9 @@ class ConnectionHandler: ChannelInboundHandler {
internal func sendPing(_ rttCommand: RttCommand? = nil) async {
let pingsOut = self.outstandingPings.wrappingIncrementThenLoad(
ordering: AtomicUpdateOrdering.relaxed)
if pingsOut > 2 {
if pingsOut > 1 {
// > 2 > 1
// 2ping(~20)
handleDisconnect()
return
}