Skip to content

Commit a1c1593

Browse files
committed
Fixing lock behaviour
1 parent b2046c5 commit a1c1593

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

src/GlobControllerQueryProvider.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,26 @@ private function getInstancesList(): array
101101
$this->instancesList = $this->cache->get($key);
102102
if ($this->instancesList === null) {
103103
$lock = $this->lockFactory->createLock('buildInstanceList_'.$this->namespace, 5);
104-
if ($lock->isAcquired()) {
104+
if (!$lock->acquire()) {
105105
// Lock is being held right now. Generation is happening.
106106
// Let's wait and fetch the result from the cache.
107107
$lock->acquire(true);
108108
$lock->release();
109109
return $this->getInstancesList();
110110
}
111+
$lock->acquire(true);
112+
try {
113+
return $this->buildInstancesList();
114+
} finally {
115+
$lock->release();
116+
}
111117

112-
$this->instancesList = $this->lockAndBuildInstanceList($lock);
113118
$this->cache->set($key, $this->instancesList, $this->cacheTtl);
114119
}
115120
}
116121
return $this->instancesList;
117122
}
118123

119-
private function lockAndBuildInstanceList(Lock $lock): array
120-
{
121-
$lock->acquire(true);
122-
try {
123-
return $this->buildInstancesList();
124-
} finally {
125-
$lock->release();
126-
}
127-
}
128-
129124
/**
130125
* @return string[]
131126
*/

src/Mappers/GlobTypeMapper.php

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,18 @@ private function getMaps(): array
169169
$this->mapInputNameToFactory
170170
) {
171171
$lock = $this->lockFactory->createLock('buildmap_'.$this->namespace, 5);
172-
if ($lock->isAcquired()) {
172+
if (!$lock->acquire()) {
173173
// Lock is being held right now. Generation is happening.
174174
// Let's wait and fetch the result from the cache.
175175
$lock->acquire(true);
176176
$lock->release();
177177
return $this->getMaps();
178178
}
179-
$this->lockAndBuildMap($lock);
179+
try {
180+
$this->buildMap();
181+
} finally {
182+
$lock->release();
183+
}
180184
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
181185
// Defaults to 2 seconds.
182186
$this->cache->set($keyClassCache, $this->mapClassToTypeArray, $this->globTtl);
@@ -222,15 +226,19 @@ private function getMapClassToExtendTypeArray(): array
222226
$this->mapClassToExtendTypeArray = $this->cache->get($keyExtendClassCache);
223227
if ($this->mapClassToExtendTypeArray === null) {
224228
$lock = $this->lockFactory->createLock('buildmapclassextend_'.$this->namespace, 5);
225-
if ($lock->isAcquired()) {
229+
if (!$lock->acquire()) {
226230
// Lock is being held right now. Generation is happening.
227231
// Let's wait and fetch the result from the cache.
228232
$lock->acquire(true);
229233
$lock->release();
230234
return $this->getMapClassToExtendTypeArray();
231235
}
232-
233-
$this->buildMapClassToExtendTypeArray($lock);
236+
$lock->acquire(true);
237+
try {
238+
$this->buildMapClassToExtendTypeArray($lock);
239+
} finally {
240+
$lock->release();
241+
}
234242
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
235243
// Defaults to 2 seconds.
236244
$this->cache->set($keyExtendClassCache, $this->mapClassToExtendTypeArray, $this->globTtl);
@@ -248,15 +256,19 @@ private function getMapNameToExtendType(RecursiveTypeMapperInterface $recursiveT
248256
$this->mapNameToExtendType = $this->cache->get($keyExtendNameCache);
249257
if ($this->mapNameToExtendType === null) {
250258
$lock = $this->lockFactory->createLock('buildmapnameextend_'.$this->namespace, 5);
251-
if ($lock->isAcquired()) {
259+
if (!$lock->acquire()) {
252260
// Lock is being held right now. Generation is happening.
253261
// Let's wait and fetch the result from the cache.
254262
$lock->acquire(true);
255263
$lock->release();
256264
return $this->getMapNameToExtendType($recursiveTypeMapper);
257265
}
258-
259-
$this->buildMapNameToExtendTypeArray($lock, $recursiveTypeMapper);
266+
$lock->acquire(true);
267+
try {
268+
$this->buildMapNameToExtendTypeArray($recursiveTypeMapper);
269+
} finally {
270+
$lock->release();
271+
}
260272
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
261273
// Defaults to 2 seconds.
262274
$this->cache->set($keyExtendNameCache, $this->mapNameToExtendType, $this->globTtl);
@@ -292,16 +304,6 @@ private function getClassList(): array
292304
return $this->classes;
293305
}
294306

295-
private function lockAndBuildMap(Lock $lock): void
296-
{
297-
$lock->acquire(true);
298-
try {
299-
$this->buildMap();
300-
} finally {
301-
$lock->release();
302-
}
303-
}
304-
305307
private function buildMap(): void
306308
{
307309
$this->mapClassToTypeArray = [];
@@ -363,21 +365,16 @@ private function buildMapClassToExtendTypeArray(Lock $lock): void
363365
}
364366
}
365367

366-
private function buildMapNameToExtendTypeArray(Lock $lock, RecursiveTypeMapperInterface $recursiveTypeMapper): void
368+
private function buildMapNameToExtendTypeArray(RecursiveTypeMapperInterface $recursiveTypeMapper): void
367369
{
368-
$lock->acquire(true);
369-
try {
370-
$this->mapNameToExtendType = [];
371-
$classes = $this->getClassList();
372-
foreach ($classes as $className => $refClass) {
373-
$extendType = $this->annotationReader->getExtendTypeAnnotation($refClass);
370+
$this->mapNameToExtendType = [];
371+
$classes = $this->getClassList();
372+
foreach ($classes as $className => $refClass) {
373+
$extendType = $this->annotationReader->getExtendTypeAnnotation($refClass);
374374

375-
if ($extendType !== null) {
376-
$this->storeExtendTypeMapperByNameInCache($className, $extendType, $refClass->getFileName(), $recursiveTypeMapper);
377-
}
375+
if ($extendType !== null) {
376+
$this->storeExtendTypeMapperByNameInCache($className, $extendType, $refClass->getFileName(), $recursiveTypeMapper);
378377
}
379-
} finally {
380-
$lock->release();
381378
}
382379
}
383380

0 commit comments

Comments
 (0)