Speed (same fixes as InstantAdPay): catalog reads in parallel + stale-while-revalidate /api/catalog, positions read side by side, member reads cached 15 s (cleared on every new chain event)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-20 05:21:48 -05:00
parent a9994e708d
commit 693a539503
2 changed files with 29 additions and 18 deletions
+11 -9
View File
@@ -1282,10 +1282,10 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/catalog' && req.method === 'GET') {
// five live quotes per call: serve a 20-second cache so pages that load the
// ladder (join, home, buy) are not waiting on the RPC every time
if (!catalogCache.at || Date.now() - catalogCache.at > 20000) {
try { catalogCache.products = await chain.catalog(); catalogCache.at = Date.now(); }
catch (e) { if (!catalogCache.products) throw e; }
}
// stale-while-revalidate (2026-10-20, same as InstantAdPay): serve the cached ladder at once, refresh in the background
const age = catalogCache.at ? Date.now() - catalogCache.at : Infinity;
if (age > 20000 && !catalogCache.busy) catalogCache.busy = chain.catalog().then(pr => { catalogCache.products = pr; catalogCache.at = Date.now(); }).catch(e => console.error('catalog refresh', e.message)).finally(() => { catalogCache.busy = null; });
if (!catalogCache.products || age > 120000) { try { await catalogCache.busy; } catch (e) {} if (!catalogCache.products) throw new Error('catalog unavailable'); }
return json(res, 200, { products: catalogCache.products });
}
if (p === '/api/feed' && req.method === 'GET') {
@@ -1792,17 +1792,19 @@ const server = http.createServer(async (req, res) => {
const out = [];
const posIds = [mainId, ...list.map(p => p.memberId)].filter(Boolean);
let balPer = {}, credited = 0; try { const bb = await ads.balances(posIds, s.email); credited = bb.credited; for (const p of bb.per) balPer[p.memberId] = p.avail; } catch (e) {}
for (const pos of list) {
// every position's chain reads run side by side (2026-10-20: one after another took 3-8 s, same fix as InstantAdPay)
out.push(...await Promise.all(list.map(async pos => {
let id = pos.memberId;
if (!id) { try { id = await chain.memberIdByAccount(pos.address); if (id) await accounts.setPositionMember(pos.address, id); } catch (e) {} }
const row = { address: pos.address, memberId: id || 0, buyerCount: 0, counted: false, credits: 0, created: pos.created };
if (id) {
try { const mm = await chain.member(id); row.buyerCount = mm.buyerCount; row.counted = mm.countedAsBuyer; row.sponsorId = mm.sponsorId; } catch (e) {}
const [mm, blocked] = await Promise.all([chain.member(id).catch(() => null), payoutChainBlocked(id, 3).catch(() => false)]);
if (mm) { row.buyerCount = mm.buyerCount; row.counted = mm.countedAsBuyer; row.sponsorId = mm.sponsorId; }
row.credits = balPer[id] != null ? balPer[id] : 0;
row.noPayout = await payoutChainBlocked(id, 3); // linkage only: a buy from here would pay a listed wallet
row.noPayout = blocked; // linkage only: a buy from here would pay a listed wallet
}
out.push(row);
}
return row;
})));
const main = { address: (acct && acct.address) || null, memberId: mainId, credits: 0, buyerCount: 0 };
if (mainId) {
main.credits = balPer[mainId] != null ? balPer[mainId] : 0;