Make the JSON endpoint quick

This commit is contained in:
Marco van Dijk 2024-12-01 13:02:31 +01:00
parent 8a994ba272
commit 24bd005f04

View File

@ -87,6 +87,8 @@ function sleep(ms) {
let ensDomainCache = {}; let ensDomainCache = {};
let orchCache = {}; let orchCache = {};
let lastStringify = 0; //< We will stringify every 15 seconds so the the JSON endpoint can respond quickly
let jsonString = "";
let lastLeaderboardCheck = 0; let lastLeaderboardCheck = 0;
let isSynced = false; let isSynced = false;
@ -125,11 +127,11 @@ const getEnsDomain = async function (addr) {
} }
console.log( console.log(
"Updated ENS domain " + "Updated ENS domain " +
ensObj.domain + ensObj.domain +
" owned by " + " owned by " +
ensObj.address + ensObj.address +
" @ " + " @ " +
ensObj.timestamp ensObj.timestamp
); );
ensDomainCache[addr] = ensObj; ensDomainCache[addr] = ensObj;
await storage.setItem("ensDomainCache", ensDomainCache); await storage.setItem("ensDomainCache", ensDomainCache);
@ -312,9 +314,9 @@ const onOrchUpdate = async function (id, obj, tag, region, livepeer_regions) {
) { ) {
console.log( console.log(
"Removing expired key " + "Removing expired key " +
key + key +
" from the probed-from cache for orch " + " from the probed-from cache for orch " +
id id
); );
delete newInstance.probedFrom[key]; delete newInstance.probedFrom[key];
} }
@ -339,9 +341,9 @@ const onOrchUpdate = async function (id, obj, tag, region, livepeer_regions) {
) { ) {
console.log( console.log(
"Removing expired key " + "Removing expired key " +
key + key +
" from the livepeer regions cache for orch " + " from the livepeer regions cache for orch " +
id id
); );
delete newInstance.livepeer_regions[key]; delete newInstance.livepeer_regions[key];
} }
@ -446,7 +448,7 @@ masterRouter.get("/prometheus", async (req, res) => {
masterRouter.get("/json", async (req, res) => { masterRouter.get("/json", async (req, res) => {
try { try {
res.set("Content-Type", "application/json"); res.set("Content-Type", "application/json");
res.end(JSON.stringify(orchCache)); res.end(jsonString);
} catch (err) { } catch (err) {
res.status(400).send(err); res.status(400).send(err);
} }
@ -487,11 +489,11 @@ const updateScore = async function (address) {
} }
console.log( console.log(
"Found new RTR=" + "Found new RTR=" +
newRTR + newRTR +
" and new success rate of " + " and new success rate of " +
newSR * 100 + newSR * 100 +
"%, livepeer region " + "%, livepeer region " +
instance.region instance.region
); );
promLatestRTR.set( promLatestRTR.set(
{ {
@ -571,13 +573,13 @@ const recoverStorage = async function () {
} }
console.log( console.log(
"Re-init leaderboard scores for orch=" + "Re-init leaderboard scores for orch=" +
id + id +
", RTR=" + ", RTR=" +
res.latestRTR + res.latestRTR +
" and success rate of " + " and success rate of " +
res.latestSR * 100 + res.latestSR * 100 +
"%, livepeer region " + "%, livepeer region " +
region region
); );
let latitude = null; let latitude = null;
let longitude = null; let longitude = null;
@ -614,16 +616,40 @@ const recoverStorage = async function () {
}; };
recoverStorage(); recoverStorage();
// Strip individual measurements from the cache to keep the response tiny
function shallowCopy() {
const mrClean = {};
for (const orchestratorId in orchCache) {
const orchestrator = orchCache[orchestratorId];
// Shallow copy (which references original memory pointers)
mrClean[orchestratorId] = { ...orchestrator };
// Overwrite regionalStats ref
if (orchestrator.regionalStats) {
mrClean[orchestratorId].regionalStats = {};
for (const region in orchestrator.regionalStats) {
const regionStats = orchestrator.regionalStats[region];
// Shallow copy region stats without measurements
mrClean[orchestratorId].regionalStats[region] = { ...regionStats };
delete mrClean[orchestratorId].regionalStats[region].measurements;
}
}
}
return JSON.stringify(mrClean);
}
const runTests = async function () { const runTests = async function () {
try { try {
const now = new Date().getTime(); const now = new Date().getTime();
if ( if (!lastLeaderboardCheck || now - lastLeaderboardCheck > CONF_SCORE_TIMEOUT) {
!lastLeaderboardCheck ||
now - lastLeaderboardCheck > CONF_SCORE_TIMEOUT
) {
await updateOrchScores(); await updateOrchScores();
lastLeaderboardCheck = now; lastLeaderboardCheck = now;
} }
if (!lastStringify || now - lastStringify > 15000) {
jsonString = shallowCopy();
lastStringify = now;
}
setTimeout(() => { setTimeout(() => {
runTests(); runTests();
}, CONF_SLEEPTIME); }, CONF_SLEEPTIME);