mirror of https://github.com/nextcloud/server
perf(ILockManager): Allow registering a lock provider lazy
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
parent
fbb4518a05
commit
ff3fc85d10
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue