Give the menu reserve route its own request limit
Every page reads the reserve for the Sokko TCG menu count, and those reads shared the game API limit of 60 requests per minute and per address. Browser tests reloading many pages from one address got 429 responses. The route now has its own limit, like the public configuration, and focus or tab returns read it at most once every 15 seconds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0723d97788
commit
4db6489760
3 changed files with 26 additions and 9 deletions
|
|
@ -19,8 +19,14 @@ export function publishCardsReserve(recharge) {
|
|||
for (const listener of listeners) listener();
|
||||
}
|
||||
|
||||
async function refreshReserve() {
|
||||
// Le focus et le retour sur l'onglet arrivent souvent ensemble : une relecture par période suffit.
|
||||
const REFRESH_INTERVAL_MS = 15_000;
|
||||
let lastRefreshAt = 0;
|
||||
|
||||
async function refreshReserve({ force = false } = {}) {
|
||||
if (globalThis.document.visibilityState === "hidden") return;
|
||||
if (!force && Date.now() - lastRefreshAt < REFRESH_INTERVAL_MS) return;
|
||||
lastRefreshAt = Date.now();
|
||||
try {
|
||||
publishCardsReserve((await loadCardsReserve()).recharge);
|
||||
} catch {
|
||||
|
|
@ -35,7 +41,7 @@ export function useFullCardsReserve(enabled) {
|
|||
|
||||
useEffect(() => {
|
||||
if (!enabled) return undefined;
|
||||
void refreshReserve();
|
||||
void refreshReserve({ force: true });
|
||||
window.addEventListener("focus", refreshReserve);
|
||||
globalThis.document.addEventListener("visibilitychange", refreshReserve);
|
||||
return () => {
|
||||
|
|
@ -46,7 +52,7 @@ export function useFullCardsReserve(enabled) {
|
|||
|
||||
useEffect(() => {
|
||||
if (!enabled || !nextOpeningAt) return undefined;
|
||||
const timeout = window.setTimeout(refreshReserve, Math.max(0, nextOpeningAt - Date.now()) + 250);
|
||||
const timeout = window.setTimeout(() => refreshReserve({ force: true }), Math.max(0, nextOpeningAt - Date.now()) + 250);
|
||||
return () => window.clearTimeout(timeout);
|
||||
}, [enabled, nextOpeningAt]);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue