2 Commits
1.0.2 ... 1.0.5

Author SHA1 Message Date
wenzuhuai
cd680abe55 fix: 修复 errorCaught 导致的频繁重连问题
- 移除 errorCaught 中重复的 handleDisconnect 调用
- 将 uncleanShutdown 日志降级为 debug 级别
- 避免双重触发导致的断开重连循环
2026-01-23 09:06:50 +08:00
wenzuhuai
b014494819 fix: write操作在batchBuffer为nil时也触发重连
- 解决"假连接"边界情况:状态为connected但batchBuffer为nil
- 确保发送消息失败后能自动恢复连接
2026-01-22 09:52:52 +08:00

View File

@@ -921,14 +921,14 @@ class ConnectionHandler: ChannelInboundHandler {
if let natsErr = error as? NatsErrorProtocol { if let natsErr = error as? NatsErrorProtocol {
self.fire(.error(natsErr)) self.fire(.error(natsErr))
} else { } 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 // handleDisconnect
let currentState = state.withLockedValue { $0 } // context.close() channelInactive
if currentState != .closed && currentState != .disconnected { //
handleDisconnect()
}
} }
func handleDisconnect() { func handleDisconnect() {
@@ -1056,6 +1056,13 @@ class ConnectionHandler: ChannelInboundHandler {
func write(operation: ClientOp) async throws { func write(operation: ClientOp) async throws {
guard let buffer = self.batchBuffer else { guard let buffer = self.batchBuffer else {
// If state is connected but batchBuffer is nil, this is a "fake connection" state
// Trigger reconnect to recover
let currentState = state.withLockedValue { $0 }
if currentState == .connected {
logger.error("Write failed: batchBuffer is nil but state is connected, triggering reconnect")
handleDisconnect()
}
throw NatsError.ClientError.invalidConnection("not connected") throw NatsError.ClientError.invalidConnection("not connected")
} }
do { do {