diff --git a/backend/app/api/sessions.py b/backend/app/api/sessions.py index e156fa4..f3872a3 100644 --- a/backend/app/api/sessions.py +++ b/backend/app/api/sessions.py @@ -388,7 +388,8 @@ async def retry_enhanced_session( pty = session_manager.deserialize_pty(record.pty_info) last_error: Exception | None = None - for attempt in range(1, 4): + retry_schedule = [2, 4, 8, 16, 32] + for attempt in range(1, len(retry_schedule) + 1): try: await session_manager.close_session(session_id) managed = await session_manager.create_session( @@ -412,7 +413,8 @@ async def retry_enhanced_session( return response except Exception as exc: last_error = exc - await asyncio.sleep(5) + if attempt < len(retry_schedule): + await asyncio.sleep(retry_schedule[attempt - 1]) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(last_error) if last_error else "重试失败") diff --git a/backend/app/core/state.py b/backend/app/core/state.py index 8765dec..67967b1 100644 --- a/backend/app/core/state.py +++ b/backend/app/core/state.py @@ -35,10 +35,11 @@ def _serialize_session_status(record: SessionRecord) -> str: def _required_elapsed_seconds_for_next_retry(retry_cycle_count: int) -> int: - # Retry spacing: 5s, 10s, 15s... + # Retry spacing (seconds): 2, 4, 8, 16, 32 # retry_cycle_count is the number of attempts already made in current cycle. - next_attempt = retry_cycle_count + 1 - return 5 * next_attempt * (next_attempt + 1) // 2 + schedule = [2, 4, 8, 16, 32] + index = min(retry_cycle_count, len(schedule) - 1) + return schedule[index] def build_state() -> tuple[ @@ -95,7 +96,7 @@ def build_state() -> tuple[ SessionRecord.allow_auto_retry.is_(True), SessionRecord.enhanced_fingerprint.is_not(None), SessionRecord.tmux_binary_path.is_not(None), - SessionRecord.retry_cycle_count < 3, + SessionRecord.retry_cycle_count < 5, ) ).scalars().all() diff --git a/frontend/src/pages/sessions/SessionsDesktop.tsx b/frontend/src/pages/sessions/SessionsDesktop.tsx index 5374f8f..43216b0 100644 --- a/frontend/src/pages/sessions/SessionsDesktop.tsx +++ b/frontend/src/pages/sessions/SessionsDesktop.tsx @@ -165,7 +165,7 @@ export function SessionsDesktop({ state }: SessionsDesktopProps) { ) : null} {session.enhanced_enabled && session.status !== "active" && session.allow_auto_retry !== false ? (

- {state.t("本轮重试", "Retry cycle")}: {session.retry_cycle_count ?? 0}/3 + {state.t("本轮重试", "Retry cycle")}: {session.retry_cycle_count ?? 0}/5

) : null} diff --git a/frontend/src/pages/sessions/useSessionsState.ts b/frontend/src/pages/sessions/useSessionsState.ts index 7836a1c..90fac70 100644 --- a/frontend/src/pages/sessions/useSessionsState.ts +++ b/frontend/src/pages/sessions/useSessionsState.ts @@ -201,7 +201,7 @@ export function useSessionsState() { session.enhanced_enabled === true && session.status !== "active" && session.allow_auto_retry !== false - && (session.retry_cycle_count ?? 0) < 3 + && (session.retry_cycle_count ?? 0) < 5 ); }, []); diff --git a/frontend/src/pages/terminal/useTerminalSocket.ts b/frontend/src/pages/terminal/useTerminalSocket.ts index 6f8929f..67531e2 100644 --- a/frontend/src/pages/terminal/useTerminalSocket.ts +++ b/frontend/src/pages/terminal/useTerminalSocket.ts @@ -184,13 +184,15 @@ export function useTerminalSocket({ } if (autoReconnect && !event.wasClean && reconnectAttemptRef.current < 5) { + const schedule = [2, 4, 8, 16, 32]; const attempt = reconnectAttemptRef.current + 1; - const delay = attempt * 5000; + const delaySeconds = schedule[attempt - 1] ?? schedule[schedule.length - 1]; + const delay = delaySeconds * 1000; reconnectAttemptRef.current = attempt; const reconnectMessage = t( - `连接已断开,${Math.round(delay / 1000)}秒后自动重连 (${reconnectAttemptRef.current}/5)...`, - `Connection lost. Auto reconnect in ${Math.round(delay / 1000)}s (${reconnectAttemptRef.current}/5)...` + `连接已断开,${delaySeconds}秒后自动重连 (${reconnectAttemptRef.current}/5)...`, + `Connection lost. Auto reconnect in ${delaySeconds}s (${reconnectAttemptRef.current}/5)...` ); term.write(`\r\n\x1b[31m${reconnectMessage}\x1b[0m\r\n`); setReconnectCountdown(Math.round(delay / 1000));