fix and update
This commit is contained in:
@@ -544,6 +544,9 @@ export async function refreshAuthSession(): Promise<RefreshSessionResponse> {
|
||||
export async function syncFedcmSession(accessToken?: string | null) {
|
||||
const token = resolveAccessToken(accessToken);
|
||||
if (typeof window === 'undefined' || !token) return;
|
||||
if (!isApiGatewayReady()) {
|
||||
await ensureApiGatewayReady();
|
||||
}
|
||||
try {
|
||||
await apiFetch<{ synced: boolean }>(
|
||||
'/fedcm/session/sync',
|
||||
@@ -558,6 +561,25 @@ export async function syncFedcmSession(accessToken?: string | null) {
|
||||
}
|
||||
}
|
||||
|
||||
let fedcmSyncTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let fedcmSyncInFlight = false;
|
||||
|
||||
/** Откладывает FedCM sync после OAuth/bootstrap, чтобы не конкурировать с consent/session. */
|
||||
export function scheduleFedcmSessionSync(accessToken?: string | null, delayMs = 1500) {
|
||||
if (typeof window === 'undefined') return;
|
||||
if (fedcmSyncTimer) {
|
||||
clearTimeout(fedcmSyncTimer);
|
||||
}
|
||||
fedcmSyncTimer = setTimeout(() => {
|
||||
fedcmSyncTimer = null;
|
||||
if (fedcmSyncInFlight) return;
|
||||
fedcmSyncInFlight = true;
|
||||
void syncFedcmSession(accessToken).finally(() => {
|
||||
fedcmSyncInFlight = false;
|
||||
});
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
async function trySilentTokenRefresh(): Promise<RefreshSessionResponse | null> {
|
||||
try {
|
||||
const refreshed = await refreshAuthSession();
|
||||
@@ -654,11 +676,12 @@ export function buildSystemSettingPayload(setting: Pick<SystemSetting, 'key' | '
|
||||
}
|
||||
|
||||
const GATEWAY_RETRY_STATUS = new Set([502, 503, 504]);
|
||||
const GATEWAY_RETRY_ATTEMPTS = 8;
|
||||
const GATEWAY_WARMUP_MAX_ATTEMPTS = 80;
|
||||
const GATEWAY_WARMUP_DELAY_MS = 400;
|
||||
const GATEWAY_READY_CONSECUTIVE_OK = 2;
|
||||
const GATEWAY_WARMUP_MAX_DELAY_MS = 2500;
|
||||
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;
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
@@ -674,7 +697,6 @@ export function resetApiGatewayWarmup() {
|
||||
|
||||
export function invalidateApiGatewayReady() {
|
||||
gatewayReadyResolved = false;
|
||||
gatewayReadyPromise = null;
|
||||
}
|
||||
|
||||
export function isApiGatewayReady() {
|
||||
@@ -685,11 +707,13 @@ export function markApiGatewayReady() {
|
||||
gatewayReadyResolved = true;
|
||||
}
|
||||
|
||||
async function probeGatewayReady(): Promise<boolean> {
|
||||
/** Liveness: nginx → api-gateway HTTP. Не /health/ready — gRPC 503 не должен блокировать SPA и засорять консоль. */
|
||||
async function probeGatewayAlive(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`${getApiUrl()}/health/ready`, {
|
||||
const response = await fetch(`${getApiUrl()}/health`, {
|
||||
method: 'GET',
|
||||
cache: 'no-store'
|
||||
cache: 'no-store',
|
||||
signal: AbortSignal.timeout(4500)
|
||||
});
|
||||
return response.ok;
|
||||
} catch {
|
||||
@@ -697,56 +721,69 @@ async function probeGatewayReady(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Ждёт готовности api-gateway и sso-core (readiness по gRPC, не liveness /health). */
|
||||
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) {
|
||||
if (await probeGatewayAlive()) {
|
||||
consecutiveOk += 1;
|
||||
if (consecutiveOk >= GATEWAY_READY_CONSECUTIVE_OK) {
|
||||
gatewayReadyResolved = true;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
consecutiveOk = 0;
|
||||
}
|
||||
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. */
|
||||
export async function ensureApiGatewayReady(force = false): Promise<void> {
|
||||
if (typeof window === 'undefined') return;
|
||||
if (gatewayReadyResolved && !force) return;
|
||||
if (gatewayReadyPromise && !force) return gatewayReadyPromise;
|
||||
if (gatewayReadyPromise) return gatewayReadyPromise;
|
||||
|
||||
if (force) {
|
||||
gatewayReadyResolved = false;
|
||||
gatewayReadyPromise = null;
|
||||
}
|
||||
gatewayReadyPromise = (async () => {
|
||||
await sleep(600);
|
||||
let consecutiveOk = 0;
|
||||
for (let attempt = 0; attempt < GATEWAY_WARMUP_MAX_ATTEMPTS; attempt++) {
|
||||
if (await probeGatewayReady()) {
|
||||
consecutiveOk += 1;
|
||||
if (consecutiveOk >= GATEWAY_READY_CONSECUTIVE_OK) {
|
||||
gatewayReadyResolved = true;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
consecutiveOk = 0;
|
||||
}
|
||||
const delay = Math.min(GATEWAY_WARMUP_DELAY_MS * 1.35 ** attempt, GATEWAY_WARMUP_MAX_DELAY_MS);
|
||||
await sleep(delay);
|
||||
}
|
||||
|
||||
gatewayReadyPromise = waitForGatewayAlive().finally(() => {
|
||||
gatewayReadyPromise = null;
|
||||
})();
|
||||
});
|
||||
|
||||
return gatewayReadyPromise;
|
||||
}
|
||||
|
||||
function gatewayRetryDelay(attempt: number): number {
|
||||
return Math.min(500 * (attempt + 1), 2800);
|
||||
}
|
||||
|
||||
/** Повтор при кратковременном 502/503/504 (nginx не достучался до api-gateway после перезапуска). */
|
||||
async function fetchWithGatewayRetry(url: string, init: RequestInit): Promise<Response> {
|
||||
let lastNetworkError: unknown;
|
||||
for (let attempt = 0; attempt < GATEWAY_RETRY_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
const response = await fetch(url, init);
|
||||
if (response.ok) {
|
||||
markApiGatewayReady();
|
||||
}
|
||||
if (GATEWAY_RETRY_STATUS.has(response.status) && attempt + 1 < GATEWAY_RETRY_ATTEMPTS) {
|
||||
invalidateApiGatewayReady();
|
||||
await ensureApiGatewayReady(true);
|
||||
await sleep(400 * (attempt + 1));
|
||||
await sleep(gatewayRetryDelay(attempt));
|
||||
continue;
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
lastNetworkError = error;
|
||||
if (attempt + 1 < GATEWAY_RETRY_ATTEMPTS) {
|
||||
invalidateApiGatewayReady();
|
||||
await ensureApiGatewayReady(true);
|
||||
await sleep(400 * (attempt + 1));
|
||||
await sleep(gatewayRetryDelay(attempt));
|
||||
continue;
|
||||
}
|
||||
throw mapFetchError(error);
|
||||
@@ -804,13 +841,14 @@ export async function apiFetch<T>(path: string, options: RequestInit = {}, token
|
||||
|
||||
if (allowRetry && isGatewayUnavailableError(error)) {
|
||||
invalidateApiGatewayReady();
|
||||
await ensureApiGatewayReady(true);
|
||||
await sleep(900);
|
||||
return apiFetch<T>(path, options, token, false);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
markApiGatewayReady();
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user