Option to track quota for analysis

This commit is contained in:
Cadence Ember
2020-07-23 01:20:06 +12:00
parent 112d9cc90e
commit f7c4ae19f4
3 changed files with 32 additions and 8 deletions

View File

@@ -1,7 +1,8 @@
const constants = require("../constants")
const LimitByFrame = require("./LimitByFrame")
const {getIdentifier} = require("./get_identifier")
require("../testimports")(LimitByFrame, getIdentifier)
const db = require("../db")
require("../testimports")(LimitByFrame, getIdentifier, db)
const limiter = new LimitByFrame()
@@ -13,19 +14,27 @@ function getIPFromReq(req) {
}
}
const preparedTrack = db.prepare("INSERT INTO QuotaHistory VALUES (?, ?, ?)")
function remaining(req) {
if (!constants.quota.enabled) return Infinity // sure.
const ip = getIPFromReq(req)
const identifier = getIdentifier(ip)
return limiter.remaining(identifier)
const identifier = String(getIdentifier(ip))
const remaining = limiter.remaining(identifier)
if (constants.quota.track) {
preparedTrack.run(identifier, Date.now(), remaining)
}
return remaining
}
function add(req, count) {
if (!constants.quota.enabled) return Infinity // why not.
const ip = getIPFromReq(req)
const identifier = getIdentifier(ip)
const identifier = String(getIdentifier(ip))
return limiter.add(identifier, count)
}