2014-02-12 05:40:42 -05:00
|
|
|
metrics = require('./Metrics')
|
|
|
|
Settings = require('settings-sharelatex')
|
2014-10-07 07:08:36 -04:00
|
|
|
redis = require("redis-sharelatex")
|
|
|
|
rclient = redis.createClient(Settings.redis.web)
|
2014-02-12 05:40:42 -05:00
|
|
|
keys = require('./RedisKeyBuilder')
|
|
|
|
logger = require "logger-sharelatex"
|
2016-04-13 06:59:56 -04:00
|
|
|
os = require "os"
|
|
|
|
crypto = require "crypto"
|
|
|
|
|
|
|
|
HOST = os.hostname()
|
|
|
|
PID = process.pid
|
2014-02-12 05:40:42 -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
|
2016-04-13 06:59:56 -04:00
|
|
|
LOCK_TTL: 30 # seconds. Time until lock auto expires in redis.
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
RND = crypto.randomBytes(4).toString('hex')
|
|
|
|
return "locked:host=#{HOST}:pid=#{PID}:random=#{RND}:time=#{time}"
|
|
|
|
|
|
|
|
unlockScript: 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end';
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
tryLock : (doc_id, callback = (err, isFree)->)->
|
2016-04-13 06:59:56 -04:00
|
|
|
lockValue = LockManager.randomLock()
|
|
|
|
key = keys.blockingKey(doc_id:doc_id)
|
|
|
|
rclient.set key, lockValue, "EX", @LOCK_TTL, "NX", (err, gotLock)->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(err) if err?
|
|
|
|
if gotLock == "OK"
|
|
|
|
metrics.inc "doc-not-blocking"
|
2016-04-13 06:59:56 -04:00
|
|
|
callback err, true, lockValue
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
|
|
|
metrics.inc "doc-blocking"
|
2016-04-13 06:59:56 -04:00
|
|
|
logger.log {doc_id, lockValue}, "doc is locked"
|
2014-02-12 05:40:42 -05:00
|
|
|
callback err, false
|
|
|
|
|
|
|
|
getLock: (doc_id, callback = (error) ->) ->
|
|
|
|
startTime = Date.now()
|
|
|
|
do attempt = () ->
|
|
|
|
if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
|
2016-04-13 06:59:56 -04:00
|
|
|
e = new Error("Timeout")
|
|
|
|
e.doc_id = doc_id
|
|
|
|
return callback(e)
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-04-13 06:59:56 -04:00
|
|
|
LockManager.tryLock doc_id, (error, gotLock, lockValue) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(error) if error?
|
|
|
|
if gotLock
|
2016-04-13 06:59:56 -04:00
|
|
|
callback(null, lockValue)
|
2014-02-12 05:40:42 -05:00
|
|
|
else
|
|
|
|
setTimeout attempt, LockManager.LOCK_TEST_INTERVAL
|
|
|
|
|
|
|
|
checkLock: (doc_id, callback = (err, isFree)->)->
|
2016-04-13 06:59:56 -04:00
|
|
|
key = keys.blockingKey(doc_id:doc_id)
|
|
|
|
rclient.exists key, (err, exists) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
return callback(err) if err?
|
2016-04-13 06:59:56 -04:00
|
|
|
exists = parseInt exists
|
2014-02-12 05:40:42 -05:00
|
|
|
if exists == 1
|
|
|
|
metrics.inc "doc-blocking"
|
|
|
|
callback err, false
|
|
|
|
else
|
|
|
|
metrics.inc "doc-not-blocking"
|
|
|
|
callback err, true
|
|
|
|
|
2016-04-13 06:59:56 -04:00
|
|
|
releaseLock: (doc_id, lockValue, callback)->
|
|
|
|
key = keys.blockingKey(doc_id:doc_id)
|
|
|
|
rclient.eval LockManager.unlockScript, 1, key, lockValue, callback
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
|