From cd680abe55679ee5a23b75ea38d5dbdbd1d7db99 Mon Sep 17 00:00:00 2001 From: wenzuhuai Date: Fri, 23 Jan 2026 09:06:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20errorCaught=20?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E9=A2=91=E7=B9=81=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 errorCaught 中重复的 handleDisconnect 调用 - 将 uncleanShutdown 日志降级为 debug 级别 - 避免双重触发导致的断开重连循环 --- Sources/Nats/NatsConnection.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/Nats/NatsConnection.swift b/Sources/Nats/NatsConnection.swift index 08820a6..11504cc 100644 --- a/Sources/Nats/NatsConnection.swift +++ b/Sources/Nats/NatsConnection.swift @@ -921,14 +921,14 @@ class ConnectionHandler: ChannelInboundHandler { if let natsErr = error as? NatsErrorProtocol { self.fire(.error(natsErr)) } else { - logger.error("unexpected error: \(error)") + // 降级为 debug 级别,避免频繁输出错误日志 + // uncleanShutdown 是常见的 TLS 关闭情况,不需要作为错误处理 + logger.debug("Channel error (will reconnect if needed): \(error)") } - // Unified handling: use handleDisconnect for all non-closed/non-disconnected states - let currentState = state.withLockedValue { $0 } - if currentState != .closed && currentState != .disconnected { - handleDisconnect() - } + // 注意:不在这里调用 handleDisconnect + // context.close() 会触发 channelInactive,由它负责处理断开逻辑 + // 这样可以避免重复处理和过度重连 } func handleDisconnect() {