From 2751306a85fd1079453f181e2cf4539cbef0af24 Mon Sep 17 00:00:00 2001 From: Milan Cerovsky Date: Wed, 14 Aug 2024 09:42:17 +0200 Subject: [PATCH 1/2] Tor initialization and disposal --- .../cash/z/ecc/android/sdk/SdkSynchronizer.kt | 1 + .../exchange/UsdExchangeRateFetcher.kt | 42 ++++++++++++------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt index c34e5cc2..a2806b0c 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt @@ -370,6 +370,7 @@ class SdkSynchronizer private constructor( coroutineScope.launch { Twig.info { "Stopping synchronizer $synchronizerKey…" } processor.stop() + fetchExchangeChangeUsd.dispose() } instances[synchronizerKey] = InstanceState.ShuttingDown(shutdownJob) diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt index 4b01b654..98eeb20a 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt @@ -5,33 +5,25 @@ import cash.z.ecc.android.sdk.internal.model.TorClient import cash.z.ecc.android.sdk.model.FetchFiatCurrencyResult import cash.z.ecc.android.sdk.model.FiatCurrencyConversion import kotlinx.coroutines.delay +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.datetime.Clock import java.io.File -internal class UsdExchangeRateFetcher(private val torDir: File) { +internal class UsdExchangeRateFetcher(torDir: File) { + + private val torHolder = TorClientHolder(torDir) + @Suppress("TooGenericExceptionCaught", "ReturnCount") suspend operator fun invoke(): FetchFiatCurrencyResult { - val tor = - retry { - try { - Twig.info { "[USD] Tor client bootstrap" } - TorClient.new(torDir) - } catch (e: Exception) { - Twig.error(e) { "[USD] To client bootstrap failed" } - return FetchFiatCurrencyResult.Error(exception = e) - } - } - return retry { val rate = try { Twig.info { "[USD] Fetch start" } - tor.getExchangeRateUsd() + torHolder().getExchangeRateUsd() } catch (e: Exception) { Twig.error(e) { "[USD] Fetch failed" } return FetchFiatCurrencyResult.Error(e) - } finally { - tor.dispose() } Twig.debug { "[USD] Fetch success: $rate" } @@ -46,6 +38,10 @@ internal class UsdExchangeRateFetcher(private val torDir: File) { } } + suspend fun dispose() { + torHolder.dispose() + } + /** * Retry with geometric order. */ @@ -69,3 +65,19 @@ internal class UsdExchangeRateFetcher(private val torDir: File) { return block() // last attempt } } + +private class TorClientHolder(private val torDir: File) { + private val mutex = Mutex() + private var torClient: TorClient? = null + + suspend operator fun invoke(): TorClient = mutex.withLock { + if (torClient == null) { + torClient = TorClient.new(torDir) + } + return torClient!! + } + + suspend fun dispose() = mutex.withLock { + torClient?.dispose() + } +} From 862c08bb827ab57816b6e39ecf866033b5062f08 Mon Sep 17 00:00:00 2001 From: Milan Cerovsky Date: Wed, 14 Aug 2024 11:23:32 +0200 Subject: [PATCH 2/2] Code cleanup --- .../exchange/UsdExchangeRateFetcher.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt index 98eeb20a..2fcc520d 100644 --- a/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt +++ b/sdk-lib/src/main/java/cash/z/ecc/android/sdk/internal/exchange/UsdExchangeRateFetcher.kt @@ -11,7 +11,6 @@ import kotlinx.datetime.Clock import java.io.File internal class UsdExchangeRateFetcher(torDir: File) { - private val torHolder = TorClientHolder(torDir) @Suppress("TooGenericExceptionCaught", "ReturnCount") @@ -70,14 +69,16 @@ private class TorClientHolder(private val torDir: File) { private val mutex = Mutex() private var torClient: TorClient? = null - suspend operator fun invoke(): TorClient = mutex.withLock { - if (torClient == null) { - torClient = TorClient.new(torDir) + suspend operator fun invoke(): TorClient = + mutex.withLock { + if (torClient == null) { + torClient = TorClient.new(torDir) + } + return torClient!! } - return torClient!! - } - suspend fun dispose() = mutex.withLock { - torClient?.dispose() - } + suspend fun dispose() = + mutex.withLock { + torClient?.dispose() + } }