-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #214
- Loading branch information
Konstantin Kalinovsky
committed
Dec 29, 2019
1 parent
deb8ed7
commit b68bdf0
Showing
4 changed files
with
48 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
let cache = {}; | ||
const request = require("request"); | ||
const cache = {}; | ||
let isFetching = false; | ||
|
||
const PLAYER_COUNT_UPDATE_INTERVAL = parseInt(process.env.PLAYER_COUNT_UPDATE_INTERVAL) * 1000; | ||
|
||
exports = module.exports = function(req, res) { | ||
const resource = req.query.resource; | ||
if (cache[resource]){ | ||
if (isFetching || Date.now() - cache[resource].pollTime < parseInt(process.env.PLAYER_COUNT_UPDATE_INTERVAL) * 1000){ | ||
return res.send(cache[resource].data); | ||
if (cache[resource]) { | ||
if (isFetching || Date.now() - cache[resource].pollTime < PLAYER_COUNT_UPDATE_INTERVAL) { | ||
return res.send(cache[resource].count.toString()); | ||
} | ||
} | ||
isFetching = true; | ||
const request = require('request'); | ||
request(process.env.LOBBY_API_URL + '/' + resource, function (error, response, body) { | ||
request(process.env.LOBBY_API_URL + "/" + resource, function (error, response, body) { | ||
const data = JSON.parse(body); | ||
cache[resource] = { | ||
pollTime: Date.now(), | ||
data: data | ||
count: data.length | ||
}; | ||
isFetching = false; | ||
return res.send(data); | ||
return res.send(cache[resource].count.toString()); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters