overleaf/services/track-changes/app/js/LockManager.js

165 lines
4.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable
handle-callback-err,
*/
// 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
*/
let LockManager
const Settings = require('@overleaf/settings')
const redis = require('@overleaf/redis-wrapper')
const rclient = redis.createClient(Settings.redis.lock)
const os = require('os')
const crypto = require('crypto')
const logger = require('logger-sharelatex')
const HOST = os.hostname()
const PID = process.pid
const RND = crypto.randomBytes(4).toString('hex')
let COUNT = 0
2014-02-26 05:55:20 -05:00
module.exports = LockManager = {
LOCK_TEST_INTERVAL: 50, // 50ms between each test of the lock
MAX_LOCK_WAIT_TIME: 10000, // 10s maximum time to spend trying to get the lock
LOCK_TTL: 300, // seconds (allow 5 minutes for any operation to complete)
2014-02-26 05:55:20 -05:00
// Use a signed lock value as described in
// http://redis.io/topics/distlock#correct-implementation-with-a-single-instance
// to prevent accidental unlocking by multiple processes
randomLock() {
const time = Date.now()
return `locked:host=${HOST}:pid=${PID}:random=${RND}:time=${time}:count=${COUNT++}`
},
unlockScript:
'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end',
tryLock(key, callback) {
if (callback == null) {
2020-06-04 04:24:21 -04:00
callback = function (err, gotLock) {}
}
const lockValue = LockManager.randomLock()
2021-07-13 07:04:43 -04:00
return rclient.set(
key,
lockValue,
'EX',
this.LOCK_TTL,
'NX',
function (err, gotLock) {
if (err != null) {
return callback(err)
}
if (gotLock === 'OK') {
return callback(err, true, lockValue)
} else {
return callback(err, false)
}
}
2021-07-13 07:04:43 -04:00
)
},
2014-02-26 05:55:20 -05:00
getLock(key, callback) {
let attempt
if (callback == null) {
2020-06-04 04:24:21 -04:00
callback = function (error) {}
}
const startTime = Date.now()
2020-06-04 04:24:21 -04:00
return (attempt = function () {
if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
const e = new Error('Timeout')
e.key = key
return callback(e)
}
2014-02-26 05:55:20 -05:00
2020-06-04 04:24:21 -04:00
return LockManager.tryLock(key, function (error, gotLock, lockValue) {
if (error != null) {
return callback(error)
}
if (gotLock) {
return callback(null, lockValue)
} else {
return setTimeout(attempt, LockManager.LOCK_TEST_INTERVAL)
}
})
})()
},
2014-02-26 05:55:20 -05:00
checkLock(key, callback) {
if (callback == null) {
2020-06-04 04:24:21 -04:00
callback = function (err, isFree) {}
}
2020-06-04 04:24:21 -04:00
return rclient.exists(key, function (err, exists) {
if (err != null) {
return callback(err)
}
exists = parseInt(exists)
if (exists === 1) {
return callback(err, false)
} else {
return callback(err, true)
}
})
},
2014-02-26 05:55:20 -05:00
releaseLock(key, lockValue, callback) {
2021-07-13 07:04:43 -04:00
return rclient.eval(
LockManager.unlockScript,
1,
key,
lockValue,
function (err, result) {
if (err != null) {
return callback(err)
}
if (result != null && result !== 1) {
// successful unlock should release exactly one key
logger.error(
{ key, lockValue, redis_err: err, redis_result: result },
'unlocking error'
)
return callback(new Error('tried to release timed out lock'))
}
return callback(err, result)
}
2021-07-13 07:04:43 -04:00
)
},
2014-02-26 05:55:20 -05:00
runWithLock(key, runner, callback) {
if (callback == null) {
2020-06-04 04:24:21 -04:00
callback = function (error) {}
}
2020-06-04 04:24:21 -04:00
return LockManager.getLock(key, function (error, lockValue) {
if (error != null) {
return callback(error)
}
2021-07-13 07:04:43 -04:00
return runner(error1 =>
2020-06-04 04:24:21 -04:00
LockManager.releaseLock(key, lockValue, function (error2) {
error = error1 || error2
if (error != null) {
return callback(error)
}
return callback()
})
)
})
},
2014-02-26 05:55:20 -05:00
healthCheck(callback) {
2021-07-13 07:04:43 -04:00
const action = releaseLock => releaseLock()
return LockManager.runWithLock(
`HistoryLock:HealthCheck:host=${HOST}:pid=${PID}:random=${RND}`,
action,
callback
)
},
close(callback) {
rclient.quit()
return rclient.once('end', callback)
2021-07-13 07:04:43 -04:00
},
}