mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-25 00:41:32 +00:00
Merge pull request #39 from sharelatex/bg-lock-exponential-backoff
exponential backoff for lock up to maximum time
This commit is contained in:
commit
4622362e42
1 changed files with 5 additions and 1 deletions
|
@ -14,6 +14,7 @@ COUNT = 0
|
|||
|
||||
module.exports = LockManager =
|
||||
LOCK_TEST_INTERVAL: 50 # 50ms between each test of the lock
|
||||
MAX_TEST_INTERVAL: 1000 # back off to 1s between each test of the lock
|
||||
MAX_LOCK_WAIT_TIME: 10000 # 10s maximum time to spend trying to get the lock
|
||||
LOCK_TTL: 30 # seconds. Time until lock auto expires in redis.
|
||||
|
||||
|
@ -41,6 +42,7 @@ module.exports = LockManager =
|
|||
|
||||
getLock: (doc_id, callback = (error, lockValue) ->) ->
|
||||
startTime = Date.now()
|
||||
testInterval = LockManager.LOCK_TEST_INTERVAL
|
||||
do attempt = () ->
|
||||
if Date.now() - startTime > LockManager.MAX_LOCK_WAIT_TIME
|
||||
e = new Error("Timeout")
|
||||
|
@ -52,7 +54,9 @@ module.exports = LockManager =
|
|||
if gotLock
|
||||
callback(null, lockValue)
|
||||
else
|
||||
setTimeout attempt, LockManager.LOCK_TEST_INTERVAL
|
||||
setTimeout attempt, testInterval
|
||||
# back off when the lock is taken to avoid overloading
|
||||
testInterval = Math.min(testInterval * 2, LockManager.MAX_TEST_INTERVAL)
|
||||
|
||||
checkLock: (doc_id, callback = (err, isFree)->)->
|
||||
key = keys.blockingKey(doc_id:doc_id)
|
||||
|
|
Loading…
Reference in a new issue