Skip to content

Commit f855a7c

Browse files
committed
limit request and confirm lock on delay
1 parent b3877c2 commit f855a7c

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

lib/Service/LockService.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,32 @@ public function lock(): void {
6464

6565
public function update(): void {
6666
if ($this->nextPing === -1) {
67-
throw new LockException('lock service not initiated on this process');
67+
throw new LockException('Lock service not initiated on this process');
6868
}
6969

7070
$time = time();
7171

72-
// update ping
73-
if ($this->nextPing < $time) {
74-
$this->nextPing = $time + self::LOCK_PING_DELAY;
75-
$this->appConfig->setValueInt(Application::APP_ID, ConfigLexicon::LOCK_PING, $this->nextPing + self::LOCK_TIMEOUT);
72+
// do not flood database
73+
if ($this->nextPing > $time) {
74+
return;
75+
}
76+
77+
$this->appConfig->clearCache(true);
78+
$currentLockId = $this->appConfig->getValueString(Application::APP_ID, ConfigLexicon::LOCK_ID);
79+
80+
// new lock; enforce ping on new lock
81+
if ($currentLockId === '') {
82+
throw new LockException('Index not locked');
7683
}
84+
85+
// confirm the lock belongs to the current process
86+
if ($currentLockId !== $this->lockId) {
87+
throw new LockException('Index locked by another process');
88+
}
89+
90+
// update ping
91+
$this->nextPing = $time + self::LOCK_PING_DELAY;
92+
$this->appConfig->setValueInt(Application::APP_ID, ConfigLexicon::LOCK_PING, $this->nextPing + self::LOCK_TIMEOUT);
7793
}
7894

7995
public function unlock(): void {

0 commit comments

Comments
 (0)