LockManager.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. Settings = require "settings-sharelatex"
  2. redis = require("redis-sharelatex")
  3. rclient = redis.createClient(Settings.redis.lock)
  4. os = require "os"
  5. crypto = require "crypto"
  6. logger = require "logger-sharelatex"
  7. HOST = os.hostname()
  8. PID = process.pid
  9. RND = crypto.randomBytes(4).toString('hex')
  10. COUNT = 0
  11. module.exports = LockManager =
  12. LOCK_TEST_INTERVAL: 50 # 50ms between each test of the lock
  13. MAX_LOCK_WAIT_TIME: 10000 # 10s maximum time to spend trying to get the lock
  14. LOCK_TTL: 300 # seconds (allow 5 minutes for any operation to complete)
  15. # Use a signed lock value as described in
  16. # http://redis.io/topics/distlock#correct-implementation-with-a-single-instance
  17. # to prevent accidental unlocking by multiple processes
  18. randomLock : () ->
  19. time = Date.now()
  20. return "locked:host=#{HOST}:pid=#{PID}:random=#{RND}:time=#{time}:count=#{COUNT++}"
  21. unlockScript: 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end';
  22. tryLock : (key, callback = (err, gotLock) ->) ->
  23. lockValue = LockManager.randomLock()
  24. rclient.set key, lockValue, "EX", @LOCK_TTL, "NX", (err, gotLock)->
  25. return callback(err) if err?
  26. if gotLock == "OK"
  27. callback err, true, lockValue
  28. else
  29. callback err, false
  30. getLock: (key, callback = (error) ->) ->
  31. startTime = Date.now()
  32. do attempt = () ->
  33. if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
  34. e = new Error("Timeout")
  35. e.key = key
  36. return callback(e)
  37. LockManager.tryLock key, (error, gotLock, lockValue) ->
  38. return callback(error) if error?
  39. if gotLock
  40. callback(null, lockValue)
  41. else
  42. setTimeout attempt, LockManager.LOCK_TEST_INTERVAL
  43. checkLock: (key, callback = (err, isFree) ->) ->
  44. rclient.exists key, (err, exists) ->
  45. return callback(err) if err?
  46. exists = parseInt exists
  47. if exists == 1
  48. callback err, false
  49. else
  50. callback err, true
  51. releaseLock: (key, lockValue, callback) ->
  52. rclient.eval LockManager.unlockScript, 1, key, lockValue, (err, result) ->
  53. if err?
  54. return callback(err)
  55. if result? and result isnt 1 # successful unlock should release exactly one key
  56. logger.error {key:key, lockValue:lockValue, redis_err:err, redis_result:result}, "unlocking error"
  57. return callback(new Error("tried to release timed out lock"))
  58. callback(err,result)
  59. runWithLock: (key, runner = ( (releaseLock = (error) ->) -> ), callback = ( (error) -> )) ->
  60. LockManager.getLock key, (error, lockValue) ->
  61. return callback(error) if error?
  62. runner (error1) ->
  63. LockManager.releaseLock key, lockValue, (error2) ->
  64. error = error1 or error2
  65. return callback(error) if error?
  66. callback()
  67. healthCheck: (callback) ->
  68. action = (releaseLock) ->
  69. releaseLock()
  70. LockManager.runWithLock "HistoryLock:HealthCheck:host=#{HOST}:pid=#{PID}:random=#{RND}", action, callback
  71. close: (callback) ->
  72. rclient.quit()
  73. rclient.once 'end', callback