mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
market: gate finnhub requests to reduce yahoo fallback pressure (#1711)
Co-authored-by: Elie Habib <elie.habib@gmail.com>
This commit is contained in:
@@ -24,3 +24,23 @@ export function yahooGate(): Promise<void> {
|
|||||||
});
|
});
|
||||||
return yahooQueue;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Shared helpers, types, and constants for the market service handler RPCs.
|
* 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 cryptoConfig from '../../../../shared/crypto.json';
|
||||||
import stablecoinConfig from '../../../../shared/stablecoins.json';
|
import stablecoinConfig from '../../../../shared/stablecoins.json';
|
||||||
export { parseStringArray } from '../../../_shared/parse-string-array';
|
export { parseStringArray } from '../../../_shared/parse-string-array';
|
||||||
@@ -108,6 +108,7 @@ export async function fetchFinnhubQuote(
|
|||||||
apiKey: string,
|
apiKey: string,
|
||||||
): Promise<{ symbol: string; price: number; changePercent: number } | null> {
|
): Promise<{ symbol: string; price: number; changePercent: number } | null> {
|
||||||
try {
|
try {
|
||||||
|
await finnhubGate();
|
||||||
const url = `https://finnhub.io/api/v1/quote?symbol=${encodeURIComponent(symbol)}`;
|
const url = `https://finnhub.io/api/v1/quote?symbol=${encodeURIComponent(symbol)}`;
|
||||||
const resp = await fetch(url, {
|
const resp = await fetch(url, {
|
||||||
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA, 'X-Finnhub-Token': apiKey },
|
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA, 'X-Finnhub-Token': apiKey },
|
||||||
|
|||||||
Reference in New Issue
Block a user