mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-26 19:54:25 +00:00
c530b791a4
Replace filesystem lock in CLSI with in-memory lock GitOrigin-RevId: de1ac3beca67bb4e9070806871a1c7b6a59aa77f
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
const logger = require('@overleaf/logger')
|
|
const Errors = require('./Errors')
|
|
const RequestParser = require('./RequestParser')
|
|
|
|
// The lock timeout should be higher than the maximum end-to-end compile time.
|
|
// Here, we use the maximum compile timeout plus 2 minutes.
|
|
const LOCK_TIMEOUT_MS = RequestParser.MAX_TIMEOUT * 1000 + 120000
|
|
|
|
const LOCKS = new Map()
|
|
|
|
function acquire(key) {
|
|
const currentLock = LOCKS.get(key)
|
|
if (currentLock != null) {
|
|
if (currentLock.isExpired()) {
|
|
logger.warn({ key }, 'Compile lock expired')
|
|
currentLock.release()
|
|
} else {
|
|
throw new Errors.AlreadyCompilingError('compile in progress')
|
|
}
|
|
}
|
|
|
|
const lock = new Lock(key)
|
|
LOCKS.set(key, lock)
|
|
return lock
|
|
}
|
|
|
|
class Lock {
|
|
constructor(key) {
|
|
this.key = key
|
|
this.expiresAt = Date.now() + LOCK_TIMEOUT_MS
|
|
}
|
|
|
|
isExpired() {
|
|
return Date.now() >= this.expiresAt
|
|
}
|
|
|
|
release() {
|
|
const lockWasActive = LOCKS.delete(this.key)
|
|
if (!lockWasActive) {
|
|
logger.error({ key: this.key }, 'Lock was released twice')
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { acquire }
|