13
13
use ReflectionClass ;
14
14
use ReflectionMethod ;
15
15
use Symfony \Component \Lock \Factory as LockFactory ;
16
+ use Symfony \Component \Lock \Lock ;
16
17
use Symfony \Component \Lock \Store \SemaphoreStore ;
17
18
use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
18
19
use TheCodingMachine \GraphQLite \AnnotationReader ;
@@ -167,7 +168,15 @@ private function getMaps(): array
167
168
$ this ->mapClassToFactory === null ||
168
169
$ this ->mapInputNameToFactory
169
170
) {
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 );
171
180
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
172
181
// Defaults to 2 seconds.
173
182
$ this ->cache ->set ($ keyClassCache , $ this ->mapClassToTypeArray , $ this ->globTtl );
@@ -212,7 +221,16 @@ private function getMapClassToExtendTypeArray(): array
212
221
$ keyExtendClassCache = 'globTypeMapperExtend_ ' .$ namespace ;
213
222
$ this ->mapClassToExtendTypeArray = $ this ->cache ->get ($ keyExtendClassCache );
214
223
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 );
216
234
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
217
235
// Defaults to 2 seconds.
218
236
$ this ->cache ->set ($ keyExtendClassCache , $ this ->mapClassToExtendTypeArray , $ this ->globTtl );
@@ -229,7 +247,16 @@ private function getMapNameToExtendType(RecursiveTypeMapperInterface $recursiveT
229
247
$ keyExtendNameCache = 'globTypeMapperExtend_names_ ' .$ namespace ;
230
248
$ this ->mapNameToExtendType = $ this ->cache ->get ($ keyExtendNameCache );
231
249
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 );
233
260
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
234
261
// Defaults to 2 seconds.
235
262
$ this ->cache ->set ($ keyExtendNameCache , $ this ->mapNameToExtendType , $ this ->globTtl );
@@ -265,9 +292,8 @@ private function getClassList(): array
265
292
return $ this ->classes ;
266
293
}
267
294
268
- private function lockAndBuildMap (): void
295
+ private function lockAndBuildMap (Lock $ lock ): void
269
296
{
270
- $ lock = $ this ->lockFactory ->createLock ('buildmap_ ' .$ this ->namespace , 5 );
271
297
$ lock ->acquire (true );
272
298
try {
273
299
$ this ->buildMap ();
@@ -319,9 +345,8 @@ private function buildMap(): void
319
345
}
320
346
}
321
347
322
- private function buildMapClassToExtendTypeArray (): void
348
+ private function buildMapClassToExtendTypeArray (Lock $ lock ): void
323
349
{
324
- $ lock = $ this ->lockFactory ->createLock ('buildmapclassextend_ ' .$ this ->namespace , 5 );
325
350
$ lock ->acquire (true );
326
351
try {
327
352
$ this ->mapClassToExtendTypeArray = [];
@@ -338,9 +363,8 @@ private function buildMapClassToExtendTypeArray(): void
338
363
}
339
364
}
340
365
341
- private function buildMapNameToExtendTypeArray (RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
366
+ private function buildMapNameToExtendTypeArray (Lock $ lock , RecursiveTypeMapperInterface $ recursiveTypeMapper ): void
342
367
{
343
- $ lock = $ this ->lockFactory ->createLock ('buildmapnameextend_ ' .$ this ->namespace , 5 );
344
368
$ lock ->acquire (true );
345
369
try {
346
370
$ this ->mapNameToExtendType = [];
0 commit comments