You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
974 B
JavaScript
33 lines
974 B
JavaScript
const { OpcountExceededError } = require('../error');
|
|
const db = require('../service/db');
|
|
|
|
const coll = db.collection('opcount');
|
|
|
|
/**
|
|
* @param {string} op
|
|
* @param {string} ident
|
|
* @param {number} period_secs
|
|
* @param {number} max_operations
|
|
*/
|
|
async function inc(op, ident, periodSecs, maxOperations) {
|
|
const curTime = new Date().getTime();
|
|
const beginAt = new Date(curTime - (curTime % (periodSecs * 1000)));
|
|
const expireAt = new Date(beginAt.getTime() + periodSecs * 1000);
|
|
try {
|
|
await coll.findOneAndUpdate({
|
|
ident,
|
|
beginAt,
|
|
expireAt,
|
|
op: { $not: { $gte: maxOperations } },
|
|
}, { $inc: { op: 1 } }, { upsert: true });
|
|
} catch (e) {
|
|
throw new OpcountExceededError(op, periodSecs, maxOperations);
|
|
}
|
|
}
|
|
|
|
function ensureIndexes() {
|
|
return coll.createIndex('expireAt', { expireAfterSeconds: 0 });
|
|
}
|
|
|
|
global.Hydro.model.opcount = module.exports = { inc, ensureIndexes };
|