fix and update

This commit is contained in:
lendry
2026-06-30 09:11:55 +03:00
parent f1d6a5167f
commit 250976ca08
5 changed files with 94 additions and 142 deletions

View File

@@ -567,6 +567,7 @@ let fedcmSyncInFlight = false;
/** Откладывает FedCM sync после OAuth/bootstrap, чтобы не конкурировать с consent/session. */
export function scheduleFedcmSessionSync(accessToken?: string | null, delayMs = 1500) {
if (typeof window === 'undefined') return;
if (window.location.pathname.startsWith('/auth/oauth/authorize')) return;
if (fedcmSyncTimer) {
clearTimeout(fedcmSyncTimer);
}
@@ -676,12 +677,9 @@ export function buildSystemSettingPayload(setting: Pick<SystemSetting, 'key' | '
}
const GATEWAY_RETRY_STATUS = new Set([502, 503, 504]);
const GATEWAY_RETRY_ATTEMPTS = 6;
const GATEWAY_WARMUP_INITIAL_DELAY_MS = 500;
const GATEWAY_WARMUP_DELAY_MS = 450;
const GATEWAY_READY_CONSECUTIVE_OK = 1;
const GATEWAY_WARMUP_MAX_DELAY_MS = 2200;
const GATEWAY_WARMUP_MAX_TOTAL_MS = 90_000;
const GATEWAY_RETRY_ATTEMPTS = 2;
const GATEWAY_PROBE_MAX_ATTEMPTS = 4;
const GATEWAY_PROBE_DELAY_MS = 600;
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -722,30 +720,18 @@ async function probeGatewayAlive(): Promise<boolean> {
}
async function waitForGatewayAlive(): Promise<void> {
await sleep(GATEWAY_WARMUP_INITIAL_DELAY_MS);
const deadline = Date.now() + GATEWAY_WARMUP_MAX_TOTAL_MS;
let consecutiveOk = 0;
let attempt = 0;
while (Date.now() < deadline) {
for (let attempt = 0; attempt < GATEWAY_PROBE_MAX_ATTEMPTS; attempt += 1) {
if (await probeGatewayAlive()) {
consecutiveOk += 1;
if (consecutiveOk >= GATEWAY_READY_CONSECUTIVE_OK) {
gatewayReadyResolved = true;
return;
}
} else {
consecutiveOk = 0;
gatewayReadyResolved = true;
return;
}
if (attempt + 1 < GATEWAY_PROBE_MAX_ATTEMPTS) {
await sleep(GATEWAY_PROBE_DELAY_MS * (attempt + 1));
}
attempt += 1;
const delay = Math.min(GATEWAY_WARMUP_DELAY_MS * 1.28 ** attempt, GATEWAY_WARMUP_MAX_DELAY_MS);
await sleep(delay);
}
gatewayReadyResolved = true;
}
/** Один общий прогрев перед API: без сброса promise и без десятков параллельных проб /health/ready. */
/** Один общий прогрев: до 4 проб /health, без бесконечного цикла. */
export async function ensureApiGatewayReady(force = false): Promise<void> {
if (typeof window === 'undefined') return;
if (gatewayReadyResolved && !force) return;
@@ -763,7 +749,7 @@ export async function ensureApiGatewayReady(force = false): Promise<void> {
}
function gatewayRetryDelay(attempt: number): number {
return Math.min(500 * (attempt + 1), 2800);
return 700 + attempt * 400;
}
/** Повтор при кратковременном 502/503/504 (nginx не достучался до api-gateway после перезапуска). */
@@ -793,7 +779,7 @@ async function fetchWithGatewayRetry(url: string, init: RequestInit): Promise<Re
}
export async function apiFetch<T>(path: string, options: RequestInit = {}, token?: string | null, allowRetry = true): Promise<T> {
if (typeof window !== 'undefined' && !path.startsWith('/health')) {
if (typeof window !== 'undefined' && !gatewayReadyResolved && !path.startsWith('/health')) {
await ensureApiGatewayReady();
}
@@ -839,12 +825,6 @@ export async function apiFetch<T>(path: string, options: RequestInit = {}, token
}
}
if (allowRetry && isGatewayUnavailableError(error)) {
invalidateApiGatewayReady();
await sleep(900);
return apiFetch<T>(path, options, token, false);
}
throw error;
}