mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
module.add(user,problem,record)
This commit is contained in:
commit
d5b1f623f8
43 files changed
+5965
No files matched your search
@@ -0,0 +1,35 @@
|
||||
const
|
||||
{ OpcountExceededError } = require('../error'),
|
||||
db = require('../service/db.js'),
|
||||
coll = db.collection('opcount');
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @param {string} op
|
||||
* @param {string} ident
|
||||
* @param {number} period_secs
|
||||
* @param {number} max_operations
|
||||
*/
|
||||
async inc(op, ident, period_secs, max_operations) {
|
||||
let cur_time = new Date().getTime();
|
||||
let begin_at = new Date(cur_time - cur_time % (period_secs * 1000));
|
||||
let expire_at = new Date(begin_at.getTime() + period_secs * 1000);
|
||||
try {
|
||||
await coll.find_one_and_update({
|
||||
ident, begin_at, expire_at,
|
||||
op: { $not: { $gte: max_operations } }
|
||||
}, { $inc: { op: 1 } }, { upsert: true });
|
||||
} catch (e) {
|
||||
throw new OpcountExceededError(op, period_secs, max_operations);
|
||||
}
|
||||
}
|
||||
};
|
||||
/*
|
||||
@argmethod.wrap
|
||||
async def ensure_indexes():
|
||||
coll = db.coll('opcount')
|
||||
await coll.create_index([('ident', 1),
|
||||
('begin_at', 1),
|
||||
('expire_at', 1)], unique=True)
|
||||
await coll.create_index('expire_at', expireAfterSeconds=0)
|
||||
*/
|
||||
Reference in new issue
Block a user