Skip to content

Commit

Permalink
Caching MPV properties and sending cached property list if the CPU us…
Browse files Browse the repository at this point in the history
…age high on the host machine
  • Loading branch information
husudosu committed Jun 16, 2022
1 parent 6ba43f4 commit 79178b1
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions remoteServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const collections = require("./collections");
const { detectFileType } = require("./filebrowser");
const { loadSettings, settings, CORSOPTIONS } = require("./settings");
const { version } = require("./package.json");
// Returning cached properties if the CPU usage high.
let cachedProps = {};

const argv = yargs
.option("webport", {
Expand Down Expand Up @@ -95,12 +97,37 @@ function stringIsAValidUrl(s) {
}
}

// Thanks: https://javascript.plainenglish.io/how-to-add-a-timeout-limit-to-asynchronous-javascript-functions-3676d89c186d
const asyncCallWithTimeout = async (asyncPromise, timeLimit) => {
let timeoutHandle;

const timeoutPromise = new Promise((_resolve, reject) => {
timeoutHandle = setTimeout(
() => reject(new Error("Async call timeout limit reached")),
timeLimit
);
});

return Promise.race([asyncPromise, timeoutPromise]).then((result) => {
clearTimeout(timeoutHandle);
return result;
});
};

app.get("/api/v1/status", async (req, res) => {
setTimeout(async () => {});
try {
return res.json(await getMPVProps(req.query.exclude));
const result = await asyncCallWithTimeout(
getMPVProps(req.query.exclude),
500
);
// Returning cached properties if the CPU usage high.
cachedProps = Object.assign(cachedProps, result);
return res.json(result);
} catch (exc) {
console.log(exc);
return res.status(500).json({ message: exc });
if (exc.message == "Async call timeout limit reached")
return res.json(cachedProps);
else return res.status(500).json({ message: exc });
}
});

Expand Down Expand Up @@ -772,9 +799,9 @@ async function getMPVProps(exclude = []) {
retval[key] = val;
}
}

return retval;
}

portfinder
.getPortPromise({
port: settings.serverPort,
Expand Down

0 comments on commit 79178b1

Please sign in to comment.