Skip to content

Adding a lock mechanism on cache rebuild #29

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

Merged
merged 1 commit into from
Apr 3, 2019
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"phpdocumentor/reflection-docblock": "^4.3",
"phpdocumentor/type-resolver": "^0.4",
"psr/http-message": "^1",
"ecodev/graphql-upload": "^4.0"
"ecodev/graphql-upload": "^4.0",
"symfony/lock": "^3 || ^4"
},
"require-dev": {
"phpunit/phpunit": "^6.1",
Expand Down
22 changes: 21 additions & 1 deletion src/GlobControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Mouf\Composer\ClassNameMapper;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Lock\Store\SemaphoreStore;
use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
use Symfony\Component\Lock\Factory as LockFactory;

/**
* Scans all the classes in a given namespace of the main project (not the vendor directory).
Expand Down Expand Up @@ -53,6 +55,10 @@ final class GlobControllerQueryProvider implements QueryProviderInterface
* @var bool
*/
private $recursive;
/**
* @var LockFactory
*/
private $lockFactory;

/**
* @param string $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation)
Expand All @@ -72,6 +78,9 @@ public function __construct(string $namespace, FieldsBuilderFactory $fieldsBuild
$this->fieldsBuilderFactory = $fieldsBuilderFactory;
$this->recursiveTypeMapper = $recursiveTypeMapper;
$this->recursive = $recursive;
$store = new SemaphoreStore();
$this->lockFactory = new LockFactory($store);

}

private function getAggregateControllerQueryProvider(): AggregateControllerQueryProvider
Expand All @@ -93,13 +102,24 @@ private function getInstancesList(): array
$key = 'globQueryProvider_'.str_replace('\\', '_', $this->namespace);
$this->instancesList = $this->cache->get($key);
if ($this->instancesList === null) {
$this->instancesList = $this->buildInstancesList();
$this->instancesList = $this->lockAndBuildInstanceList();
$this->cache->set($key, $this->instancesList, $this->cacheTtl);
}
}
return $this->instancesList;
}

private function lockAndBuildInstanceList(): array
{
$lock = $this->lockFactory->createLock('buildInstanceList_'.$this->namespace, 5);
$lock->acquire(true);
try {
return $this->buildInstancesList();
} finally {
$lock->release();
}
}

/**
* @return string[]
*/
Expand Down
57 changes: 44 additions & 13 deletions src/Mappers/GlobTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Psr\SimpleCache\CacheInterface;
use ReflectionClass;
use ReflectionMethod;
use Symfony\Component\Lock\Factory as LockFactory;
use Symfony\Component\Lock\Store\SemaphoreStore;
use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer;
use TheCodingMachine\GraphQLite\AnnotationReader;
use TheCodingMachine\GraphQLite\Annotations\ExtendType;
Expand Down Expand Up @@ -119,6 +121,10 @@ final class GlobTypeMapper implements TypeMapperInterface
* @var bool
*/
private $recursive;
/**
* @var LockFactory
*/
private $lockFactory;

/**
* @param string $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation)
Expand All @@ -136,6 +142,8 @@ public function __construct(string $namespace, TypeGenerator $typeGenerator, Inp
$this->inputTypeGenerator = $inputTypeGenerator;
$this->inputTypeUtils = $inputTypeUtils;
$this->recursive = $recursive;
$store = new SemaphoreStore();
$this->lockFactory = new LockFactory($store);
}

/**
Expand All @@ -160,7 +168,7 @@ private function getMaps(): array
$this->mapClassToFactory === null ||
$this->mapInputNameToFactory
) {
$this->buildMap();
$this->lockAndBuildMap();
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
// Defaults to 2 seconds.
$this->cache->set($keyClassCache, $this->mapClassToTypeArray, $this->globTtl);
Expand Down Expand Up @@ -258,6 +266,17 @@ private function getClassList(): array
return $this->classes;
}

private function lockAndBuildMap(): void
{
$lock = $this->lockFactory->createLock('buildmap_'.$this->namespace, 5);
$lock->acquire(true);
try {
$this->buildMap();
} finally {
$lock->release();
}
}

private function buildMap(): void
{
$this->mapClassToTypeArray = [];
Expand Down Expand Up @@ -303,27 +322,39 @@ private function buildMap(): void

private function buildMapClassToExtendTypeArray(): void
{
$this->mapClassToExtendTypeArray = [];
$classes = $this->getClassList();
foreach ($classes as $className => $refClass) {
$extendType = $this->annotationReader->getExtendTypeAnnotation($refClass);
$lock = $this->lockFactory->createLock('buildmapclassextend_'.$this->namespace, 5);
$lock->acquire(true);
try {
$this->mapClassToExtendTypeArray = [];
$classes = $this->getClassList();
foreach ($classes as $className => $refClass) {
$extendType = $this->annotationReader->getExtendTypeAnnotation($refClass);

if ($extendType !== null) {
$this->storeExtendTypeMapperByClassInCache($className, $extendType, $refClass->getFileName());
if ($extendType !== null) {
$this->storeExtendTypeMapperByClassInCache($className, $extendType, $refClass->getFileName());
}
}
} finally {
$lock->release();
}
}

private function buildMapNameToExtendTypeArray(RecursiveTypeMapperInterface $recursiveTypeMapper): void
{
$this->mapNameToExtendType = [];
$classes = $this->getClassList();
foreach ($classes as $className => $refClass) {
$extendType = $this->annotationReader->getExtendTypeAnnotation($refClass);
$lock = $this->lockFactory->createLock('buildmapnameextend_'.$this->namespace, 5);
$lock->acquire(true);
try {
$this->mapNameToExtendType = [];
$classes = $this->getClassList();
foreach ($classes as $className => $refClass) {
$extendType = $this->annotationReader->getExtendTypeAnnotation($refClass);

if ($extendType !== null) {
$this->storeExtendTypeMapperByNameInCache($className, $extendType, $refClass->getFileName(), $recursiveTypeMapper);
if ($extendType !== null) {
$this->storeExtendTypeMapperByNameInCache($className, $extendType, $refClass->getFileName(), $recursiveTypeMapper);
}
}
} finally {
$lock->release();
}
}

Expand Down