fix: write操作在batchBuffer为nil时也触发重连

- 解决"假连接"边界情况:状态为connected但batchBuffer为nil
- 确保发送消息失败后能自动恢复连接
This commit is contained in:
wenzuhuai
2026-01-22 09:52:52 +08:00
parent 387f6cf273
commit b014494819

View File

@@ -1056,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 {