Skip to content

Commit 3154815

Browse files
committed
throttle backfill
1 parent ba01461 commit 3154815

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/modules/system/ccxt_candle_prefill_service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { ExchangeInstanceService } from './exchange_instance_service';
77
export type PrefillJob = [string, string, string];
88

99
const CANDLES_LIMIT = 500;
10+
// Delay between API calls to avoid rate limits (ms)
11+
const THROTTLE_MS = 250;
1012

1113
export class CcxtCandlePrefillService {
1214
private queue: PrefillJob[] = [];
@@ -59,9 +61,18 @@ export class CcxtCandlePrefillService {
5961
} catch (e: any) {
6062
this.logger.error(`[CcxtCandlePrefill] ${this.key(job)}: ${e.message || String(e)}`, { job });
6163
}
64+
65+
// Throttle between requests to avoid rate limits
66+
if (this.queue.length > 0) {
67+
await this.sleep(THROTTLE_MS);
68+
}
6269
}
6370
}
6471

72+
private sleep(ms: number): Promise<void> {
73+
return new Promise(resolve => setTimeout(resolve, ms));
74+
}
75+
6576
/**
6677
* Fetch 500 candles from the exchange REST API and return them immediately.
6778
* No DB storage — caller uses the candles directly.

0 commit comments

Comments
 (0)