fix(webui): recover stalled requests

This commit is contained in:
DrSmoothl committed 2026-07-29 02:15:30 +08:00
1 parent f97444c391
commit 04cbb8cbf3
5 files changed
+146 -13

No files matched your search

@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Eye, Loader2, User, UserX } from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
@@ -37,31 +37,48 @@ export function ProcessProbeDialog({ pid, processName, open, onOpenChange, onLoa
const [loading, setLoading] = useState(false);
const [info, setInfo] = useState<QqPortLoginInfo | null>(null);
const [error, setError] = useState<string | null>(null);
const activeProbe = useRef<AbortController | null>(null);
const probe = useCallback(async () => {
activeProbe.current?.abort();
const controller = new AbortController();
activeProbe.current = controller;
setLoading(true);
setError(null);
setInfo(null);
try {
const result = await api.processes.probeLoginInfo(pid);
const result = await api.processes.probeLoginInfo(pid, controller.signal);
if (activeProbe.current !== controller) return;
setInfo(result as QqPortLoginInfo | null);
if (!result) {
setError('未检测到登录信息(端口 9210-9219 无响应)');
}
} catch (err) {
if (activeProbe.current !== controller || controller.signal.aborted) return;
setError(err instanceof Error ? err.message : '探测失败');
} finally {
setLoading(false);
if (activeProbe.current === controller) {
activeProbe.current = null;
setLoading(false);
}
}
}, [api.processes, pid]);
useEffect(() => {
if (open) {
probe();
void probe();
} else {
activeProbe.current?.abort();
activeProbe.current = null;
setLoading(false);
setInfo(null);
setError(null);
}
return () => {
activeProbe.current?.abort();
activeProbe.current = null;
};
}, [open, probe]);
return (
+32 -4
View File
@@ -18,6 +18,7 @@ import {
import { localStorageTokenStore } from './token-store';
const DEFAULT_TOKEN_KEY = 'snowluma_token';
const REQUEST_TIMEOUT_MS = 30_000;
interface ErrorPayload {
message?: string;
@@ -72,7 +73,10 @@ class HttpApiClient implements ApiClient {
load: (pid) => this.postJson<ProcessActionResult>(`/api/processes/${pid}/load`),
unload: (pid) => this.postJson<ProcessActionResult>(`/api/processes/${pid}/unload`),
refresh: (pid) => this.postJson<ProcessActionResult>(`/api/processes/${pid}/refresh`),
probeLoginInfo: (pid) => this.getJson<{ info: unknown }>(`/api/processes/${pid}/probe-login`).then((d) => d.info ?? null),
probeLoginInfo: (pid, signal) => this.fetchJson<{ info: unknown }>(
`/api/processes/${pid}/probe-login`,
{ signal },
).then((d) => d.info ?? null),
};
this.config = {
@@ -211,7 +215,7 @@ class HttpApiClient implements ApiClient {
getPublic: async () => {
// Pre-auth path: a plain fetch with no bearer. Used by the login page
// to theme itself before the operator has signed in.
const res = await fetch('/api/ui/public');
const res = await this.fetchWithDeadline('/api/ui/public');
if (!res.ok) throw new ApiError(res.status, '无法获取外观配置');
const data = await readJson<{ appearance: UiAppearance }>(res);
return data.appearance;
@@ -303,7 +307,8 @@ class HttpApiClient implements ApiClient {
};
if (this.currentToken) headers['Authorization'] = `Bearer ${this.currentToken}`;
if (init.body && !headers['Content-Type']) headers['Content-Type'] = 'application/json';
const res = await fetch(url, { ...init, headers });
const res = await this.fetchWithDeadline(url, { ...init, headers });
if (res.status === 401) {
this.setToken(null);
this.onUnauthorized?.();
@@ -311,6 +316,29 @@ class HttpApiClient implements ApiClient {
return res;
}
private async fetchWithDeadline(
url: string,
init: RequestInit = {},
): Promise<Response> {
const deadline = new AbortController();
const onCallerAbort = () => deadline.abort(init.signal?.reason);
if (init.signal?.aborted) onCallerAbort();
else init.signal?.addEventListener('abort', onCallerAbort, { once: true });
const timer = setTimeout(() => deadline.abort(), REQUEST_TIMEOUT_MS);
try {
const res = await fetch(url, { ...init, signal: deadline.signal });
return res;
} catch (error) {
if (deadline.signal.aborted && !init.signal?.aborted) {
throw new ApiError(408, '请求超时,请重试', 'REQUEST_TIMEOUT');
}
throw error;
} finally {
clearTimeout(timer);
init.signal?.removeEventListener('abort', onCallerAbort);
}
}
/** Like request(), but throws ApiError on non-2xx and parses JSON. */
private async fetchJson<T>(url: string, init: RequestInit = {}): Promise<T> {
const res = await this.request(url, init);
@@ -349,7 +377,7 @@ class HttpApiClient implements ApiClient {
// Login deliberately bypasses fetchJson/onUnauthorized so a bad password
// doesn't trigger a global sign-out side effect.
try {
const res = await fetch('/api/login', {
const res = await this.fetchWithDeadline('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password }),
+1 -1
View File
@@ -151,7 +151,7 @@ export interface ApiClient {
load(pid: number): Promise<ProcessActionResult>;
unload(pid: number): Promise<ProcessActionResult>;
refresh(pid: number): Promise<ProcessActionResult>;
probeLoginInfo(pid: number): Promise<unknown>;
probeLoginInfo(pid: number, signal?: AbortSignal): Promise<unknown>;
};
// ---- OneBotInstance per-UIN config ----