统一全系统资源加载、轻量趋势与缓存恢复
This commit is contained in:
@@ -0,0 +1,457 @@
|
||||
export interface RequestScope {
|
||||
userId: string | null;
|
||||
authRevision: string | null;
|
||||
dataRevision: string | null;
|
||||
currentReleaseId: string | null;
|
||||
}
|
||||
|
||||
export interface CoordinatedResult<T> {
|
||||
data: T;
|
||||
status?: number;
|
||||
bytes?: number;
|
||||
}
|
||||
|
||||
export interface CoordinatedRequest<T> {
|
||||
url: string;
|
||||
method?: string;
|
||||
signal?: AbortSignal;
|
||||
timeoutMs?: number;
|
||||
cache?: boolean;
|
||||
queue?: boolean;
|
||||
retry?: boolean;
|
||||
tags?: readonly string[];
|
||||
category?: string;
|
||||
priority?: 'active' | 'background';
|
||||
execute: (signal: AbortSignal) => Promise<CoordinatedResult<T>>;
|
||||
}
|
||||
|
||||
export interface RequestDiagnostic {
|
||||
category: string;
|
||||
elapsedMs: number;
|
||||
bytes: number;
|
||||
status: number | null;
|
||||
retries: number;
|
||||
}
|
||||
|
||||
export const REQUEST_CACHE_LIMIT_BYTES = 32 * 1024 * 1024;
|
||||
export const REQUEST_MAX_GET_CONCURRENCY = 4;
|
||||
const DEFAULT_GET_TIMEOUT_MS = 30_000;
|
||||
const RETRY_DELAY_MS = 1_000;
|
||||
const MAX_DIAGNOSTICS = 120;
|
||||
|
||||
export class RequestTimeoutError extends Error {
|
||||
constructor() {
|
||||
super('请求超时');
|
||||
this.name = 'RequestTimeoutError';
|
||||
}
|
||||
}
|
||||
|
||||
interface CacheEntry {
|
||||
data: unknown;
|
||||
bytes: number;
|
||||
tags: readonly string[];
|
||||
}
|
||||
|
||||
interface Subscriber<T = unknown> {
|
||||
resolve: (value: T) => void;
|
||||
reject: (error: unknown) => void;
|
||||
signal?: AbortSignal;
|
||||
abortListener?: () => void;
|
||||
}
|
||||
|
||||
interface Flight<T = unknown> {
|
||||
key: string;
|
||||
url: string;
|
||||
scopeKey: string;
|
||||
scopeEpoch: number;
|
||||
cacheEpoch: number;
|
||||
controller: AbortController;
|
||||
subscribers: Set<Subscriber<T>>;
|
||||
request: CoordinatedRequest<T>;
|
||||
startedAt: number;
|
||||
deadlineAt: number;
|
||||
timer?: ReturnType<typeof setTimeout>;
|
||||
retries: number;
|
||||
queued: boolean;
|
||||
slotHeld: boolean;
|
||||
settled: boolean;
|
||||
}
|
||||
|
||||
let scope: RequestScope = { userId: null, authRevision: null, dataRevision: null, currentReleaseId: null };
|
||||
let scopeKey = serializeScope(scope);
|
||||
let scopeEpoch = 0;
|
||||
let cacheEpoch = 0;
|
||||
let activeGets = 0;
|
||||
let cacheBytes = 0;
|
||||
const flights = new Map<string, Flight<any>>();
|
||||
const queue: Flight<any>[] = [];
|
||||
const cache = new Map<string, CacheEntry>();
|
||||
const diagnostics: RequestDiagnostic[] = [];
|
||||
const directControllers = new Set<AbortController>();
|
||||
|
||||
export function configureRequestScope(next: RequestScope): void {
|
||||
const normalized: RequestScope = {
|
||||
userId: next.userId ?? null,
|
||||
authRevision: next.authRevision ?? null,
|
||||
dataRevision: next.dataRevision ?? null,
|
||||
currentReleaseId: next.currentReleaseId ?? null,
|
||||
};
|
||||
const nextKey = serializeScope(normalized);
|
||||
if (nextKey === scopeKey) return;
|
||||
scope = normalized;
|
||||
scopeKey = nextKey;
|
||||
scopeEpoch += 1;
|
||||
clearCache();
|
||||
for (const flight of [...flights.values()]) abortFlight(flight, createAbortError());
|
||||
for (const controller of directControllers) controller.abort();
|
||||
drainQueue();
|
||||
}
|
||||
|
||||
export function invalidateReadResources(tags?: readonly string[]): void {
|
||||
const selected = tags ? new Set(tags) : null;
|
||||
for (const [key, entry] of cache) {
|
||||
if (!selected || entry.tags.some(tag => selected.has(tag))) {
|
||||
cacheBytes -= entry.bytes;
|
||||
cache.delete(key);
|
||||
}
|
||||
}
|
||||
for (const flight of [...flights.values()]) {
|
||||
if (!selected || flight.request.tags?.some(tag => selected.has(tag))) abortFlight(flight, createAbortError());
|
||||
}
|
||||
drainQueue();
|
||||
}
|
||||
|
||||
/** Clears private response data without interrupting unrelated active reads. */
|
||||
export function clearReadCache(): void {
|
||||
clearCache();
|
||||
}
|
||||
|
||||
export function resetRequestScope(): void {
|
||||
scope = { userId: null, authRevision: null, dataRevision: null, currentReleaseId: null };
|
||||
scopeKey = serializeScope(scope);
|
||||
scopeEpoch += 1;
|
||||
clearCache();
|
||||
for (const flight of [...flights.values()]) abortFlight(flight, createAbortError());
|
||||
for (const controller of directControllers) controller.abort();
|
||||
drainQueue();
|
||||
}
|
||||
|
||||
export function coordinateRequest<T>(request: CoordinatedRequest<T>): Promise<T> {
|
||||
const method = (request.method ?? 'GET').toUpperCase();
|
||||
if (request.signal?.aborted) return Promise.reject(createAbortError());
|
||||
if (method !== 'GET' || request.queue === false) return runDirect(request);
|
||||
|
||||
const normalizedUrl = normalizeUrl(request.url);
|
||||
const flightKey = `${scopeEpoch}\u0000${scopeKey}\u0000${normalizedUrl}`;
|
||||
if (request.cache) {
|
||||
const cached = cache.get(flightKey);
|
||||
if (cached) {
|
||||
cache.delete(flightKey);
|
||||
cache.set(flightKey, cached);
|
||||
recordDiagnostic(request.category ?? categoryFromUrl(normalizedUrl), 0, cached.bytes, 200, 0);
|
||||
return Promise.resolve(cloneValue(cached.data) as T);
|
||||
}
|
||||
}
|
||||
|
||||
const existing = flights.get(flightKey) as Flight<T> | undefined;
|
||||
if (existing && !existing.settled) return subscribe(existing, request.signal);
|
||||
|
||||
const timeoutMs = request.timeoutMs ?? DEFAULT_GET_TIMEOUT_MS;
|
||||
const flight: Flight<T> = {
|
||||
key: flightKey,
|
||||
url: normalizedUrl,
|
||||
scopeKey,
|
||||
scopeEpoch,
|
||||
cacheEpoch,
|
||||
controller: new AbortController(),
|
||||
subscribers: new Set(),
|
||||
request: { ...request, method },
|
||||
startedAt: Date.now(),
|
||||
deadlineAt: Date.now() + timeoutMs,
|
||||
retries: 0,
|
||||
queued: true,
|
||||
slotHeld: false,
|
||||
settled: false,
|
||||
};
|
||||
flights.set(flightKey, flight);
|
||||
const subscriber = subscribe(flight, request.signal);
|
||||
flight.timer = setTimeout(() => {
|
||||
abortFlight(flight, new RequestTimeoutError());
|
||||
}, timeoutMs);
|
||||
queue.push(flight);
|
||||
drainQueue();
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
export function getRequestDiagnostics(): readonly RequestDiagnostic[] {
|
||||
return diagnostics.map(item => ({ ...item }));
|
||||
}
|
||||
|
||||
export function clearRequestDiagnostics(): void {
|
||||
diagnostics.length = 0;
|
||||
}
|
||||
|
||||
function subscribe<T>(flight: Flight<T>, signal?: AbortSignal): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const subscriber: Subscriber<T> = { resolve, reject, signal };
|
||||
if (signal?.aborted) {
|
||||
reject(createAbortError());
|
||||
return;
|
||||
}
|
||||
subscriber.abortListener = () => {
|
||||
removeSubscriber(flight, subscriber);
|
||||
reject(createAbortError());
|
||||
if (flight.subscribers.size === 0) abortFlight(flight, createAbortError());
|
||||
};
|
||||
signal?.addEventListener('abort', subscriber.abortListener, { once: true });
|
||||
flight.subscribers.add(subscriber);
|
||||
});
|
||||
}
|
||||
|
||||
function removeSubscriber<T>(flight: Flight<T>, subscriber: Subscriber<T>): void {
|
||||
flight.subscribers.delete(subscriber);
|
||||
if (subscriber.signal && subscriber.abortListener) subscriber.signal.removeEventListener('abort', subscriber.abortListener);
|
||||
}
|
||||
|
||||
function drainQueue(): void {
|
||||
if (activeGets >= REQUEST_MAX_GET_CONCURRENCY || queue.length === 0) return;
|
||||
queue.sort((left, right) => {
|
||||
const priority = Number(right.request.priority !== 'background') - Number(left.request.priority !== 'background');
|
||||
return priority || left.startedAt - right.startedAt;
|
||||
});
|
||||
while (activeGets < REQUEST_MAX_GET_CONCURRENCY && queue.length > 0) {
|
||||
const flight = queue.shift()!;
|
||||
if (flight.settled || flight.subscribers.size === 0) continue;
|
||||
flight.queued = false;
|
||||
flight.slotHeld = true;
|
||||
activeGets += 1;
|
||||
void executeFlight(flight);
|
||||
}
|
||||
}
|
||||
|
||||
async function executeFlight(flight: Flight<any>): Promise<void> {
|
||||
const request = flight.request;
|
||||
let status: number | null = null;
|
||||
let bytes = 0;
|
||||
try {
|
||||
const result = await executeWithRetry(flight);
|
||||
status = result.status ?? 200;
|
||||
bytes = result.bytes ?? estimateBytes(result.data);
|
||||
if (flight.settled) return;
|
||||
if (!isCurrentScope(flight)) throw createAbortError();
|
||||
if (request.cache && flight.cacheEpoch === cacheEpoch) putCache(flight.key, { data: cloneValue(result.data), bytes, tags: request.tags ?? [] });
|
||||
settleFlight(flight, undefined, result.data);
|
||||
} catch (error) {
|
||||
status = readStatus(error);
|
||||
bytes = readBytes(error);
|
||||
if (!flight.settled) settleFlight(flight, error);
|
||||
} finally {
|
||||
if (flight.timer) clearTimeout(flight.timer);
|
||||
flight.timer = undefined;
|
||||
releaseSlot(flight);
|
||||
recordDiagnostic(request.category ?? categoryFromUrl(flight.url), Date.now() - flight.startedAt, bytes, status, flight.retries);
|
||||
drainQueue();
|
||||
}
|
||||
}
|
||||
|
||||
async function executeWithRetry(flight: Flight<any>): Promise<CoordinatedResult<any>> {
|
||||
for (;;) {
|
||||
if (flight.controller.signal.aborted) throw createAbortError();
|
||||
try {
|
||||
const result = await flight.request.execute(flight.controller.signal);
|
||||
if (flight.controller.signal.aborted) throw createAbortError();
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (flight.controller.signal.aborted || flight.request.retry === false || flight.retries >= 1 || !isRetryable(error)) throw error;
|
||||
flight.retries += 1;
|
||||
await delay(RETRY_DELAY_MS, flight.controller.signal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runDirect<T>(request: CoordinatedRequest<T>): Promise<T> {
|
||||
const startedAt = Date.now(), ownerEpoch = scopeEpoch;
|
||||
const controller = new AbortController();
|
||||
directControllers.add(controller);
|
||||
let timedOut = false;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let status: number | null = null;
|
||||
let bytes = 0;
|
||||
let rejectCancellation: (error: Error) => void = () => {};
|
||||
const cancelled = new Promise<never>((_, reject) => { rejectCancellation = reject; });
|
||||
const onCancelled = () => rejectCancellation(createAbortError());
|
||||
controller.signal.addEventListener('abort', onCancelled, { once: true });
|
||||
const abortListener = () => controller.abort();
|
||||
request.signal?.addEventListener('abort', abortListener, { once: true });
|
||||
if (request.signal?.aborted) controller.abort();
|
||||
try {
|
||||
if (controller.signal.aborted) throw createAbortError();
|
||||
if (request.timeoutMs && request.timeoutMs > 0) {
|
||||
timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
controller.abort();
|
||||
}, request.timeoutMs);
|
||||
}
|
||||
const result = await Promise.race([request.execute(controller.signal), cancelled]);
|
||||
if (controller.signal.aborted || ownerEpoch !== scopeEpoch) throw createAbortError();
|
||||
status = result.status ?? 200;
|
||||
bytes = result.bytes ?? estimateBytes(result.data);
|
||||
return result.data;
|
||||
} catch (error) {
|
||||
status = readStatus(error);
|
||||
bytes = readBytes(error);
|
||||
if (timedOut) throw new RequestTimeoutError();
|
||||
throw error;
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
directControllers.delete(controller);
|
||||
controller.signal.removeEventListener('abort', onCancelled);
|
||||
request.signal?.removeEventListener('abort', abortListener);
|
||||
recordDiagnostic(request.category ?? categoryFromUrl(request.url), Date.now() - startedAt, bytes, status, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function settleFlight(flight: Flight<any>, error?: unknown, value?: unknown): void {
|
||||
if (flight.settled) return;
|
||||
flight.settled = true;
|
||||
if (flight.timer) clearTimeout(flight.timer);
|
||||
flight.timer = undefined;
|
||||
flights.delete(flight.key);
|
||||
if (flight.queued) {
|
||||
const index = queue.indexOf(flight);
|
||||
if (index >= 0) queue.splice(index, 1);
|
||||
flight.queued = false;
|
||||
}
|
||||
const subscribers = [...flight.subscribers];
|
||||
flight.subscribers.clear();
|
||||
for (const subscriber of subscribers) {
|
||||
if (subscriber.signal && subscriber.abortListener) subscriber.signal.removeEventListener('abort', subscriber.abortListener);
|
||||
if (error !== undefined) subscriber.reject(error);
|
||||
else subscriber.resolve(value as any);
|
||||
}
|
||||
}
|
||||
|
||||
function abortFlight(flight: Flight<any>, error: unknown): void {
|
||||
if (flight.settled) return;
|
||||
flight.controller.abort();
|
||||
settleFlight(flight, error);
|
||||
releaseSlot(flight);
|
||||
}
|
||||
|
||||
function releaseSlot(flight: Flight<any>): void {
|
||||
if (!flight.slotHeld) return;
|
||||
flight.slotHeld = false;
|
||||
activeGets = Math.max(0, activeGets - 1);
|
||||
}
|
||||
|
||||
function isCurrentScope(flight: Flight<any>): boolean {
|
||||
return flight.scopeEpoch === scopeEpoch && flight.scopeKey === scopeKey;
|
||||
}
|
||||
|
||||
function putCache(key: string, entry: CacheEntry): void {
|
||||
const safeBytes = Math.max(0, Number.isFinite(entry.bytes) ? entry.bytes : estimateBytes(entry.data));
|
||||
if (safeBytes > REQUEST_CACHE_LIMIT_BYTES) return;
|
||||
const previous = cache.get(key);
|
||||
if (previous) cacheBytes -= previous.bytes;
|
||||
cache.delete(key);
|
||||
cache.set(key, { ...entry, bytes: safeBytes });
|
||||
cacheBytes += safeBytes;
|
||||
while (cacheBytes > REQUEST_CACHE_LIMIT_BYTES && cache.size > 0) {
|
||||
const oldestKey = cache.keys().next().value as string | undefined;
|
||||
if (oldestKey === undefined) break;
|
||||
const oldest = cache.get(oldestKey)!;
|
||||
cache.delete(oldestKey);
|
||||
cacheBytes -= oldest.bytes;
|
||||
}
|
||||
}
|
||||
|
||||
function clearCache(): void {
|
||||
cache.clear();
|
||||
cacheBytes = 0;
|
||||
cacheEpoch += 1;
|
||||
}
|
||||
|
||||
function estimateBytes(value: unknown): number {
|
||||
try { return new TextEncoder().encode(JSON.stringify(value) ?? '').byteLength; }
|
||||
catch { return 0; }
|
||||
}
|
||||
|
||||
function cloneValue(value: unknown): unknown {
|
||||
if (typeof structuredClone === 'function') return structuredClone(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
function recordDiagnostic(category: string, elapsedMs: number, bytes: number, status: number | null, retries: number): void {
|
||||
diagnostics.push({ category, elapsedMs: Math.max(0, elapsedMs), bytes: Math.max(0, bytes), status, retries });
|
||||
if (diagnostics.length > MAX_DIAGNOSTICS) diagnostics.splice(0, diagnostics.length - MAX_DIAGNOSTICS);
|
||||
}
|
||||
|
||||
function isRetryable(error: unknown): boolean {
|
||||
if (!error || typeof error !== 'object') return false;
|
||||
if ((error as { name?: unknown }).name === 'AbortError' || (error as { name?: unknown }).name === 'RequestTimeoutError') return false;
|
||||
const status = readStatus(error);
|
||||
return error instanceof TypeError || status === 0 || status === 502 || status === 503 || status === 504;
|
||||
}
|
||||
|
||||
function readStatus(error: unknown): number | null {
|
||||
if (!error || typeof error !== 'object') return null;
|
||||
const status = (error as { status?: unknown }).status;
|
||||
return typeof status === 'number' && Number.isFinite(status) ? status : null;
|
||||
}
|
||||
|
||||
function readBytes(error: unknown): number {
|
||||
if (!error || typeof error !== 'object') return 0;
|
||||
const bytes = (error as { responseBytes?: unknown }).responseBytes;
|
||||
return typeof bytes === 'number' && Number.isFinite(bytes) ? bytes : 0;
|
||||
}
|
||||
|
||||
function delay(ms: number, signal: AbortSignal): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (signal.aborted) { reject(createAbortError()); return; }
|
||||
const timer = setTimeout(() => {
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
resolve();
|
||||
}, ms);
|
||||
const onAbort = () => {
|
||||
clearTimeout(timer);
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
reject(createAbortError());
|
||||
};
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeUrl(value: string): string {
|
||||
const base = typeof location === 'undefined' ? 'http://localhost' : location.origin;
|
||||
const url = new URL(value, base);
|
||||
const params = [...url.searchParams.entries()].sort(([leftKey, leftValue], [rightKey, rightValue]) =>
|
||||
leftKey.localeCompare(rightKey) || leftValue.localeCompare(rightValue));
|
||||
const normalized = new URLSearchParams();
|
||||
for (const [key, item] of params) normalized.append(key, item);
|
||||
return `${url.pathname}${normalized.size ? `?${normalized.toString()}` : ''}`;
|
||||
}
|
||||
|
||||
function serializeScope(value: RequestScope): string {
|
||||
return JSON.stringify([value.userId, value.authRevision, value.dataRevision, value.currentReleaseId]);
|
||||
}
|
||||
|
||||
function categoryFromUrl(value: string): string {
|
||||
const pathname = new URL(value, 'http://localhost').pathname;
|
||||
if (pathname === '/api/workspace/state') return 'workspace';
|
||||
if (pathname.startsWith('/api/auth/')) return 'auth';
|
||||
if (pathname.startsWith('/api/metrics')) return 'metrics';
|
||||
if (pathname.includes('/export')) return 'export';
|
||||
if (pathname.startsWith('/api/analysis/')) return 'analysis';
|
||||
if (pathname.startsWith('/api/governance/')) return 'governance';
|
||||
if (pathname.startsWith('/api/tasks')) return 'tasks';
|
||||
if (pathname.startsWith('/api/sources')) return 'sources';
|
||||
if (pathname.startsWith('/api/analytics/')) return 'analytics';
|
||||
return 'other';
|
||||
}
|
||||
|
||||
function createAbortError(): Error {
|
||||
if (typeof DOMException !== 'undefined') return new DOMException('请求已取消', 'AbortError');
|
||||
const error = new Error('请求已取消');
|
||||
error.name = 'AbortError';
|
||||
return error;
|
||||
}
|
||||
Reference in New Issue
Block a user