1 Commits

Author SHA1 Message Date
xiaochao
03f8f3fa21 Update coordinator.py 2025-07-04 17:44:56 +08:00
2 changed files with 91 additions and 78 deletions

View File

@@ -69,6 +69,9 @@ class FlynasCoordinator(DataUpdateCoordinator):
async def get_ssh_connection(self):
"""从连接池获取或创建SSH连接"""
# 避免递归调用,改为循环等待
start_time = time.time()
while True:
# 如果连接池中有可用连接且没有超过最大活动命令数
while len(self.ssh_pool) > 0 and self.active_commands < self.max_connections:
conn = self.ssh_pool.pop()
@@ -99,9 +102,13 @@ class FlynasCoordinator(DataUpdateCoordinator):
except Exception as e:
_LOGGER.error("创建SSH连接失败: %s", str(e), exc_info=True)
raise UpdateFailed(f"SSH连接失败: {str(e)}")
else:
# 等待0.1秒后重试,避免递归
await asyncio.sleep(0.1)
return await self.get_ssh_connection()
# 设置超时30秒
if time.time() - start_time > 30:
raise UpdateFailed("获取SSH连接超时")
async def determine_sudo_setting(self, conn):
"""确定是否需要使用sudo权限"""
@@ -179,6 +186,9 @@ class FlynasCoordinator(DataUpdateCoordinator):
async def run_command(self, command: str, retries=2) -> str:
"""使用连接池执行命令"""
conn = None
current_retries = retries
while current_retries >= 0:
try:
conn = await self.get_ssh_connection()
@@ -202,24 +212,27 @@ class FlynasCoordinator(DataUpdateCoordinator):
# 连接可能已损坏,关闭它
await self.close_connection(conn)
conn = None
if retries > 0:
return await self.run_command(command, retries-1)
if current_retries > 0:
current_retries -= 1
continue
else:
raise UpdateFailed(f"Command failed: {command}") from e
except asyncssh.Error as e:
_LOGGER.error("SSH连接错误: %s", str(e))
await self.close_connection(conn)
conn = None
if retries > 0:
return await self.run_command(command, retries-1)
if current_retries > 0:
current_retries -= 1
continue
else:
raise UpdateFailed(f"SSH错误: {str(e)}") from e
except Exception as e:
_LOGGER.error("意外错误: %s", str(e), exc_info=True)
await self.close_connection(conn)
conn = None
if retries > 0:
return await self.run_command(command, retries-1)
if current_retries > 0:
current_retries -= 1
continue
else:
raise UpdateFailed(f"意外错误: {str(e)}") from e
finally:

View File

@@ -1,7 +1,7 @@
{
"domain": "fn_nas",
"name": "飞牛NAS",
"version": "1.3.1",
"version": "1.3.3",
"documentation": "https://github.com/anxms/fn_nas",
"dependencies": [],
"codeowners": ["@anxms"],