perf: shared Finnhub cache with timeouts, key pool and live quotes

- Every Finnhub call has a 5s timeout (a stalled socket held pages for 300s),
  at most 2 in flight per key, de-duplicated, stale-while-revalidate, and a 60s
  failure cool-down.
- FINNHUB_API_KEYS pool rotated per request, token sent as a header so cache keys
  are shared; falls back on 429.
- NEXT_PUBLIC_OPENSTOCK_DATA_MODE: cached (hourly, default) or realtime (15s).
- /api/quotes + useLiveQuotes for on-screen prices; honest freshness labels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ravixalgorithm
2026-09-25 15:28:19 +05:30
parent 74cc4307b9
commit 149199bc9c
5 changed files with 229 additions and 67 deletions

23
app/api/quotes/route.ts Normal file
View File

@@ -0,0 +1,23 @@
import { NextRequest, NextResponse } from "next/server";
import { getSession } from "@/lib/better-auth/auth";
import { getLiveQuotes } from "@/lib/actions/finnhub.actions";
const MAX_SYMBOLS = 25;
const SYMBOL_PATTERN = /^[A-Z0-9.:\-]{1,20}$/;
// Polled by on-screen prices. Signed-in only and capped, so nobody can burn the shared Finnhub quota.
export async function GET(req: NextRequest) {
const session = await getSession();
if (!session?.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const symbols = [...new Set(
(req.nextUrl.searchParams.get("symbols") ?? "")
.split(",")
.map((s) => s.trim().toUpperCase())
.filter((s) => SYMBOL_PATTERN.test(s)),
)].slice(0, MAX_SYMBOLS);
return NextResponse.json(await getLiveQuotes(symbols), {
headers: { "Cache-Control": "private, max-age=5" },
});
}

View File

@@ -0,0 +1,14 @@
import { isRealtime } from "@/lib/market-data";
// Honest label for how old a number is: "Live" in realtime mode, otherwise the time of the last trade.
const DataFreshness = ({ tradeTime }: { tradeTime?: number }) => {
if (isRealtime) {
return <span className="inline-flex items-center gap-1.5 text-[12px] text-faint"><span className="live-dot" /> Live · 15s</span>;
}
const asOf = tradeTime
? new Date(tradeTime * 1000).toLocaleString('en-US', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' })
: null;
return <span className="text-[12px] text-faint">{asOf ? `As of ${asOf}` : 'Delayed'} · refreshed hourly</span>;
};
export default DataFreshness;

37
hooks/useLiveQuotes.ts Normal file
View File

@@ -0,0 +1,37 @@
'use client';
import { useEffect, useState } from "react";
import { isRealtime } from "@/lib/market-data";
export type LiveQuote = { c?: number; d?: number; dp?: number; h?: number; l?: number; o?: number; pc?: number; t?: number };
// Realtime mode only: polls the shared /api/quotes cache while the tab is visible.
// In cached mode prices come from the server render and refresh hourly for everyone.
export function useLiveQuotes(symbols: string[], intervalMs = 15_000) {
const [quotes, setQuotes] = useState<Record<string, LiveQuote | null>>({});
const key = symbols.join(",");
useEffect(() => {
if (!key || !isRealtime) return;
let cancelled = false;
const load = async () => {
if (document.visibilityState !== "visible") return;
try {
const res = await fetch(`/api/quotes?symbols=${encodeURIComponent(key)}`);
if (res.ok && !cancelled) setQuotes(await res.json());
} catch { /* keep the last good prices */ }
};
const onVisibility = () => { if (document.visibilityState === "visible") load(); };
const interval = setInterval(load, intervalMs);
document.addEventListener("visibilitychange", onVisibility);
return () => {
cancelled = true;
clearInterval(interval);
document.removeEventListener("visibilitychange", onVisibility);
};
}, [key, intervalMs]);
return quotes;
}

View File

@@ -1,16 +1,38 @@
'use server';
import { getDateRange, validateArticle, formatArticle } from '@/lib/utils';
import { POPULAR_STOCK_SYMBOLS } from '@/lib/constants';
import { POPULAR_STOCKS } from '@/lib/constants';
import { cache } from 'react';
import { QUOTE_TTL_SECONDS } from '@/lib/market-data';
import { hasFinnhubQuotes } from '@/lib/markets';
const FINNHUB_BASE_URL = 'https://finnhub.io/api/v1';
const NEXT_PUBLIC_FINNHUB_API_KEY = process.env.NEXT_PUBLIC_FINNHUB_API_KEY ?? '';
// Key pool: each free key has its own 60 req/min, so N keys = N x the quota. Rotated per request.
const FINNHUB_KEYS = (process.env.FINNHUB_API_KEYS || process.env.NEXT_PUBLIC_FINNHUB_API_KEY || process.env.FINNHUB_API_KEY || '')
.split(',')
.map((k) => k.trim())
.filter(Boolean);
let nextKeyIndex = 0;
const FETCH_TIMEOUT_MS = 5000;
// Finnhub stalls bursts of parallel requests from one key (measured: 4 at once -> most hang),
// so calls are queued. ponytail: per-instance limiter; a shared queue (Redis) if instances multiply.
const MAX_IN_FLIGHT_PER_KEY = 2;
const MAX_CACHE_ENTRIES = 1000;
const FAILURE_TTL_MS = 60_000;
// Quotes are shared across everyone looking at a symbol: Finnhub cost scales with symbols, not users
const LIVE_QUOTE_TTL = QUOTE_TTL_SECONDS;
// Long-lived responses also go to the Next data cache, which Vercel shares across all instances
const SHARED_CACHE_MIN_TTL = 60;
type FinnhubQuote = {
c?: number;
d?: number;
dp?: number;
c?: number; // current
d?: number; // change
dp?: number; // change %
h?: number; // day high
l?: number; // day low
o?: number; // open
pc?: number; // previous close
t?: number; // unix time of the last trade
};
type FinnhubCompanyProfile = {
@@ -20,6 +42,8 @@ type FinnhubCompanyProfile = {
marketCapitalization?: number;
name?: string;
ticker?: string;
finnhubIndustry?: string;
weburl?: string;
};
type SearchStockCandidate = FinnhubSearchResult & {
@@ -33,20 +57,92 @@ const FINNHUB_EXCHANGE_SUFFIXES = new Set([
'TA', 'TO', 'TW', 'TWO', 'V', 'VI', 'WA',
]);
async function fetchJSON<T>(url: string, revalidateSeconds?: number): Promise<T> {
const options: RequestInit & { next?: { revalidate?: number } } = revalidateSeconds
? { cache: 'force-cache', next: { revalidate: revalidateSeconds } }
: { cache: 'no-store' };
// Thrown instead of calling Finnhub while an endpoint is in its failure cool-down; not worth logging again.
class RecentFailure extends Error {}
const logFailure = (what: string, symbol: string, e: unknown) => {
if (!(e instanceof RecentFailure)) console.error(`Error fetching ${what} for`, symbol, e);
};
const res = await fetch(url, options);
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(`Fetch failed ${res.status}: ${text}`);
let activeRequests = 0;
const waitingForSlot: (() => void)[] = [];
const inFlight = new Map<string, Promise<unknown>>();
const responseCache = new Map<string, { value: unknown; freshUntil: number; failed?: boolean }>();
async function withSlot<T>(run: () => Promise<T>): Promise<T> {
if (activeRequests >= MAX_IN_FLIGHT_PER_KEY * Math.max(1, FINNHUB_KEYS.length)) await new Promise<void>((resolve) => waitingForSlot.push(resolve));
activeRequests++;
try {
return await run();
} finally {
activeRequests--;
waitingForSlot.shift()?.();
}
return (await res.json()) as T;
}
export { fetchJSON };
// The token travels in a header, so cache keys are the same whichever key served them.
// On 429 the next key in the pool gets a turn.
async function fetchWithKeyPool(url: string, ttl: number) {
if (FINNHUB_KEYS.length === 0) throw new Error('No Finnhub API key configured (FINNHUB_API_KEYS)');
let res: Response | null = null;
for (let attempt = 0; attempt < FINNHUB_KEYS.length; attempt++) {
const key = FINNHUB_KEYS[nextKeyIndex++ % FINNHUB_KEYS.length];
res = await fetch(url, {
headers: { 'X-Finnhub-Token': key },
// Without a timeout a stalled Finnhub socket holds the page for Node's 300s default
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
...(ttl >= SHARED_CACHE_MIN_TTL ? { next: { revalidate: ttl } } : { cache: 'no-store' as const }),
});
if (res.status !== 429) break;
}
return res!;
}
// One network call per URL at a time, however many pages ask for it
function request<T>(url: string, ttl = 0): Promise<T> {
let pending = inFlight.get(url) as Promise<T> | undefined;
if (!pending) {
pending = withSlot(async () => {
const res = await fetchWithKeyPool(url, ttl);
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(`Fetch failed ${res.status}: ${text}`);
}
return (await res.json()) as T;
}).finally(() => inFlight.delete(url));
inFlight.set(url, pending);
}
return pending;
}
// Stale-while-revalidate: after the first fetch nobody waits on Finnhub; expired entries
// are served instantly and refreshed in the background.
async function fetchJSON<T>(url: string, revalidateSeconds = 0): Promise<T> {
if (!revalidateSeconds) return request<T>(url);
const store = (entry: { value: unknown; freshUntil: number; failed?: boolean }) => {
if (responseCache.size >= MAX_CACHE_ENTRIES) responseCache.delete(responseCache.keys().next().value!);
responseCache.set(url, entry);
};
const refresh = () => request<T>(url, revalidateSeconds).then(
(value) => {
store({ value, freshUntil: Date.now() + revalidateSeconds * 1000 });
return value;
},
(error) => {
// A stalling endpoint fails fast for a minute instead of costing every visitor the full timeout
if (!responseCache.get(url) || responseCache.get(url)!.failed) {
store({ value: null, freshUntil: Date.now() + FAILURE_TTL_MS, failed: true });
}
throw error;
},
);
const hit = responseCache.get(url);
if (!hit || (hit.failed && hit.freshUntil < Date.now())) return refresh();
if (hit.failed) throw new RecentFailure(`Finnhub request failed recently: ${url.split('?')[0]}`);
if (hit.freshUntil < Date.now()) refresh().catch(() => { /* keep serving the stale value */ });
return hit.value as T;
}
function getExchangeLabel(symbol: string, exchange?: string) {
if (exchange?.trim()) {
@@ -63,26 +159,33 @@ function getExchangeLabel(symbol: string, exchange?: string) {
return FINNHUB_EXCHANGE_SUFFIXES.has(suffix) ? suffix : 'US';
}
export async function getQuote(symbol: string) {
export async function getQuote(symbol: string, revalidateSeconds = 0) {
try {
const token = NEXT_PUBLIC_FINNHUB_API_KEY;
const url = `${FINNHUB_BASE_URL}/quote?symbol=${encodeURIComponent(symbol)}&token=${token}`;
// No caching for real-time price
return await fetchJSON<FinnhubQuote>(url, 0);
const url = `${FINNHUB_BASE_URL}/quote?symbol=${encodeURIComponent(symbol)}`;
// Real-time by default; shared views pass a short cache to stay inside Finnhub's 60 req/min
return await fetchJSON<FinnhubQuote>(url, revalidateSeconds);
} catch (e) {
console.error('Error fetching quote for', symbol, e);
logFailure('quote', symbol, e);
return null;
}
}
// Shared, short-lived quotes for anything shown live on screen (pages and /api/quotes)
export async function getLiveQuotes(symbols: string[]) {
// Symbols the free plan can't price are skipped instead of spending a request on a 403
const quotes = await Promise.all(symbols.map((s) => (hasFinnhubQuotes(s) ? getQuote(s, LIVE_QUOTE_TTL) : null)));
return Object.fromEntries(symbols.map((s, i) => [s, quotes[i]?.c ? quotes[i] : null]));
}
export async function getCompanyProfile(symbol: string) {
// Profiles exist for stocks Finnhub free covers; crypto pairs and non-US listings have none
if (symbol.includes(':') || !hasFinnhubQuotes(symbol)) return null;
try {
const token = NEXT_PUBLIC_FINNHUB_API_KEY;
const url = `${FINNHUB_BASE_URL}/stock/profile2?symbol=${encodeURIComponent(symbol)}&token=${token}`;
const url = `${FINNHUB_BASE_URL}/stock/profile2?symbol=${encodeURIComponent(symbol)}`;
// Cache profile for 24 hours
return await fetchJSON<FinnhubCompanyProfile>(url, 86400);
} catch (e) {
console.error('Error fetching profile for', symbol, e);
logFailure('profile', symbol, e);
return null;
}
}
@@ -92,10 +195,10 @@ export async function getWatchlistData(symbols: string[]) {
// Fetch quotes and profiles in parallel
const promises = symbols.map(async (sym) => {
const [quote, profile] = await Promise.all([
getQuote(sym),
getCompanyProfile(sym)
]);
const covered = hasFinnhubQuotes(sym);
const [quote, profile] = covered
? await Promise.all([getQuote(sym, LIVE_QUOTE_TTL), getCompanyProfile(sym)])
: [null, null];
return {
symbol: sym,
@@ -117,8 +220,7 @@ export async function getWatchlistData(symbols: string[]) {
export async function getNews(symbols?: string[]): Promise<MarketNewsArticle[]> {
try {
const range = getDateRange(5);
const token = NEXT_PUBLIC_FINNHUB_API_KEY;
if (!token) {
if (FINNHUB_KEYS.length === 0) {
throw new Error('FINNHUB API key is not configured');
}
const cleanSymbols = (symbols || [])
@@ -134,7 +236,7 @@ export async function getNews(symbols?: string[]): Promise<MarketNewsArticle[]>
await Promise.all(
cleanSymbols.map(async (sym) => {
try {
const url = `${FINNHUB_BASE_URL}/company-news?symbol=${encodeURIComponent(sym)}&from=${range.from}&to=${range.to}&token=${token}`;
const url = `${FINNHUB_BASE_URL}/company-news?symbol=${encodeURIComponent(sym)}&from=${range.from}&to=${range.to}`;
const articles = await fetchJSON<RawNewsArticle[]>(url, 300);
perSymbolArticles[sym] = (articles || []).filter(validateArticle);
} catch (e) {
@@ -168,7 +270,7 @@ export async function getNews(symbols?: string[]): Promise<MarketNewsArticle[]>
}
// General market news fallback or when no symbols provided
const generalUrl = `${FINNHUB_BASE_URL}/news?category=general&token=${token}`;
const generalUrl = `${FINNHUB_BASE_URL}/news?category=general`;
const general = await fetchJSON<RawNewsArticle[]>(generalUrl, 300);
const seen = new Set<string>();
@@ -192,8 +294,7 @@ export async function getNews(symbols?: string[]): Promise<MarketNewsArticle[]>
export const searchStocks = cache(async (query?: string): Promise<StockWithWatchlistStatus[]> => {
try {
const token = NEXT_PUBLIC_FINNHUB_API_KEY;
if (!token) {
if (FINNHUB_KEYS.length === 0) {
// If no token, log and return empty to avoid throwing per requirements
console.error('Error in stock search:', new Error('FINNHUB API key is not configured'));
return [];
@@ -204,40 +305,16 @@ export const searchStocks = cache(async (query?: string): Promise<StockWithWatch
let results: SearchStockCandidate[] = [];
if (!trimmed) {
// Fetch top 10 popular symbols' profiles
const top = POPULAR_STOCK_SYMBOLS.slice(0, 10);
const profiles = await Promise.all(
top.map(async (sym) => {
try {
const url = `${FINNHUB_BASE_URL}/stock/profile2?symbol=${encodeURIComponent(sym)}&token=${token}`;
// Revalidate every hour
const profile = await fetchJSON<FinnhubCompanyProfile>(url, 3600);
return { sym, profile } as { sym: string; profile: FinnhubCompanyProfile | null };
} catch (e) {
console.error('Error fetching profile2 for', sym, e);
return { sym, profile: null } as { sym: string; profile: FinnhubCompanyProfile | null };
}
})
);
results = profiles
.map(({ sym, profile }) => {
const symbol = sym.toUpperCase();
const name: string | undefined = profile?.name || profile?.ticker || undefined;
const exchange: string | undefined = profile?.exchange || undefined;
if (!name) return undefined;
const r: SearchStockCandidate = {
symbol,
description: name,
displaySymbol: symbol,
type: 'Common Stock',
};
r.__exchange = exchange;
return r;
})
.filter((x): x is SearchStockCandidate => Boolean(x));
// Shown in the palette on every page, so no API calls: Finnhub's free tier is 60 req/min
results = POPULAR_STOCKS.map(({ symbol, name, exchange }) => ({
symbol,
description: name,
displaySymbol: symbol,
type: 'Common Stock',
__exchange: exchange,
}));
} else {
const url = `${FINNHUB_BASE_URL}/search?q=${encodeURIComponent(trimmed)}&token=${token}`;
const url = `${FINNHUB_BASE_URL}/search?q=${encodeURIComponent(trimmed)}`;
const data = await fetchJSON<FinnhubSearchResponse>(url, 1800);
results = Array.isArray(data?.result) ? data.result : [];
}

11
lib/market-data.ts Normal file
View File

@@ -0,0 +1,11 @@
// How fresh market numbers are. Safe to import from client components (no keys here).
//
// cached (default) Public instance: quotes refresh hourly, shared by every user via the data cache.
// realtime OpenStock Cloud or self-hosted with your own Finnhub keys: quotes refresh every 15s.
export type DataMode = 'cached' | 'realtime';
export const DATA_MODE: DataMode = process.env.NEXT_PUBLIC_OPENSTOCK_DATA_MODE === 'realtime' ? 'realtime' : 'cached';
export const QUOTE_TTL_SECONDS = DATA_MODE === 'realtime' ? 15 : 3600;
export const isRealtime = DATA_MODE === 'realtime';