fix and update

This commit is contained in:
lendry
2026-06-30 10:36:21 +03:00
parent a76997986a
commit 7f10b18336
6 changed files with 73 additions and 29 deletions

View File

@@ -677,10 +677,12 @@ export function buildSystemSettingPayload(setting: Pick<SystemSetting, 'key' | '
}
const GATEWAY_RETRY_STATUS = new Set([502, 503, 504]);
const GATEWAY_RETRY_ATTEMPTS = 4;
const GATEWAY_RETRY_ATTEMPTS = 2;
const GATEWAY_PROBE_MAX_ATTEMPTS = 48;
const GATEWAY_PROBE_BASE_DELAY_MS = 450;
const API_MAX_CONCURRENT = 8;
const API_BURST_MAX_CONCURRENT = 3;
const API_BURST_WINDOW_MS = 3000;
const API_READY_EVENT = 'idp-api-ready';
function sleep(ms: number): Promise<void> {
@@ -689,6 +691,7 @@ function sleep(ms: number): Promise<void> {
let gatewayReadyResolved = false;
let gatewayReadyPromise: Promise<void> | null = null;
let gatewayReadyAt = 0;
let activeApiRequests = 0;
const apiRequestWaiters: Array<() => void> = [];
@@ -699,6 +702,7 @@ export function resetApiGatewayWarmup() {
export function invalidateApiGatewayReady() {
gatewayReadyResolved = false;
gatewayReadyAt = 0;
}
export function isApiGatewayReady() {
@@ -708,6 +712,7 @@ export function isApiGatewayReady() {
export function markApiGatewayReady() {
if (gatewayReadyResolved) return;
gatewayReadyResolved = true;
gatewayReadyAt = Date.now();
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent(API_READY_EVENT));
}
@@ -724,13 +729,14 @@ export function subscribeApiReady(listener: () => void): () => void {
}
async function acquireApiSlot(): Promise<void> {
if (activeApiRequests < API_MAX_CONCURRENT) {
activeApiRequests += 1;
return;
const inBurst = gatewayReadyAt > 0 && Date.now() - gatewayReadyAt < API_BURST_WINDOW_MS;
const limit = inBurst ? API_BURST_MAX_CONCURRENT : API_MAX_CONCURRENT;
while (activeApiRequests >= limit) {
await new Promise<void>((resolve) => {
apiRequestWaiters.push(resolve);
});
}
await new Promise<void>((resolve) => {
apiRequestWaiters.push(resolve);
});
activeApiRequests += 1;
}