Merge cc into dd (#317)

* Fix ETH fee fetching (#303)

* v7.3.3 (#304)

* Throttle clock skew and fetch logs

* PR Fixes

* Reduce throttle time

* Remove old endpoint

* Fix standard styles and improve readability

* Simplify throttling

* Small fixes

* Bump version
This commit is contained in:
Rafael Taranto 2019-09-20 12:33:00 +01:00 committed by Josh Harvey
parent 2e675f738f
commit 1153c6af6a
3 changed files with 28 additions and 17 deletions

View file

@ -26,10 +26,14 @@ const argv = require('minimist')(process.argv.slice(2))
const CLOCK_SKEW = 60 * 1000
const REQUEST_TTL = 3 * 60 * 1000
const THROTTLE_LOGS_QUERY = 30 * 1000
const THROTTLE_CLOCK_SKEW = 60 * 1000
const pids = {}
const reboots = {}
const restartServicesMap = {}
const canGetLastSeenMap = {}
const canLogClockSkewMap = {}
const devMode = argv.dev || options.http
@ -167,13 +171,6 @@ function stateChange (req, res, next) {
.catch(next)
}
function deviceEvent (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
pi.logEvent(req.body)
.then(() => respond(req, res))
.catch(next)
}
function verifyUser (req, res, next) {
const pi = plugins(req.settings, req.deviceId)
pi.verifyUser(req.body)
@ -245,9 +242,19 @@ function updateCustomer (req, res, next) {
}
function getLastSeen (req, res, next) {
return logs.getLastSeen(req.deviceId)
.then(r => res.json(r))
.catch(next)
const deviceId = req.deviceId
const timestamp = Date.now()
const shouldTrigger = !canGetLastSeenMap[deviceId] ||
timestamp - canGetLastSeenMap[deviceId] >= THROTTLE_LOGS_QUERY
if (shouldTrigger) {
canGetLastSeenMap[deviceId] = timestamp
return logs.getLastSeen(deviceId)
.then(r => res.json(r))
.catch(next)
}
return res.status(408).json({})
}
function updateLogs (req, res, next) {
@ -310,9 +317,15 @@ function httpError (msg, code) {
function filterOldRequests (req, res, next) {
const deviceTime = req.deviceTime
const delta = Date.now() - Date.parse(deviceTime)
const deviceId = req.deviceId
const timestamp = Date.now()
const delta = timestamp - Date.parse(deviceTime)
if (delta > CLOCK_SKEW) {
const shouldTrigger = !canLogClockSkewMap[deviceId] ||
timestamp - canLogClockSkewMap[deviceId] >= THROTTLE_CLOCK_SKEW
if (delta > CLOCK_SKEW && shouldTrigger) {
canLogClockSkewMap[deviceId] = timestamp
logger.error('Clock skew with lamassu-machine[%s] too high [%ss], adjust lamassu-machine clock',
req.deviceName, (delta / 1000).toFixed(2))
}
@ -337,8 +350,7 @@ function authorize (req, res, next) {
.catch(next)
}
const skip = (req, res) => _.includes(req.path, ['/poll', '/state', '/logs']) &&
res.statusCode === 200
const skip = (req, res) => _.includes(req.path, ['/poll', '/state', '/logs']) && _.includes(res.statusCode, [200, 408])
const configRequiredRoutes = [
'/poll',
@ -367,7 +379,6 @@ app.use(filterOldRequests)
app.get('/poll', poll)
app.post('/state', stateChange)
app.post('/event', deviceEvent)
app.post('/verify_user', verifyUser)
app.post('/verify_transaction', verifyTx)