Skip to content

Commit 3e4474d

Browse files
committed
ref: enhance permission loading with retry mechanism for improved concurrency
1 parent a1a2d74 commit 3e4474d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/PermissionRegistrar.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function clearClassPermissions()
194194
* Thread-safe implementation to prevent race conditions in concurrent environments
195195
* (e.g., Laravel Octane, Swoole, parallel requests)
196196
*/
197-
private function loadPermissions(): void
197+
private function loadPermissions(int $retries = 0): void
198198
{
199199
// First check (without lock) - fast path for already loaded permissions
200200
if ($this->permissions) {
@@ -203,12 +203,13 @@ private function loadPermissions(): void
203203

204204
// Prevent concurrent loading using a flag-based lock
205205
// This protects against cache stampede and duplicate database queries
206-
if ($this->isLoadingPermissions) {
206+
if ($this->isLoadingPermissions && $retries < 10) {
207207
// Another thread is loading, wait and retry
208208
usleep(10000); // Wait 10ms
209+
$retries++;
209210

210211
// After wait, recursively check again if permissions were loaded
211-
$this->loadPermissions();
212+
$this->loadPermissions($retries);
212213

213214
return;
214215
}

0 commit comments

Comments
 (0)