Skip to content
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

[5.x] Handle lock timeout in cache middleware #10607

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
16 changes: 15 additions & 1 deletion src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Cache\Lock;
use Illuminate\Contracts\Cache\LockTimeoutException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -57,7 +58,11 @@ public function handle($request, Closure $next)

$lock = $this->createLock($request);

return $lock->block($this->lockFor, fn () => $this->handleRequest($request, $next));
try {
return $lock->block($this->lockFor, fn () => $this->handleRequest($request, $next));
} catch (LockTimeoutException $e) {
return $this->outputRefreshResponse($request);
}
}

private function handleRequest($request, Closure $next)
Expand Down Expand Up @@ -191,4 +196,13 @@ public static function isBeingUsedOnCurrentRoute()
{
return in_array(static::class, app('router')->gatherRouteMiddleware(request()->route()));
}

private function outputRefreshResponse($request)
{
$html = $request->ajax() || $request->wantsJson()
? __('Service Unavailable')
: sprintf('<meta http-equiv="refresh" content="1; URL=\'%s\'" />', $request->getUri());

return response($html, 503, ['Retry-After' => 1]);
}
}
Loading