Skip to content

Commit

Permalink
feat: apply a thread safe lock over dataloaders (#1838)
Browse files Browse the repository at this point in the history
### 📝 Description
apply a synchronized lock over data loaders, the reactive stack could
cause multiple threads trying to access to the same memory.

### 🔗 Related Issues
#1837
#1819
  • Loading branch information
samuelAndalon authored Sep 11, 2023
1 parent 1401099 commit 3903664
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Expedia, Inc
* Copyright 2023 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,17 +61,23 @@ class KotlinDataLoaderRegistry(
* @return list of current completable futures.
*/
fun getCurrentFutures(): List<CompletableFuture<*>> =
registry.dataLoaders.map { it.cacheMap.all }.flatten()
synchronized(registry.dataLoaders) {
registry.dataLoaders.map { dataLoader ->
dataLoader.cacheMap.all
}.flatten()
}

/**
* This will invoke [DataLoader.dispatch] on each of the registered [DataLoader]s,
* it will start to keep track of the [CompletableFuture]s of each [DataLoader] by adding them to
* [onDispatchFutures]
*/
override fun dispatchAll() {
onDispatchFutures.clear()
onDispatchFutures.addAll(getCurrentFutures())
registry.dispatchAll()
synchronized(onDispatchFutures) {
onDispatchFutures.clear()
onDispatchFutures.addAll(getCurrentFutures())
registry.dispatchAll()
}
}

/**
Expand All @@ -82,12 +88,16 @@ class KotlinDataLoaderRegistry(
* @return weather or not all futures gathered before [dispatchAll] were handled
*/
fun onDispatchFuturesHandled(): Boolean =
onDispatchFutures.all { it.numberOfDependents == 0 }
synchronized(onDispatchFutures) {
onDispatchFutures.all { it.numberOfDependents == 0 }
}

/**
* Will signal if more dataLoaders where invoked during the [dispatchAll] invocation
* @return weather or not futures where loaded during [dispatchAll]
*/
fun dataLoadersInvokedOnDispatch(): Boolean =
getCurrentFutures().size > onDispatchFutures.size
synchronized(onDispatchFutures) {
getCurrentFutures().size > onDispatchFutures.size
}
}

0 comments on commit 3903664

Please sign in to comment.