market: gate finnhub requests to reduce yahoo fallback pressure (#1711)

Co-authored-by: Elie Habib <elie.habib@gmail.com>
This commit is contained in:
DrDavidL
2026-03-18 18:29:30 -05:00
committed by GitHub
parent a40c0a11fb
commit 2530416f5b
2 changed files with 22 additions and 1 deletions

View File

@@ -24,3 +24,23 @@ export function yahooGate(): Promise<void> {
});
return yahooQueue;
}
/**
* Global Finnhub request gate.
* Free-tier Finnhub keys are sensitive to burst concurrency; spacing requests
* reduces 429 cascades that otherwise spill into Yahoo fallback.
*/
let finnhubLastRequest = 0;
const FINNHUB_MIN_GAP_MS = 350;
let finnhubQueue: Promise<void> = Promise.resolve();
export function finnhubGate(): Promise<void> {
finnhubQueue = finnhubQueue.then(async () => {
const elapsed = Date.now() - finnhubLastRequest;
if (elapsed < FINNHUB_MIN_GAP_MS) {
await new Promise<void>(r => setTimeout(r, FINNHUB_MIN_GAP_MS - elapsed));
}
finnhubLastRequest = Date.now();
});
return finnhubQueue;
}

View File

@@ -1,7 +1,7 @@
/**
* Shared helpers, types, and constants for the market service handler RPCs.
*/
import { CHROME_UA, yahooGate } from '../../../_shared/constants';
import { CHROME_UA, finnhubGate, yahooGate } from '../../../_shared/constants';
import cryptoConfig from '../../../../shared/crypto.json';
import stablecoinConfig from '../../../../shared/stablecoins.json';
export { parseStringArray } from '../../../_shared/parse-string-array';
@@ -108,6 +108,7 @@ export async function fetchFinnhubQuote(
apiKey: string,
): Promise<{ symbol: string; price: number; changePercent: number } | null> {
try {
await finnhubGate();
const url = `https://finnhub.io/api/v1/quote?symbol=${encodeURIComponent(symbol)}`;
const resp = await fetch(url, {
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA, 'X-Finnhub-Token': apiKey },