Speed: catalog reads run in parallel and /api/catalog serves the cached ladder while refreshing in the background (was ~6 s of sequential chain reads on every 20 s miss); position rows read the chain side by side
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -162,15 +162,16 @@ async function product(id) {
|
||||
async function quoteWei(id) { try { return toBig(word(await call(SEL.quoteWei, [id]), 0)); } catch (e) { return null; } }
|
||||
async function creditBalance(id, type) { return Number(toBig(word(await call(SEL.creditBalance, [id, type || 0]), 0))); }
|
||||
async function catalog() {
|
||||
// products and quotes are read side by side, not one after another (2026-10-19: 15 sequential reads took ~6 s
|
||||
// and every dashboard waited on them)
|
||||
const n = await productCount();
|
||||
const out = [];
|
||||
for (let i = 1; i <= n; i++) {
|
||||
const p = await product(i);
|
||||
if (!p.exists || !p.active) continue;
|
||||
const q = await quoteWei(i);
|
||||
out.push(Object.assign(p, { costWei: q === null ? null : q.toString() }));
|
||||
}
|
||||
return out;
|
||||
const ids = Array.from({ length: n }, (_, k) => k + 1);
|
||||
const rows = await Promise.all(ids.map(async i => {
|
||||
const [p, q] = await Promise.all([product(i), quoteWei(i)]);
|
||||
if (!p.exists || !p.active) return null;
|
||||
return Object.assign(p, { costWei: q === null ? null : q.toString() });
|
||||
}));
|
||||
return rows.filter(Boolean);
|
||||
}
|
||||
|
||||
// ---- event decode ----
|
||||
|
||||
@@ -2053,9 +2053,16 @@ 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-19): serve the cached ladder at once and refresh it in the background after
|
||||
// 20 s, one refresh at a time; only wait for a fresh read when there is none or it is over 2 minutes old
|
||||
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 });
|
||||
}
|
||||
@@ -2642,17 +2649,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-19: one after another took 3-8 s for members with several)
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user