Added caching to /api/slaves (default 5s)

This commit is contained in:
Danielv123 committed 2018-05-06 14:30:03 +02:00
1 parent 6dd25cb1e8
commit cf1a97ac9f
1 file changed
+13 -5
+13 -5
View File
@@ -373,17 +373,25 @@ GET endpoint for getting information about all our slaves
@instance @instance
@alias /api/slaves @alias /api/slaves
*/ */
let slaveCache = {
timestamp: Date.now(),
};
app.get("/api/slaves", function(req, res) { app.get("/api/slaves", function(req, res) {
endpointHitCounter.labels(req.route.path).inc(); endpointHitCounter.labels(req.route.path).inc();
let reqStartTime = Date.now(); let reqStartTime = Date.now();
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
let copyOfSlaves = JSON.parse(JSON.stringify(slaves)); if(!slaveCache.cache || Date.now() - slaveCache.timestamp > 5000){
// filter out the rcon password because thats kindof not a safe thing to share let copyOfSlaves = JSON.parse(JSON.stringify(slaves));
for(key in copyOfSlaves){ // filter out the rcon password because thats kindof not a safe thing to share
copyOfSlaves[key].rconPassword = "hidden"; for(key in copyOfSlaves){
copyOfSlaves[key].rconPassword = "hidden";
}
slaveCache.cache = copyOfSlaves;
slaveCache.timestamp = Date.now();
} }
res.send(copyOfSlaves);
res.send(slaveCache.cache);
httpRequestDurationMilliseconds.labels(req.route.path).observe(Date.now()-reqStartTime); httpRequestDurationMilliseconds.labels(req.route.path).observe(Date.now()-reqStartTime);
}); });
var recievedItemStatisticsBySlaveID = {}; var recievedItemStatisticsBySlaveID = {};