1
0

MemcacheLockingProvider.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Lock;
  24. use OCP\IMemcacheTTL;
  25. use OCP\Lock\LockedException;
  26. use OCP\IMemcache;
  27. class MemcacheLockingProvider extends AbstractLockingProvider {
  28. /**
  29. * @var \OCP\IMemcache
  30. */
  31. private $memcache;
  32. /**
  33. * @param \OCP\IMemcache $memcache
  34. * @param int $ttl
  35. */
  36. public function __construct(IMemcache $memcache, int $ttl = 3600) {
  37. $this->memcache = $memcache;
  38. $this->ttl = $ttl;
  39. }
  40. private function setTTL(string $path) {
  41. if ($this->memcache instanceof IMemcacheTTL) {
  42. $this->memcache->setTTL($path, $this->ttl);
  43. }
  44. }
  45. /**
  46. * @param string $path
  47. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  48. * @return bool
  49. */
  50. public function isLocked(string $path, int $type): bool {
  51. $lockValue = $this->memcache->get($path);
  52. if ($type === self::LOCK_SHARED) {
  53. return $lockValue > 0;
  54. } else if ($type === self::LOCK_EXCLUSIVE) {
  55. return $lockValue === 'exclusive';
  56. } else {
  57. return false;
  58. }
  59. }
  60. /**
  61. * @param string $path
  62. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  63. * @throws \OCP\Lock\LockedException
  64. */
  65. public function acquireLock(string $path, int $type) {
  66. if ($type === self::LOCK_SHARED) {
  67. if (!$this->memcache->inc($path)) {
  68. throw new LockedException($path);
  69. }
  70. } else {
  71. $this->memcache->add($path, 0);
  72. if (!$this->memcache->cas($path, 0, 'exclusive')) {
  73. throw new LockedException($path);
  74. }
  75. }
  76. $this->setTTL($path);
  77. $this->markAcquire($path, $type);
  78. }
  79. /**
  80. * @param string $path
  81. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  82. */
  83. public function releaseLock(string $path, int $type) {
  84. if ($type === self::LOCK_SHARED) {
  85. if ($this->getOwnSharedLockCount($path) === 1) {
  86. $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
  87. if (!$removed) { //someone else also has a shared lock, decrease only
  88. $this->memcache->dec($path);
  89. }
  90. } else {
  91. // if we own more than one lock ourselves just decrease
  92. $this->memcache->dec($path);
  93. }
  94. } else if ($type === self::LOCK_EXCLUSIVE) {
  95. $this->memcache->cad($path, 'exclusive');
  96. }
  97. $this->markRelease($path, $type);
  98. }
  99. /**
  100. * Change the type of an existing lock
  101. *
  102. * @param string $path
  103. * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  104. * @throws \OCP\Lock\LockedException
  105. */
  106. public function changeLock(string $path, int $targetType) {
  107. if ($targetType === self::LOCK_SHARED) {
  108. if (!$this->memcache->cas($path, 'exclusive', 1)) {
  109. throw new LockedException($path);
  110. }
  111. } else if ($targetType === self::LOCK_EXCLUSIVE) {
  112. // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
  113. if (!$this->memcache->cas($path, 1, 'exclusive')) {
  114. throw new LockedException($path);
  115. }
  116. }
  117. $this->setTTL($path);
  118. $this->markChange($path, $targetType);
  119. }
  120. }