From 8907d18d773916976d8bbbb88054fd91c42a21b4 Mon Sep 17 00:00:00 2001 From: Rui Date: Tue, 23 Apr 2024 15:45:27 -0700 Subject: [PATCH 1/2] Fixing threading issues. --- .../AbacusThreadingImp.kt | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt index 4a82d033..9c3db3c2 100644 --- a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt +++ b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt @@ -10,6 +10,7 @@ import exchange.dydx.trading.common.di.CoroutineScopes import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.invoke import kotlinx.coroutines.launch import kotlinx.coroutines.plus import javax.inject.Inject @@ -22,12 +23,32 @@ class AbacusThreadingImp @Inject constructor( @CoroutineDispatchers.IO private val ioDispatcher: CoroutineDispatcher, @CoroutineDispatchers.Default private val defaultDispatcher: CoroutineDispatcher, ) : ThreadingProtocol { + + private val mainScope = appScope + + // Abacus runs lots of computations, but needs to be run without parallelism + private val abacusScope = appScope + defaultDispatcher.limitedParallelism(1) + private val networkScope = appScope + ioDispatcher + override fun async(type: ThreadingType, block: () -> Unit) { when (type) { - main -> appScope.launch { block() } - // Abacus runs lots of computations, but needs to be run without parallelism - abacus -> appScope.launch(defaultDispatcher.limitedParallelism(1)) { block() } - network -> appScope.launch(ioDispatcher) { block() } + main -> + mainScope + .launch { + block() + } + + abacus -> + abacusScope + .launch { + block() + } + + network -> + networkScope + .launch { + block() + } } } } From b01bb62d83e9a1e0f663a0a96728c580d728948c Mon Sep 17 00:00:00 2001 From: Rui Date: Tue, 23 Apr 2024 16:24:05 -0700 Subject: [PATCH 2/2] Clean up --- .../protocolImplementations/AbacusThreadingImp.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt index 9c3db3c2..949cebdd 100644 --- a/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt +++ b/v4/integration/dydxStateManager/src/main/java/exchange/dydx/dydxstatemanager/protocolImplementations/AbacusThreadingImp.kt @@ -10,7 +10,6 @@ import exchange.dydx.trading.common.di.CoroutineScopes import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.invoke import kotlinx.coroutines.launch import kotlinx.coroutines.plus import javax.inject.Inject