Option to track quota for analysis
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user