fix(server): stop track backfill from spending the OpenSky credit budget

`/api/opensky-track` costs 4 OpenSky credits per call against the same
~4000/day account as `/api/opensky`, but participated in none of that
proxy's credit governor. Its 60-second per-icao cache bounds memory, not
spend: `icao24` is six hex digits, so a caller that varies it produces a
fresh key and a fresh upstream call every time.

Both track-backfill routes now carry an always-on 30/min per-client
limiter (90 global) — the band `/api/geocode` and `/api/regional-brief`
already use, and far above the real demand, which is one backfill per
aircraft the operator selects.

`/api/opensky-track` additionally reads the 429 cooldown `/api/opensky`
already maintains. While the account is out of budget it serves any
cached track and otherwise answers 429 with the remaining cooldown,
instead of minting a token and spending four more credits. adsb.lol is
free and unmetered, so only the limiter applies there.
This commit is contained in:
daikaginza committed 2026-09-27 23:01:01 +07:00
1 parent b210ab0fe4
commit 65018015a0
7 files changed
+313 -6

No files matched your search

+15
View File
@@ -41,6 +41,21 @@ const OPENSKY_CACHE_MS = 9000;
let _openskyTtlMs = OPENSKY_CACHE_MS;
/** @type {number} Epoch-ms before which no upstream fetch is attempted. */
let _openskyCooldownUntil = 0;
/**
* Milliseconds left on the 429 cooldown, or 0 when upstream may be called.
*
* Read-only view of lever 2 above, exported so the OTHER routes that spend the
* same daily credit budget can honor it. `/api/opensky-track` costs 4 credits
* per call against this account just as `/states/all` does, so minting one
* while this proxy is cooling would spend budget the account has already been
* told it does not have. Only this module writes the cooldown.
*
* @param {number} [now] - Epoch-ms; injectable so tests need no clock control.
* @returns {number} Remaining cooldown in ms (0 when clear).
*/
export function openSkyCooldownRemainingMs(now = Date.now()) {
return Math.max(0, _openskyCooldownUntil - now);
}
/**
* Picks the cache TTL from the remaining daily credit budget.
* Client polls every 30 s, so tiers ≤30 s cost the same 480 credits/h; the