LockManager.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const settings = require('@overleaf/settings')
  2. const RedisWrapper = require('./RedisWrapper')
  3. const rclient = RedisWrapper.client('lock')
  4. const RedisWebLocker = require('@overleaf/redis-wrapper/RedisWebLocker')
  5. // this method creates a lock manager with the provided timeout options
  6. function createLockManager(options) {
  7. return new RedisWebLocker({
  8. rclient,
  9. getKey(namespace, id) {
  10. return `lock:web:${namespace}:${id}`
  11. },
  12. options,
  13. })
  14. }
  15. // this is the default lock manager for web
  16. const LockManager = createLockManager(settings.lockManager)
  17. // this method creates a lock manager with a configurable timeout
  18. // it shares the lock keys with the default lock manager
  19. LockManager.withTimeout = function (timeout) {
  20. const overrides = {
  21. redisLockExpiry: timeout, // in seconds
  22. slowExecutionThreshold: 0.5 * timeout * 1000, // in ms
  23. }
  24. const lockManagerSettingsWithTimeout = Object.assign(
  25. {},
  26. settings.lockManager,
  27. overrides
  28. )
  29. return createLockManager(lockManagerSettingsWithTimeout)
  30. }
  31. module.exports = LockManager