Skip to content

[HttpCache] Hit the backend only once after waiting for the cache lock #60502

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
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\HttpKernel\HttpCache;

class CacheWasLockedException extends \Exception
{
}
16 changes: 6 additions & 10 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
$this->record($request, 'reload');
$response = $this->fetch($request, $catch);
} else {
$response = $this->lookup($request, $catch);
try {
$response = $this->lookup($request, $catch);
} catch (CacheWasLockedException) {
$response = $this->lookup($request, $catch);
}
Comment on lines +222 to +226
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows to restart the process only once.

In fact, the old code would wait for the lock to be released, but only try to acquire it later on. That could, in theory, allow for repeated cycles without actually reaching the 503 condition in lock().

Maybe it is even better this way to fail with the exception instead of silenty retrying over and over again?

}

$this->restoreResponseBody($request, $response);
Expand Down Expand Up @@ -576,15 +580,7 @@ protected function lock(Request $request, Response $entry): bool

// wait for the lock to be released
if ($this->waitForLock($request)) {
// replace the current entry with the fresh one
$new = $this->lookup($request);
$entry->headers = $new->headers;
$entry->setContent($new->getContent());
$entry->setStatusCode($new->getStatusCode());
$entry->setProtocolVersion($new->getProtocolVersion());
foreach ($new->headers->getCookies() as $cookie) {
$entry->headers->setCookie($cookie);
}
throw new CacheWasLockedException(); // unwind back to handle(), try again
} else {
// backend is slow as hell, send a 503 response (to avoid the dog pile effect)
$entry->setStatusCode(503);
Expand Down
60 changes: 60 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
Expand Down Expand Up @@ -717,6 +718,7 @@ public function testDegradationWhenCacheLocked()
*/
sleep(10);

$this->store = $this->createStore(); // create another store instance that does not hold the current lock
$this->request('GET', '/');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());
Expand All @@ -735,6 +737,64 @@ public function testDegradationWhenCacheLocked()
$this->assertEquals('Old response', $this->response->getContent());
}

public function testHitBackendOnlyOnceWhenCacheWasLocked()
{
// Disable stale-while-revalidate, it circumvents waiting for the lock
$this->cacheConfig['stale_while_revalidate'] = 0;

$this->setNextResponses([
[
'status' => 200,
'body' => 'initial response',
'headers' => [
'Cache-Control' => 'public, no-cache',
'Last-Modified' => 'some while ago',
],
],
[
'status' => 304,
'body' => '',
'headers' => [
'Cache-Control' => 'public, no-cache',
'Last-Modified' => 'some while ago',
],
],
[
'status' => 500,
'body' => 'The backend should not be called twice during revalidation',
'headers' => [],
],
]);

$this->request('GET', '/'); // warm the cache

// Use a store that simulates a cache entry being locked upon first attempt
$this->store = new class(sys_get_temp_dir() . '/http_cache') extends Store {
private bool $hasLock = false;

public function lock(Request $request): bool
{
$hasLock = $this->hasLock;
$this->hasLock = true;

return $hasLock;
}

public function isLocked(Request $request): bool
{
return false;
}
};

$this->request('GET', '/'); // hit the cache with simulated lock/concurrency block

$this->assertEquals(200, $this->response->getStatusCode());
$this->assertEquals('initial response', $this->response->getContent());

$traces = $this->cache->getTraces();
$this->assertSame(['stale', 'valid', 'store'], current($traces));
}

public function testHitsCachedResponseWithSMaxAgeDirective()
{
$time = \DateTimeImmutable::createFromFormat('U', time() - 5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class HttpCacheTestCase extends TestCase
protected $responses;
protected $catch;
protected $esi;
protected Store $store;
protected ?Store $store = null;

protected function setUp(): void
{
Expand Down Expand Up @@ -115,7 +115,9 @@ public function request($method, $uri = '/', $server = [], $cookies = [], $esi =

$this->kernel->reset();

$this->store = new Store(sys_get_temp_dir().'/http_cache');
if (! $this->store) {
$this->store = $this->createStore();
}

if (!isset($this->cacheConfig['debug'])) {
$this->cacheConfig['debug'] = true;
Expand Down Expand Up @@ -183,4 +185,9 @@ public static function clearDirectory($directory)

closedir($fp);
}

protected function createStore(): Store
{
return new Store(sys_get_temp_dir() . '/http_cache');
}
}
Loading