모바일 로드 오류 수정
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 업비트 /ticker REST — 청크·지연·429 백오프 (공유)
|
||||
*/
|
||||
import { UPBIT_API } from './upbitApi';
|
||||
|
||||
export interface UpbitTickerRaw {
|
||||
market?: string;
|
||||
code?: string;
|
||||
trade_price: number;
|
||||
signed_change_rate: number;
|
||||
signed_change_price: number;
|
||||
acc_trade_price_24h: number;
|
||||
acc_trade_volume_24h?: number;
|
||||
opening_price: number;
|
||||
high_price: number;
|
||||
low_price: number;
|
||||
change?: 'RISE' | 'FALL' | 'EVEN';
|
||||
}
|
||||
|
||||
const CHUNK_SIZE = 60;
|
||||
const CHUNK_DELAY_MS = 150;
|
||||
const MAX_CHUNK_ATTEMPTS = 5;
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise(r => setTimeout(r, ms));
|
||||
}
|
||||
|
||||
/** 중복 제거 후 청크 단위 ticker 조회 (429 시 백오프) */
|
||||
export async function fetchTickersThrottled(markets: string[]): Promise<UpbitTickerRaw[]> {
|
||||
const unique = [...new Set(markets.filter(m => m?.startsWith('KRW-')))];
|
||||
if (unique.length === 0) return [];
|
||||
|
||||
const results: UpbitTickerRaw[] = [];
|
||||
for (let i = 0; i < unique.length; i += CHUNK_SIZE) {
|
||||
const chunk = unique.slice(i, i + CHUNK_SIZE);
|
||||
let done = false;
|
||||
for (let attempt = 0; attempt < MAX_CHUNK_ATTEMPTS && !done; attempt++) {
|
||||
try {
|
||||
const res = await fetch(`${UPBIT_API}/ticker?markets=${chunk.join(',')}`);
|
||||
if (res.status === 429) {
|
||||
await sleep(500 * (attempt + 1));
|
||||
continue;
|
||||
}
|
||||
if (res.ok) {
|
||||
const data = (await res.json()) as UpbitTickerRaw[];
|
||||
if (Array.isArray(data)) results.push(...data);
|
||||
}
|
||||
done = true;
|
||||
} catch {
|
||||
await sleep(300 * (attempt + 1));
|
||||
}
|
||||
}
|
||||
if (i + CHUNK_SIZE < unique.length) await sleep(CHUNK_DELAY_MS);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
Reference in New Issue
Block a user