Skip to content

Commit 13c91fb

Browse files
feat: apply a thread safe lock over dataloaders (#1838) (#1840)
cherry pick #1838
1 parent 5341525 commit 13c91fb

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

executions/graphql-kotlin-dataloader/src/main/kotlin/com/expediagroup/graphql/dataloader/KotlinDataLoaderRegistry.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Expedia, Inc
2+
* Copyright 2023 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,17 +61,23 @@ class KotlinDataLoaderRegistry(
6161
* @return list of current completable futures.
6262
*/
6363
fun getCurrentFutures(): List<CompletableFuture<*>> =
64-
registry.dataLoaders.map { it.cacheMap.all }.flatten()
64+
synchronized(registry.dataLoaders) {
65+
registry.dataLoaders.map { dataLoader ->
66+
dataLoader.cacheMap.all
67+
}.flatten()
68+
}
6569

6670
/**
6771
* This will invoke [DataLoader.dispatch] on each of the registered [DataLoader]s,
6872
* it will start to keep track of the [CompletableFuture]s of each [DataLoader] by adding them to
6973
* [onDispatchFutures]
7074
*/
7175
override fun dispatchAll() {
72-
onDispatchFutures.clear()
73-
onDispatchFutures.addAll(getCurrentFutures())
74-
registry.dispatchAll()
76+
synchronized(onDispatchFutures) {
77+
onDispatchFutures.clear()
78+
onDispatchFutures.addAll(getCurrentFutures())
79+
registry.dispatchAll()
80+
}
7581
}
7682

7783
/**
@@ -82,12 +88,16 @@ class KotlinDataLoaderRegistry(
8288
* @return weather or not all futures gathered before [dispatchAll] were handled
8389
*/
8490
fun onDispatchFuturesHandled(): Boolean =
85-
onDispatchFutures.all { it.numberOfDependents == 0 }
91+
synchronized(onDispatchFutures) {
92+
onDispatchFutures.all { it.numberOfDependents == 0 }
93+
}
8694

8795
/**
8896
* Will signal if more dataLoaders where invoked during the [dispatchAll] invocation
8997
* @return weather or not futures where loaded during [dispatchAll]
9098
*/
9199
fun dataLoadersInvokedOnDispatch(): Boolean =
92-
getCurrentFutures().size > onDispatchFutures.size
100+
synchronized(onDispatchFutures) {
101+
getCurrentFutures().size > onDispatchFutures.size
102+
}
93103
}

0 commit comments

Comments
 (0)