Skip to content

[Lock] prune PdoStorage table on each save according to gcProbability #31804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Symfony/Component/Lock/Store/PdoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function save(Key $key)
if ($key->isExpired()) {
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $key));
}
$this->prune();

return;
} catch (DBALException $e) {
Expand All @@ -140,9 +141,7 @@ public function save(Key $key)
throw new LockExpiredException(sprintf('Failed to store the "%s" lock.', $key));
}

if ($this->gcProbability > 0 && (1.0 === $this->gcProbability || (random_int(0, PHP_INT_MAX) / PHP_INT_MAX) <= $this->gcProbability)) {
$this->prune();
}
$this->prune();
}

/**
Expand Down Expand Up @@ -294,13 +293,15 @@ public function createTable(): void
}

/**
* Cleanups the table by removing all expired locks.
* Cleans up the table by removing all expired locks according to gcProbability.
*/
private function prune(): void
{
$sql = "DELETE FROM $this->table WHERE $this->expirationCol <= {$this->getCurrentTimestampStatement()}";
if ($this->gcProbability > 0 && (1.0 === $this->gcProbability || (random_int(0, PHP_INT_MAX) / PHP_INT_MAX) <= $this->gcProbability)) {
$sql = "DELETE FROM $this->table WHERE $this->expirationCol <= {$this->getCurrentTimestampStatement()}";

$this->getConnection()->exec($sql);
$this->getConnection()->exec($sql);
}
}

private function getDriver(): string
Expand Down