perf - Cache settings on router.js

This commit is contained in:
Rafael Taranto 2019-10-01 11:09:36 +01:00 committed by Josh Harvey
parent 20f3cf14e9
commit d083ae5a40

View file

@ -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)
} }