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

86 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-02-26 05:55:20 -05:00
Settings = require "settings-sharelatex"
2014-09-26 12:21:33 -04:00
redis = require("redis-sharelatex")
rclient = redis.createClient(Settings.redis.lock)
os = require "os"
crypto = require "crypto"
logger = require "logger-sharelatex"
HOST = os.hostname()
PID = process.pid
RND = crypto.randomBytes(4).toString('hex')
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
2015-10-08 09:43:34 -04:00
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 : () ->
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';
2014-02-26 05:55:20 -05:00
tryLock : (key, callback = (err, gotLock) ->) ->
lockValue = LockManager.randomLock()
rclient.set key, lockValue, "EX", @LOCK_TTL, "NX", (err, gotLock)->
2014-02-26 05:55:20 -05:00
return callback(err) if err?
if gotLock == "OK"
callback err, true, lockValue
2014-02-26 05:55:20 -05:00
else
callback err, false
getLock: (key, callback = (error) ->) ->
startTime = Date.now()
do attempt = () ->
if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
2016-01-12 05:36:14 -05:00
e = new Error("Timeout")
e.key = key
return callback(e)
2014-02-26 05:55:20 -05:00
LockManager.tryLock key, (error, gotLock, lockValue) ->
2014-02-26 05:55:20 -05:00
return callback(error) if error?
if gotLock
callback(null, lockValue)
2014-02-26 05:55:20 -05:00
else
setTimeout attempt, LockManager.LOCK_TEST_INTERVAL
checkLock: (key, callback = (err, isFree) ->) ->
rclient.exists key, (err, exists) ->
return callback(err) if err?
exists = parseInt exists
if exists == 1
callback err, false
else
callback err, true
releaseLock: (key, lockValue, callback) ->
rclient.eval LockManager.unlockScript, 1, key, lockValue, (err, result) ->
if err?
return callback(err)
if result? and result isnt 1 # successful unlock should release exactly one key
logger.error {key:key, lockValue:lockValue, redis_err:err, redis_result:result}, "unlocking error"
return callback(new Error("tried to release timed out lock"))
callback(err,result)
2014-02-26 05:55:20 -05:00
2020-02-17 12:32:47 -05:00
runWithLock: (key, runner, callback = ( (error) -> )) ->
LockManager.getLock key, (error, lockValue) ->
2014-02-26 05:55:20 -05:00
return callback(error) if error?
runner (error1) ->
LockManager.releaseLock key, lockValue, (error2) ->
2014-02-26 05:55:20 -05:00
error = error1 or error2
return callback(error) if error?
callback()
2015-10-29 06:52:23 -04:00
healthCheck: (callback) ->
action = (releaseLock) ->
releaseLock()
LockManager.runWithLock "HistoryLock:HealthCheck:host=#{HOST}:pid=#{PID}:random=#{RND}", action, callback
close: (callback) ->
rclient.quit()
rclient.once 'end', callback