Use flock to avoid race conditions on views-count file (#67)

This commit is contained in:
Omar Adel Brikaa
2023-09-02 16:38:09 +03:00
committed by GitHub
parent 84ac753adf
commit bdaf96941f

View File

@@ -57,8 +57,18 @@ final class CounterFileRepository implements
private function incrementViewsCount(Username $username): void
{
$counterFilePath = $this->getCounterFilePath($username);
// Need to open the file in "c" mode to avoid truncating before acquiring the lock and before reading
$counterFile = fopen($counterFilePath, "c");
file_put_contents($counterFilePath, $this->getViewsCountByUsername($username) + 1);
// Acquire an exclusive lock to avoid two requests causing a write at the same time
flock($counterFile, LOCK_EX);
$incremented = $this->getViewsCountByUsername($username) + 1;
ftruncate($counterFile, 0);
fwrite($counterFile, "$incremented\n");
fflush($counterFile);
// Release the exclusive lock
flock($counterFile, LOCK_UN);
fclose($counterFile);
}
private function getViewsFilePath(Username $username): string