LockManager.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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('settings-sharelatex')
  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(key, lockValue, 'EX', this.LOCK_TTL, 'NX', function (
  42. err,
  43. gotLock
  44. ) {
  45. if (err != null) {
  46. return callback(err)
  47. }
  48. if (gotLock === 'OK') {
  49. return callback(err, true, lockValue)
  50. } else {
  51. return callback(err, false)
  52. }
  53. })
  54. },
  55. getLock(key, callback) {
  56. let attempt
  57. if (callback == null) {
  58. callback = function (error) {}
  59. }
  60. const startTime = Date.now()
  61. return (attempt = function () {
  62. if (Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME) {
  63. const e = new Error('Timeout')
  64. e.key = key
  65. return callback(e)
  66. }
  67. return LockManager.tryLock(key, function (error, gotLock, lockValue) {
  68. if (error != null) {
  69. return callback(error)
  70. }
  71. if (gotLock) {
  72. return callback(null, lockValue)
  73. } else {
  74. return setTimeout(attempt, LockManager.LOCK_TEST_INTERVAL)
  75. }
  76. })
  77. })()
  78. },
  79. checkLock(key, callback) {
  80. if (callback == null) {
  81. callback = function (err, isFree) {}
  82. }
  83. return rclient.exists(key, function (err, exists) {
  84. if (err != null) {
  85. return callback(err)
  86. }
  87. exists = parseInt(exists)
  88. if (exists === 1) {
  89. return callback(err, false)
  90. } else {
  91. return callback(err, true)
  92. }
  93. })
  94. },
  95. releaseLock(key, lockValue, callback) {
  96. return rclient.eval(LockManager.unlockScript, 1, key, lockValue, function (
  97. err,
  98. result
  99. ) {
  100. if (err != null) {
  101. return callback(err)
  102. }
  103. if (result != null && result !== 1) {
  104. // successful unlock should release exactly one key
  105. logger.error(
  106. { key, lockValue, redis_err: err, redis_result: result },
  107. 'unlocking error'
  108. )
  109. return callback(new Error('tried to release timed out lock'))
  110. }
  111. return callback(err, result)
  112. })
  113. },
  114. runWithLock(key, runner, callback) {
  115. if (callback == null) {
  116. callback = function (error) {}
  117. }
  118. return LockManager.getLock(key, function (error, lockValue) {
  119. if (error != null) {
  120. return callback(error)
  121. }
  122. return runner((error1) =>
  123. LockManager.releaseLock(key, lockValue, function (error2) {
  124. error = error1 || error2
  125. if (error != null) {
  126. return callback(error)
  127. }
  128. return callback()
  129. })
  130. )
  131. })
  132. },
  133. healthCheck(callback) {
  134. const action = (releaseLock) => releaseLock()
  135. return LockManager.runWithLock(
  136. `HistoryLock:HealthCheck:host=${HOST}:pid=${PID}:random=${RND}`,
  137. action,
  138. callback
  139. )
  140. },
  141. close(callback) {
  142. rclient.quit()
  143. return rclient.once('end', callback)
  144. }
  145. }