fix pairing bug; remove cachedResponses

This commit is contained in:
Josh Harvey 2016-11-19 15:51:26 +02:00
parent 5a8e5627ef
commit c9af2fe5b3
4 changed files with 3 additions and 48 deletions

View file

@ -7,7 +7,6 @@ const pgp = require('pg-promise')()
const logger = require('./logger')
const CACHED_SESSION_TTL = 60 * 60 * 1000
const LIVE_SWEEP_TTL = 48 * 60 * 60 * 1000
function isUniqueViolation (err) {
@ -382,47 +381,6 @@ exports.updateNotify = function updateNotify (tx) {
})
}
function insertCachedRequest (deviceId, txId, path, method, body) {
const fields = [
'device_id',
'tx_id',
'path',
'method',
'body'
]
const sql = getInsertQuery('cached_responses', fields)
return db.none(sql, [deviceId, txId, path, method, body])
}
exports.cachedResponse = function (deviceId, txId, path, method) {
const sql = `select body from cached_responses
where device_id=$1
and tx_id=$2
and path=$3
and method=$4`
const values = [deviceId, txId, path, method]
return insertCachedRequest(deviceId, txId, path, method, {pendingRequest: true})
.then(() => ({}))
.catch(err => {
if (!isUniqueViolation(err)) throw err
console.log('DEBUG22: %j', err)
return db.one(sql, values)
.then(row => ({body: row.body}))
})
}
function pruneCachedResponses () {
const sql = `delete from cached_responses
where (EXTRACT(EPOCH FROM (now() - created))) * 1000 < $1`
const values = [CACHED_SESSION_TTL]
return db.none(sql, values)
}
exports.cacheResponse = function (deviceId, txId, path, method, body) {
const sql = `update cached_responses
set body=$1
@ -479,5 +437,3 @@ exports.markSwept = function markSwept (txId) {
return db.none(sql, [true, txId])
}
setInterval(pruneCachedResponses, CACHED_SESSION_TTL)