Merge pull request #30539 from overleaf/mfb-promisify-cooldown-manager

promisify cooldown manager

GitOrigin-RevId: 88c8c7d7e306042524ecd400fb15527206472cb1
This commit is contained in:
Maria Florencia Besteiro Gonzalez
2026-01-13 09:05:43 +00:00
committed by Copybot
parent 29f5026dda
commit 3261ffa4d0
7 changed files with 20 additions and 211 deletions
@@ -1,6 +1,5 @@
import RedisWrapper from '../../infrastructure/RedisWrapper.mjs'
import logger from '@overleaf/logger'
import { promisify } from '@overleaf/promise-utils'
const rclient = RedisWrapper.client('cooldown')
const COOLDOWN_IN_SECONDS = 60 * 10
@@ -10,39 +9,23 @@ const CooldownManager = {
return `Cooldown:{${projectId}}`
},
putProjectOnCooldown(projectId, callback) {
if (callback == null) {
callback = function () {}
}
async putProjectOnCooldown(projectId) {
logger.debug(
{ projectId },
`[Cooldown] putting project on cooldown for ${COOLDOWN_IN_SECONDS} seconds`
)
rclient.set(
await rclient.set(
CooldownManager._buildKey(projectId),
'1',
'EX',
COOLDOWN_IN_SECONDS,
callback
COOLDOWN_IN_SECONDS
)
},
isProjectOnCooldown(projectId, callback) {
if (callback == null) {
callback = function () {}
}
rclient.get(CooldownManager._buildKey(projectId), function (err, result) {
if (err != null) {
return callback(err)
}
return callback(null, result === '1')
})
async isProjectOnCooldown(projectId) {
const result = await rclient.get(CooldownManager._buildKey(projectId))
return result === '1'
},
}
CooldownManager.promises = {
putProjectOnCooldown: promisify(CooldownManager.putProjectOnCooldown),
isProjectOnCooldown: promisify(CooldownManager.isProjectOnCooldown),
}
export default CooldownManager
@@ -1,41 +0,0 @@
/* eslint-disable
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import CooldownManager from './CooldownManager.mjs'
import logger from '@overleaf/logger'
let CooldownMiddleware
export default CooldownMiddleware = {
freezeProject(req, res, next) {
const projectId = req.params.Project_id
if (projectId == null) {
return next(new Error('[Cooldown] No projectId parameter on route'))
}
return CooldownManager.isProjectOnCooldown(
projectId,
function (err, projectIsOnCooldown) {
if (err != null) {
return next(err)
}
if (projectIsOnCooldown) {
logger.debug(
{ projectId },
'[Cooldown] project is on cooldown, denying request'
)
return res.sendStatus(429)
}
return next()
}
)
},
}
@@ -554,7 +554,7 @@ async function _putElement(project, folderId, element, type, userId) {
{ projectId: project._id },
'project too big, stopping insertions'
)
CooldownManager.putProjectOnCooldown(project._id)
await CooldownManager.putProjectOnCooldown(project._id)
throw new Error('project_has_too_many_files')
}
@@ -25,8 +25,9 @@ async function newUpdate(
return null
}
const projectIsOnCooldown =
await CooldownManager.promises.isProjectOnCooldown(project._id)
const projectIsOnCooldown = await CooldownManager.isProjectOnCooldown(
project._id
)
if (projectIsOnCooldown) {
throw new Errors.TooManyRequestsError('project on cooldown')
}
@@ -173,8 +174,9 @@ async function createFolder(userId, projectId, projectName, path) {
return null
}
const projectIsOnCooldown =
await CooldownManager.promises.isProjectOnCooldown(project._id)
const projectIsOnCooldown = await CooldownManager.isProjectOnCooldown(
project._id
)
if (projectIsOnCooldown) {
throw new Errors.TooManyRequestsError('project on cooldown')
}