From 91610520331feb32757cf93ee986fed1eb1dc0ff Mon Sep 17 00:00:00 2001 From: wenzuhuai Date: Wed, 28 Jan 2026 13:56:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E7=9A=84=E8=AF=A6=E7=BB=86=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - handleReconnect 中的日志从 debug 改为 info/warning - 添加重连尝试次数和 reconnectAttempts 计数器的显示 - 添加等待时间日志 - 便于诊断重连失败的原因 --- Sources/Nats/NatsConnection.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Nats/NatsConnection.swift b/Sources/Nats/NatsConnection.swift index 5ad73f9..f36fdf1 100644 --- a/Sources/Nats/NatsConnection.swift +++ b/Sources/Nats/NatsConnection.swift @@ -1011,21 +1011,28 @@ class ConnectionHandler: ChannelInboundHandler { oldTask.cancel() } + logger.info("🔄 Starting reconnection process...") + reconnectTask = Task { var connected = false + var attempt = 0 while !Task.isCancelled && (maxReconnects == nil || self.reconnectAttempts < maxReconnects!) { + attempt += 1 + logger.info("🔄 Reconnect attempt \(attempt), total reconnectAttempts: \(self.reconnectAttempts)") do { try await self.connect() connected = true + logger.info("✅ Reconnection successful after \(attempt) attempts") break // Successfully connected } catch is CancellationError { - logger.debug("Reconnect task cancelled") + logger.info("⚠️ Reconnect task cancelled") return } catch { - logger.debug("Could not reconnect: \(error)") + logger.warning("⚠️ Reconnect attempt \(attempt) failed: \(error)") if !Task.isCancelled { + logger.info("⏳ Waiting \(Double(self.reconnectWait) / 1_000_000_000)s before next attempt...") try? await Task.sleep(nanoseconds: self.reconnectWait) } } @@ -1033,13 +1040,13 @@ class ConnectionHandler: ChannelInboundHandler { // Early return if cancelled if Task.isCancelled { - logger.debug("Reconnect task cancelled after connection attempts") + logger.info("⚠️ Reconnect task cancelled after \(attempt) connection attempts") return } // If we got here without connecting and weren't cancelled, we hit max reconnects if !connected { - logger.error("Could not reconnect; maxReconnects exceeded") + logger.error("❌ Could not reconnect; maxReconnects exceeded (\(self.maxReconnects ?? -1))") try? await self.close() return }