LockManager.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* eslint-disable
  2. handle-callback-err,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. let LockManager
  13. const Settings = require('@overleaf/settings')
  14. const redis = require('@overleaf/redis-wrapper')
  15. const rclient = redis.createClient(Settings.redis.lock)
  16. const os = require('os')
  17. const crypto = require('crypto')
  18. const logger = require('logger-sharelatex')
  19. const HOST = os.hostname()
  20. const PID = process.pid
  21. const RND = crypto.randomBytes(4).toString('hex')
  22. let COUNT = 0
  23. module.exports = LockManager = {
  24. LOCK_TEST_INTERVAL: 50, // 50ms between each test of the lock
  25. MAX_LOCK_WAIT_TIME: 10000, // 10s maximum time to spend trying to get the lock
  26. LOCK_TTL: 300, // seconds (allow 5 minutes for any operation to complete)
  27. // Use a signed lock value as described in
  28. // http://redis.io/topics/distlock#correct-implementation-with-a-single-instance
  29. // to prevent accidental unlocking by multiple processes
  30. randomLock() {
  31. const time = Date.now()
  32. return `locked:host=${HOST}:pid=${PID}:random=${RND}:time=${time}:count=${COUNT++}`
  33. },
  34. unlockScript:
  35. 'if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end',
  36. tryLock(key, callback) {
  37. if (callback == null) {
  38. callback = function (err, gotLock) {}
  39. }
  40. const lockValue = LockManager.randomLock()
  41. return rclient.set(
  42. key,
  43. lockValue,
  44. 'EX',
  45. this.LOCK_TTL,
  46. 'NX',
  47. function (err, gotLock) {
  48. if (err != null) {
  49. return callback(err)
  50. }
  51. if (gotLock === 'OK') {
  52. return callback(err, true, lockValue)
  53. } else {
  54. return callback(err, false)
  55. }
  56. }
  57. )
  58. },
  59. getLock(key, callback) {
  60. let attempt
  61. if (callback == null) {
  62. callback = function (error) {}
  63. }
  64. const startTime = Date.now()
  65. return (attempt = function () {
  66. if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
  67. const e = new Error('Timeout')
  68. e.key = key
  69. return callback(e)
  70. }
  71. return LockManager.tryLock(key, function (error, gotLock, lockValue) {
  72. if (error != null) {
  73. return callback(error)
  74. }
  75. if (gotLock) {
  76. return callback(null, lockValue)
  77. } else {
  78. return setTimeout(attempt, LockManager.LOCK_TEST_INTERVAL)
  79. }
  80. })
  81. })()
  82. },
  83. checkLock(key, callback) {
  84. if (callback == null) {
  85. callback = function (err, isFree) {}
  86. }
  87. return rclient.exists(key, function (err, exists) {
  88. if (err != null) {
  89. return callback(err)
  90. }
  91. exists = parseInt(exists)
  92. if (exists === 1) {
  93. return callback(err, false)
  94. } else {
  95. return callback(err, true)
  96. }
  97. })
  98. },
  99. releaseLock(key, lockValue, callback) {
  100. return rclient.eval(
  101. LockManager.unlockScript,
  102. 1,
  103. key,
  104. lockValue,
  105. function (err, result) {
  106. if (err != null) {
  107. return callback(err)
  108. }
  109. if (result != null && result !== 1) {
  110. // successful unlock should release exactly one key
  111. logger.error(
  112. { key, lockValue, redis_err: err, redis_result: result },
  113. 'unlocking error'
  114. )
  115. return callback(new Error('tried to release timed out lock'))
  116. }
  117. return callback(err, result)
  118. }
  119. )
  120. },
  121. runWithLock(key, runner, callback) {
  122. if (callback == null) {
  123. callback = function (error) {}
  124. }
  125. return LockManager.getLock(key, function (error, lockValue) {
  126. if (error != null) {
  127. return callback(error)
  128. }
  129. return runner(error1 =>
  130. LockManager.releaseLock(key, lockValue, function (error2) {
  131. error = error1 || error2
  132. if (error != null) {
  133. return callback(error)
  134. }
  135. return callback()
  136. })
  137. )
  138. })
  139. },
  140. healthCheck(callback) {
  141. const action = releaseLock => releaseLock()
  142. return LockManager.runWithLock(
  143. `HistoryLock:HealthCheck:host=${HOST}:pid=${PID}:random=${RND}`,
  144. action,
  145. callback
  146. )
  147. },
  148. close(callback) {
  149. rclient.quit()
  150. return rclient.once('end', callback)
  151. },
  152. }