Re-use existing build paths promise if possible

This commit is contained in:
Niels Andriesse 2021-07-12 10:02:00 +10:00
parent 1932fdd7cc
commit b2a067cba6

View File

@ -27,6 +27,7 @@ private typealias Path = List<Snode>
* See the "Onion Requests" section of [The Session Whitepaper](https://arxiv.org/pdf/2002.04609.pdf) for more information. * See the "Onion Requests" section of [The Session Whitepaper](https://arxiv.org/pdf/2002.04609.pdf) for more information.
*/ */
object OnionRequestAPI { object OnionRequestAPI {
private var buildPathsPromise: Promise<List<Path>, Exception>? = null
private val database: LokiAPIDatabaseProtocol private val database: LokiAPIDatabaseProtocol
get() = SnodeModule.shared.storage get() = SnodeModule.shared.storage
private val broadcaster: Broadcaster private val broadcaster: Broadcaster
@ -158,9 +159,11 @@ object OnionRequestAPI {
* enough (reliable) snodes are available. * enough (reliable) snodes are available.
*/ */
private fun buildPaths(reusablePaths: List<Path>): Promise<List<Path>, Exception> { private fun buildPaths(reusablePaths: List<Path>): Promise<List<Path>, Exception> {
val existingBuildPathsPromise = buildPathsPromise
if (existingBuildPathsPromise != null) { return existingBuildPathsPromise }
Log.d("Loki", "Building onion request paths.") Log.d("Loki", "Building onion request paths.")
broadcaster.broadcast("buildingPaths") broadcaster.broadcast("buildingPaths")
return SnodeAPI.getRandomSnode().bind { // Just used to populate the snode pool val promise = SnodeAPI.getRandomSnode().bind { // Just used to populate the snode pool
val reusableGuardSnodes = reusablePaths.map { it[0] } val reusableGuardSnodes = reusablePaths.map { it[0] }
getGuardSnodes(reusableGuardSnodes).map { guardSnodes -> getGuardSnodes(reusableGuardSnodes).map { guardSnodes ->
var unusedSnodes = SnodeAPI.snodePool.minus(guardSnodes).minus(reusablePaths.flatten()) var unusedSnodes = SnodeAPI.snodePool.minus(guardSnodes).minus(reusablePaths.flatten())
@ -183,6 +186,10 @@ object OnionRequestAPI {
paths paths
} }
} }
promise.success { buildPathsPromise = null }
promise.fail { buildPathsPromise = null }
buildPathsPromise = promise
return promise
} }
/** /**