-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add atomic locks for cache-backed concurrency control #10145
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
Open
memleakd
wants to merge
8
commits into
codeigniter4:4.8
Choose a base branch
from
memleakd:feat/cache-locks
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af486e9
feat(lock): add atomic lock service
memleakd ca4baa3
feat(cache): add lock store support
memleakd 3fef42c
test(lock): cover atomic lock behavior
memleakd cf3fe97
docs(lock): document atomic locks
memleakd 2fd0307
docs(lock): document cache flush behavior
memleakd 6667132
refactor(lock): align store interface and service behavior
memleakd bb56765
refactor(lock): move lock stores out of cache handlers
memleakd 229dcc0
test(lock): add lock support to mock cache
memleakd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Cache; | ||
|
|
||
| interface LockStoreInterface | ||
| { | ||
| public function acquireLock(string $key, string $owner, int $ttl): bool; | ||
|
|
||
| public function releaseLock(string $key, string $owner): bool; | ||
|
|
||
| public function forceReleaseLock(string $key): bool; | ||
|
|
||
| public function refreshLock(string $key, string $owner, int $ttl): bool; | ||
|
|
||
| public function getLockOwner(string $key): ?string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Cache; | ||
|
|
||
| interface LockStoreProvider | ||
| { | ||
| public function lockStore(): LockStoreInterface; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Cache\LockStores; | ||
|
|
||
| use CodeIgniter\Cache\Handlers\FileHandler; | ||
| use CodeIgniter\Cache\LockStoreInterface; | ||
| use CodeIgniter\I18n\Time; | ||
| use Throwable; | ||
|
|
||
| class FileLockStore implements LockStoreInterface | ||
| { | ||
| public function __construct( | ||
| private readonly string $path, | ||
| private readonly int $mode, | ||
| private readonly string $prefix = '', | ||
| ) { | ||
| } | ||
|
|
||
| public function acquireLock(string $key, string $owner, int $ttl): bool | ||
| { | ||
| return $this->withLockFile($key, static function ($handle) use ($owner, $ttl): bool { | ||
| $data = self::readLockData($handle); | ||
| $now = Time::now()->getTimestamp(); | ||
|
|
||
| if ($data !== null && $data['expires'] > $now) { | ||
| return false; | ||
| } | ||
|
|
||
| return self::writeLockData($handle, $owner, $now + $ttl); | ||
| }); | ||
| } | ||
|
|
||
| public function releaseLock(string $key, string $owner): bool | ||
| { | ||
| return $this->withLockFile($key, static function ($handle) use ($owner): bool { | ||
| $data = self::readLockData($handle); | ||
|
|
||
| if ($data === null || $data['owner'] !== $owner) { | ||
| return false; | ||
| } | ||
|
|
||
| return self::clearLockFile($handle); | ||
| }); | ||
| } | ||
|
|
||
| public function forceReleaseLock(string $key): bool | ||
| { | ||
| return ! is_file($this->path . FileHandler::validateKey($key, $this->prefix)) | ||
| || $this->withLockFile($key, static fn ($handle): bool => self::clearLockFile($handle), false); | ||
| } | ||
|
|
||
| public function refreshLock(string $key, string $owner, int $ttl): bool | ||
| { | ||
| return $this->withLockFile($key, static function ($handle) use ($owner, $ttl): bool { | ||
| $data = self::readLockData($handle); | ||
| $now = Time::now()->getTimestamp(); | ||
|
|
||
| if ($data === null || $data['owner'] !== $owner || $data['expires'] <= $now) { | ||
| return false; | ||
| } | ||
|
|
||
| return self::writeLockData($handle, $owner, $now + $ttl); | ||
| }); | ||
| } | ||
|
|
||
| public function getLockOwner(string $key): ?string | ||
| { | ||
| $owner = null; | ||
|
|
||
| $this->withLockFile($key, static function ($handle) use (&$owner): bool { | ||
| $data = self::readLockData($handle); | ||
|
|
||
| if ($data === null) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($data['expires'] <= Time::now()->getTimestamp()) { | ||
| self::clearLockFile($handle); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| $owner = $data['owner']; | ||
|
|
||
| return true; | ||
| }, false); | ||
|
|
||
| return $owner; | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(resource): bool $callback | ||
| */ | ||
| private function withLockFile(string $key, callable $callback, bool $create = true): bool | ||
| { | ||
| $key = FileHandler::validateKey($key, $this->prefix); | ||
| $handle = @fopen($this->path . $key, $create ? 'c+b' : 'r+b'); | ||
|
|
||
| if ($handle === false) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| if (! flock($handle, LOCK_EX)) { | ||
| return false; | ||
| } | ||
|
|
||
| return $callback($handle); | ||
| } finally { | ||
| flock($handle, LOCK_UN); | ||
| fclose($handle); | ||
|
|
||
| if (is_file($this->path . $key)) { | ||
| try { | ||
| chmod($this->path . $key, $this->mode); | ||
| } catch (Throwable $e) { | ||
| log_message('debug', 'Failed to set mode on cache lock file: ' . $e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $handle | ||
| * | ||
| * @return array{owner: string, expires: int}|null | ||
| */ | ||
| private static function readLockData($handle): ?array | ||
| { | ||
| rewind($handle); | ||
|
|
||
| $content = stream_get_contents($handle); | ||
|
|
||
| if ($content === false || $content === '') { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| $data = unserialize($content); | ||
| } catch (Throwable) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! is_array($data) || ! isset($data['owner'], $data['expires']) || ! is_string($data['owner']) || ! is_int($data['expires'])) { | ||
| return null; | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $handle | ||
| */ | ||
| private static function writeLockData($handle, string $owner, int $expires): bool | ||
| { | ||
| rewind($handle); | ||
|
|
||
| if (! ftruncate($handle, 0)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (fwrite($handle, serialize(['owner' => $owner, 'expires' => $expires])) === false) { | ||
| return false; | ||
| } | ||
|
|
||
| return fflush($handle); | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $handle | ||
| */ | ||
| private static function clearLockFile($handle): bool | ||
| { | ||
| rewind($handle); | ||
|
|
||
| return ftruncate($handle, 0) && fflush($handle); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.