1313use ReflectionClass ;
1414use ReflectionMethod ;
1515use Symfony \Component \Lock \Factory as LockFactory ;
16+ use Symfony \Component \Lock \Lock ;
1617use Symfony \Component \Lock \Store \SemaphoreStore ;
1718use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
1819use TheCodingMachine \GraphQLite \AnnotationReader ;
@@ -167,7 +168,15 @@ private function getMaps(): array
167168 $ this ->mapClassToFactory === null ||
168169 $ this ->mapInputNameToFactory
169170 ) {
170- $ this ->lockAndBuildMap ();
171+ $ lock = $ this ->lockFactory ->createLock ('buildmap_ ' .$ this ->namespace , 5 );
172+ if ($ lock ->isAcquired ()) {
173+ // Lock is being held right now. Generation is happening.
174+ // Let's wait and fetch the result from the cache.
175+ $ lock ->acquire (true );
176+ $ lock ->release ();
177+ return $ this ->getMaps ();
178+ }
179+ $ this ->lockAndBuildMap ($ lock );
171180 // This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
172181 // Defaults to 2 seconds.
173182 $ this ->cache ->set ($ keyClassCache , $ this ->mapClassToTypeArray , $ this ->globTtl );
@@ -212,7 +221,16 @@ private function getMapClassToExtendTypeArray(): array
212221 $ keyExtendClassCache = 'globTypeMapperExtend_ ' .$ namespace ;
213222 $ this ->mapClassToExtendTypeArray = $ this ->cache ->get ($ keyExtendClassCache );
214223 if ($ this ->mapClassToExtendTypeArray === null ) {
215- $ this ->buildMapClassToExtendTypeArray ();
224+ $ lock = $ this ->lockFactory ->createLock ('buildmapclassextend_ ' .$ this ->namespace , 5 );
225+ if ($ lock ->isAcquired ()) {
226+ // Lock is being held right now. Generation is happening.
227+ // Let's wait and fetch the result from the cache.
228+ $ lock ->acquire (true );
229+ $ lock ->release ();
230+ return $ this ->getMapClassToExtendTypeArray ();
231+ }
232+
233+ $ this ->buildMapClassToExtendTypeArray ($ lock );
216234 // This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
217235 // Defaults to 2 seconds.
218236 $ this ->cache ->set ($ keyExtendClassCache , $ this ->mapClassToExtendTypeArray , $ this ->globTtl );
@@ -229,7 +247,16 @@ private function getMapNameToExtendType(RecursiveTypeMapperInterface $recursiveT
229247 $ keyExtendNameCache = 'globTypeMapperExtend_names_ ' .$ namespace ;
230248 $ this ->mapNameToExtendType = $ this ->cache ->get ($ keyExtendNameCache );
231249 if ($ this ->mapNameToExtendType === null ) {
232- $ this ->buildMapNameToExtendTypeArray ($ recursiveTypeMapper );
250+ $ lock = $ this ->lockFactory ->createLock ('buildmapnameextend_ ' .$ this ->namespace , 5 );
251+ if ($ lock ->isAcquired ()) {
252+ // Lock is being held right now. Generation is happening.
253+ // Let's wait and fetch the result from the cache.
254+ $ lock ->acquire (true );
255+ $ lock ->release ();
256+ return $ this ->getMapNameToExtendType ($ recursiveTypeMapper );
257+ }
258+
259+ $ this ->buildMapNameToExtendTypeArray ($ lock , $ recursiveTypeMapper );
233260 // This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
234261 // Defaults to 2 seconds.
235262 $ this ->cache ->set ($ keyExtendNameCache , $ this ->mapNameToExtendType , $ this ->globTtl );
@@ -265,9 +292,8 @@ private function getClassList(): array
265292 return $ this ->classes ;
266293 }
267294
268- private function lockAndBuildMap (): void
295+ private function lockAndBuildMap (Lock $ lock ): void
269296 {
270- $ lock = $ this ->lockFactory ->createLock ('buildmap_ ' .$ this ->namespace , 5 );
271297 $ lock ->acquire (true );
272298 try {
273299 $ this ->buildMap ();
@@ -319,9 +345,8 @@ private function buildMap(): void
319345 }
320346 }
321347
322- private function buildMapClassToExtendTypeArray (): void
348+ private function buildMapClassToExtendTypeArray (Lock $ lock ): void
323349 {
324- $ lock = $ this ->lockFactory ->createLock ('buildmapclassextend_ ' .$ this ->namespace , 5 );
325350 $ lock ->acquire (true );
326351 try {
327352 $ this ->mapClassToExtendTypeArray = [];
@@ -338,9 +363,8 @@ private function buildMapClassToExtendTypeArray(): void
338363 }
339364 }
340365
341- private function buildMapNameToExtendTypeArray (RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
366+ private function buildMapNameToExtendTypeArray (Lock $ lock , RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
342367 {
343- $ lock = $ this ->lockFactory ->createLock ('buildmapnameextend_ ' .$ this ->namespace , 5 );
344368 $ lock ->acquire (true );
345369 try {
346370 $ this ->mapNameToExtendType = [];
0 commit comments