perf - Cache settings on router.js
This commit is contained in:
parent
20f3cf14e9
commit
d083ae5a40
1 changed files with 19 additions and 2 deletions
|
|
@ -28,12 +28,14 @@ const CLOCK_SKEW = 60 * 1000
|
||||||
const REQUEST_TTL = 3 * 60 * 1000
|
const REQUEST_TTL = 3 * 60 * 1000
|
||||||
const THROTTLE_LOGS_QUERY = 30 * 1000
|
const THROTTLE_LOGS_QUERY = 30 * 1000
|
||||||
const THROTTLE_CLOCK_SKEW = 60 * 1000
|
const THROTTLE_CLOCK_SKEW = 60 * 1000
|
||||||
|
const SETTINGS_CACHE_REFRESH = 60 * 60 * 1000
|
||||||
|
|
||||||
const pids = {}
|
const pids = {}
|
||||||
const reboots = {}
|
const reboots = {}
|
||||||
const restartServicesMap = {}
|
const restartServicesMap = {}
|
||||||
const canGetLastSeenMap = {}
|
const canGetLastSeenMap = {}
|
||||||
const canLogClockSkewMap = {}
|
const canLogClockSkewMap = {}
|
||||||
|
const settingsCache = {}
|
||||||
|
|
||||||
const devMode = argv.dev || options.http
|
const devMode = argv.dev || options.http
|
||||||
|
|
||||||
|
|
@ -427,6 +429,7 @@ localApp.post('/restartServices', (req, res) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
localApp.post('/dbChange', (req, res, next) => {
|
localApp.post('/dbChange', (req, res, next) => {
|
||||||
|
settingsCache.cache = null
|
||||||
return settingsLoader.loadLatest()
|
return settingsLoader.loadLatest()
|
||||||
.then(poller.reload)
|
.then(poller.reload)
|
||||||
.then(() => logger.info('Config reloaded'))
|
.then(() => logger.info('Config reloaded'))
|
||||||
|
|
@ -463,9 +466,23 @@ function populateSettings (req, res, next) {
|
||||||
oldVersionId = versionId
|
oldVersionId = versionId
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!versionId) {
|
// Clear cache every hour
|
||||||
|
if (Date.now() - settingsCache.timestamp > SETTINGS_CACHE_REFRESH) {
|
||||||
|
settingsCache.cache = null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!versionId && settingsCache.cache) {
|
||||||
|
req.settings = settingsCache.cache
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!versionId && !settingsCache.cache) {
|
||||||
return settingsLoader.loadLatest()
|
return settingsLoader.loadLatest()
|
||||||
.then(settings => { req.settings = settings })
|
.then(settings => {
|
||||||
|
settingsCache.cache = settings
|
||||||
|
settingsCache.timestamp = Date.now()
|
||||||
|
req.settings = settings
|
||||||
|
})
|
||||||
.then(() => next())
|
.then(() => next())
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue