]> source.dussan.org Git - nextcloud-server.git/commitdiff
perf(ILockManager): Allow registering a lock provider lazy 44953/head
authorJulius Härtl <jus@bitgrid.net>
Sat, 20 Apr 2024 17:51:07 +0000 (19:51 +0200)
committerJulius Härtl <jus@bitgrid.net>
Tue, 30 Apr 2024 20:28:06 +0000 (22:28 +0200)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/Files/Lock/LockManager.php
lib/public/Files/Lock/ILockManager.php

index e2af532a01c2e3536390f64bd599ab823dc57cbf..26fdeb38578c4c698ccb75be745142c4230db4f0 100644 (file)
@@ -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);
        }
 }
index 2cdb027a570e1025731b377f1ce690b3547a1ee9..d29743b7566a76cd9f60d46fd5093f931cb5bbd6 100644 (file)
@@ -42,9 +42,17 @@ interface ILockManager extends ILockProvider {
        /**
         * @throws PreConditionNotMetException if there is already a lock provider registered
         * @since 24.0.0
+        * @deprecated 30.0.0 Use registerLazyLockProvider
         */
        public function registerLockProvider(ILockProvider $lockProvider): void;
 
+       /**
+        * @param string $lockProviderClass
+        * @return void
+        * @since 30.0.0
+        */
+       public function registerLazyLockProvider(string $lockProviderClass): void;
+
        /**
         * @return bool
         * @since 24.0.0