use Friendica\Core\Cache\ICache;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
+use Friendica\Core\Lock\ILock;
use Friendica\Network\HTTPException;
/**
/** @var ICache */
private $cache;
+ /** @var ILock */
+ private $lock;
+
/** @var string */
private $baseRoutesFilepath;
* @param ICache $cache
* @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->l10n = $l10n;
$this->cache = $cache;
+ $this->lock = $lock;
$httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
$this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
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();
$this->cache->set('routerDispatchData', $routerDispatchData, Duration::DAY);
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;
use Friendica\App\Router;
use Friendica\Core\Cache\ICache;
use Friendica\Core\L10n;
+use Friendica\Core\Lock\ILock;
use Friendica\Module;
use Friendica\Network\HTTPException\MethodNotAllowedException;
use Friendica\Network\HTTPException\NotFoundException;
* @var ICache
*/
private $cache;
+ /**
+ * @var ILock
+ */
+ private $lock;
protected function setUp() : void
{
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->addRoute([Router::GET], '/', 'IndexModuleClassName');
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->addRoute([Router::POST], '/', 'IndexModuleClassName');
{
$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');
}
{
$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->addRoute([Router::GET], '/test', 'TestModuleClassName');
{
$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->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
{
$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->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
{
$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->addRoute([Router::GET], '/test', 'TestModuleClassName');
{
$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->addRoute([Router::POST], '/test', 'TestModuleClassName');
['REQUEST_METHOD' => Router::GET],
'',
$this->l10n,
- $this->cache
+ $this->cache,
+ $this->lock
))->loadRoutes($routes);
self::assertEquals(Module\Home::class, $router->getModuleClass('/'));
{
$router = (new Router([
'REQUEST_METHOD' => Router::POST
- ], '', $this->l10n, $this->cache))->loadRoutes($routes);
+ ], '', $this->l10n, $this->cache, $this->lock))->loadRoutes($routes);
// Don't find GET
self::assertEquals(Module\WellKnown\NodeInfo::class, $router->getModuleClass('/post/it'));