Lock before recreate / fix cache key misspelling

This commit is contained in:
Michael 2021-07-25 04:31:48 +00:00
parent e05c1821c2
commit 2aa2c61479
2 changed files with 32 additions and 12 deletions

View file

@ -30,6 +30,7 @@ use Friendica\Core\Cache\Duration;
use Friendica\Core\Cache\ICache; use Friendica\Core\Cache\ICache;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Lock\ILock;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
/** /**
@ -79,6 +80,9 @@ class Router
/** @var ICache */ /** @var ICache */
private $cache; private $cache;
/** @var ILock */
private $lock;
/** @var string */ /** @var string */
private $baseRoutesFilepath; private $baseRoutesFilepath;
@ -89,11 +93,12 @@ class Router
* @param ICache $cache * @param ICache $cache
* @param RouteCollector|null $routeCollector * @param RouteCollector|null $routeCollector
*/ */
public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICache $cache, RouteCollector $routeCollector = null) public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICache $cache, ILock $lock, RouteCollector $routeCollector = null)
{ {
$this->baseRoutesFilepath = $baseRoutesFilepath; $this->baseRoutesFilepath = $baseRoutesFilepath;
$this->l10n = $l10n; $this->l10n = $l10n;
$this->cache = $cache; $this->cache = $cache;
$this->lock = $lock;
$httpMethod = $server['REQUEST_METHOD'] ?? self::GET; $httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
$this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET; $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
@ -301,11 +306,20 @@ class Router
return $routerDispatchData; return $routerDispatchData;
} }
if (!$this->lock->acquire('getCachedDispatchData', 0)) {
// Immediately return uncached data when we can't aquire a lock
return $this->getDispatchData();
}
$routerDispatchData = $this->getDispatchData(); $routerDispatchData = $this->getDispatchData();
$this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY); $this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
if (!empty($routesFileModifiedTime)) { if (!empty($routesFileModifiedTime)) {
$this->cache->set('lastRoutesFileMtime', $routesFileModifiedTime, Duration::MONTH); $this->cache->set('lastRoutesFileModifiedTime', $routesFileModifiedTime, Duration::MONTH);
}
if ($this->lock->isLocked('getCachedDispatchData')) {
$this->lock->release('getCachedDispatchData');
} }
return $routerDispatchData; return $routerDispatchData;

View file

@ -24,6 +24,7 @@ namespace Friendica\Test\src\App;
use Friendica\App\Router; use Friendica\App\Router;
use Friendica\Core\Cache\ICache; use Friendica\Core\Cache\ICache;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Lock\ILock;
use Friendica\Module; use Friendica\Module;
use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\MethodNotAllowedException;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
@ -39,6 +40,10 @@ class RouterTest extends TestCase
* @var ICache * @var ICache
*/ */
private $cache; private $cache;
/**
* @var ILock
*/
private $lock;
protected function setUp() : void protected function setUp() : void
{ {
@ -54,7 +59,7 @@ class RouterTest extends TestCase
public function testGetModuleClass() public function testGetModuleClass()
{ {
$router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName'); $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
@ -78,7 +83,7 @@ class RouterTest extends TestCase
public function testPostModuleClass() public function testPostModuleClass()
{ {
$router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName'); $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
@ -104,7 +109,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
$router->getModuleClass('/unsupported'); $router->getModuleClass('/unsupported');
} }
@ -113,7 +118,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
@ -125,7 +130,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName'); $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
@ -137,7 +142,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(NotFoundException::class); $this->expectException(NotFoundException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName'); $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
@ -149,7 +154,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(MethodNotAllowedException::class); $this->expectException(MethodNotAllowedException::class);
$router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
@ -161,7 +166,7 @@ class RouterTest extends TestCase
{ {
$this->expectException(MethodNotAllowedException::class); $this->expectException(MethodNotAllowedException::class);
$router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache); $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache, $this->lock);
$routeCollector = $router->getRouteCollector(); $routeCollector = $router->getRouteCollector();
$routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName'); $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
@ -203,7 +208,8 @@ class RouterTest extends TestCase
['REQUEST_METHOD' => Router::GET], ['REQUEST_METHOD' => Router::GET],
'', '',
$this->l10n, $this->l10n,
$this->cache $this->cache,
$this->lock
))->loadRoutes($routes); ))->loadRoutes($routes);
self::assertEquals(Module\Home::class, $router->getModuleClass('/')); self::assertEquals(Module\Home::class, $router->getModuleClass('/'));
@ -219,7 +225,7 @@ class RouterTest extends TestCase
{ {
$router = (new Router([ $router = (new Router([
'REQUEST_METHOD' => Router::POST 'REQUEST_METHOD' => Router::POST
], '', $this->l10n, $this->cache))->loadRoutes($routes); ], '', $this->l10n, $this->cache, $this->lock))->loadRoutes($routes);
// Don't find GET // Don't find GET
self::assertEquals(Module\WellKnown\NodeInfo::class, $router->getModuleClass('/post/it')); self::assertEquals(Module\WellKnown\NodeInfo::class, $router->getModuleClass('/post/it'));