LockManager.mjs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import settings from '@overleaf/settings'
  2. import RedisWrapper from './RedisWrapper.mjs'
  3. import RedisWebLocker from '@overleaf/redis-wrapper/RedisWebLocker.js'
  4. const rclient = RedisWrapper.client('lock')
  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. export default LockManager