Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd680abe55 | ||
|
|
b014494819 | ||
|
|
387f6cf273 |
@@ -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() {
|
||||
@@ -937,7 +937,6 @@ class ConnectionHandler: ChannelInboundHandler {
|
||||
if currentState == .disconnected || currentState == .closed {
|
||||
return false // Already in disconnected/closed state
|
||||
}
|
||||
$0 = .disconnected
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -945,6 +944,9 @@ class ConnectionHandler: ChannelInboundHandler {
|
||||
return
|
||||
}
|
||||
|
||||
// Set state to disconnected after check
|
||||
state.withLockedValue { $0 = .disconnected }
|
||||
|
||||
// Clean up pending continuations to prevent leaks
|
||||
if let continuation = serverInfoContinuation.withLockedValue({ cont in
|
||||
let toResume = cont
|
||||
@@ -1054,6 +1056,13 @@ class ConnectionHandler: ChannelInboundHandler {
|
||||
|
||||
func write(operation: ClientOp) async throws {
|
||||
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")
|
||||
}
|
||||
do {
|
||||
|
||||
Reference in New Issue
Block a user