diff options
author | Julius Härtl <jus@bitgrid.net> | 2024-05-13 14:21:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-13 14:21:28 +0200 |
commit | c4e32d33e0e0934396a76787cf74598f9c02af04 (patch) | |
tree | 22e44233e7d3d1cfac93fb7a429dd17bb70a9ab4 /lib/private | |
parent | c31d6b1fdc502844ab907355bb553feb451cdda3 (diff) | |
parent | ff3fc85d104c2fe3d82836de14de6a0268bbf60b (diff) | |
download | nextcloud-server-c4e32d33e0e0934396a76787cf74598f9c02af04.tar.gz nextcloud-server-c4e32d33e0e0934396a76787cf74598f9c02af04.zip |
Merge pull request #44953 from nextcloud/perf/44951/lock-provider
perf(ILockManager): Allow registering a lock provider lazy
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/Lock/LockManager.php | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/lib/private/Files/Lock/LockManager.php b/lib/private/Files/Lock/LockManager.php index e2af532a01c..26fdeb38578 100644 --- a/lib/private/Files/Lock/LockManager.php +++ b/lib/private/Files/Lock/LockManager.php @@ -7,8 +7,11 @@ use OCP\Files\Lock\ILockManager; use OCP\Files\Lock\ILockProvider; use OCP\Files\Lock\LockContext; use OCP\PreConditionNotMetException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class LockManager implements ILockManager { + private ?string $lockProviderClass = null; private ?ILockProvider $lockProvider = null; private ?LockContext $lockInScope = null; @@ -20,12 +23,34 @@ class LockManager implements ILockManager { $this->lockProvider = $lockProvider; } + public function registerLazyLockProvider(string $lockProviderClass): void { + if ($this->lockProviderClass || $this->lockProvider) { + throw new PreConditionNotMetException('There is already a registered lock provider'); + } + + $this->lockProviderClass = $lockProviderClass; + } + + private function getLockProvider(): ?ILockProvider { + if ($this->lockProvider) { + return $this->lockProvider; + } + if ($this->lockProviderClass) { + try { + $this->lockProvider = \OCP\Server::get($this->lockProviderClass); + } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) { + } + } + + return $this->lockProvider; + } + public function isLockProviderAvailable(): bool { - return $this->lockProvider !== null; + return $this->getLockProvider() !== null; } public function runInScope(LockContext $lock, callable $callback): void { - if (!$this->lockProvider) { + if (!$this->getLockProvider()) { $callback(); return; } @@ -47,26 +72,26 @@ class LockManager implements ILockManager { } public function getLocks(int $fileId): array { - if (!$this->lockProvider) { + if (!$this->getLockProvider()) { throw new PreConditionNotMetException('No lock provider available'); } - return $this->lockProvider->getLocks($fileId); + return $this->getLockProvider()->getLocks($fileId); } public function lock(LockContext $lockInfo): ILock { - if (!$this->lockProvider) { + if (!$this->getLockProvider()) { throw new PreConditionNotMetException('No lock provider available'); } - return $this->lockProvider->lock($lockInfo); + return $this->getLockProvider()->lock($lockInfo); } public function unlock(LockContext $lockInfo): void { - if (!$this->lockProvider) { + if (!$this->getLockProvider()) { throw new PreConditionNotMetException('No lock provider available'); } - $this->lockProvider->unlock($lockInfo); + $this->getLockProvider()->unlock($lockInfo); } } |