Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec4618db87 | ||
|
|
30fb5d8b27 |
@@ -356,6 +356,10 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
guard let channel = self.channel else {
|
guard let channel = self.channel else {
|
||||||
throw NatsError.ClientError.internalError("empty channel")
|
throw NatsError.ClientError.internalError("empty channel")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重连成功后重置 ping 计数器,避免累积的失败计数导致立即断开
|
||||||
|
self.outstandingPings.store(0, ordering: .relaxed)
|
||||||
|
|
||||||
// Schedule the task to send a PING periodically
|
// Schedule the task to send a PING periodically
|
||||||
let pingInterval = TimeAmount.nanoseconds(Int64(self.pingInterval * 1_000_000_000))
|
let pingInterval = TimeAmount.nanoseconds(Int64(self.pingInterval * 1_000_000_000))
|
||||||
self.pingTask = channel.eventLoop.scheduleRepeatedTask(
|
self.pingTask = channel.eventLoop.scheduleRepeatedTask(
|
||||||
@@ -987,7 +991,8 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
try result.get()
|
try result.get()
|
||||||
self.fire(.disconnected)
|
self.fire(.disconnected)
|
||||||
} catch {
|
} catch {
|
||||||
logger.error("Error closing connection: \(error)")
|
// 降级为 debug:网络断开时 TLS 无法完成正常关闭握手是预期行为
|
||||||
|
logger.debug("Connection closed with error (will reconnect): \(error)")
|
||||||
}
|
}
|
||||||
// Only start reconnect after disconnect is complete
|
// Only start reconnect after disconnect is complete
|
||||||
self.handleReconnect()
|
self.handleReconnect()
|
||||||
@@ -1060,7 +1065,8 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
// Trigger reconnect to recover
|
// Trigger reconnect to recover
|
||||||
let currentState = state.withLockedValue { $0 }
|
let currentState = state.withLockedValue { $0 }
|
||||||
if currentState == .connected {
|
if currentState == .connected {
|
||||||
logger.error("Write failed: batchBuffer is nil but state is connected, triggering reconnect")
|
// 降级为 debug:这是断网恢复场景的正常状态
|
||||||
|
logger.debug("Write failed: batchBuffer is nil, triggering reconnect")
|
||||||
handleDisconnect()
|
handleDisconnect()
|
||||||
}
|
}
|
||||||
throw NatsError.ClientError.invalidConnection("not connected")
|
throw NatsError.ClientError.invalidConnection("not connected")
|
||||||
@@ -1071,7 +1077,8 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
// Trigger reconnect on write failure - connection may be broken
|
// Trigger reconnect on write failure - connection may be broken
|
||||||
let currentState = state.withLockedValue { $0 }
|
let currentState = state.withLockedValue { $0 }
|
||||||
if currentState == .connected {
|
if currentState == .connected {
|
||||||
logger.error("Write operation failed, triggering reconnect: \(error)")
|
// 降级为 debug:网络断开时写入失败是预期行为
|
||||||
|
logger.debug("Write operation failed, triggering reconnect: \(error)")
|
||||||
handleDisconnect()
|
handleDisconnect()
|
||||||
}
|
}
|
||||||
throw NatsError.ClientError.io(error)
|
throw NatsError.ClientError.io(error)
|
||||||
|
|||||||
Reference in New Issue
Block a user