[#940] Improve reliability of erasing SDK

* [#940] Improve reliability of erasing SDK

* Fix Ktlint warning

---------

Co-authored-by: Honza <rychnovsky.honza@gmail.com>
This commit is contained in:
Carter Jernigan 2023-06-08 02:46:24 -04:00 committed by GitHub
parent dba46c266c
commit 61c83989d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 15 deletions

View File

@ -68,6 +68,8 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.io.File import java.io.File
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -104,6 +106,8 @@ class SdkSynchronizer private constructor(
private val instances: MutableMap<SynchronizerKey, InstanceState> = private val instances: MutableMap<SynchronizerKey, InstanceState> =
ConcurrentHashMap<SynchronizerKey, InstanceState>() ConcurrentHashMap<SynchronizerKey, InstanceState>()
private val mutex = Mutex()
/** /**
* @throws IllegalStateException If multiple instances of synchronizer with the same network+alias are * @throws IllegalStateException If multiple instances of synchronizer with the same network+alias are
* active at the same time. Call `close` to finish one synchronizer before starting another one with the same * active at the same time. Call `close` to finish one synchronizer before starting another one with the same
@ -120,19 +124,20 @@ class SdkSynchronizer private constructor(
): CloseableSynchronizer { ): CloseableSynchronizer {
val synchronizerKey = SynchronizerKey(zcashNetwork, alias) val synchronizerKey = SynchronizerKey(zcashNetwork, alias)
waitForShutdown(synchronizerKey) return mutex.withLock {
checkForExistingSynchronizers(synchronizerKey) waitForShutdown(synchronizerKey)
checkForExistingSynchronizers(synchronizerKey)
return SdkSynchronizer( SdkSynchronizer(
synchronizerKey, synchronizerKey,
repository, repository,
txManager, txManager,
processor, processor,
backend backend
).apply { ).apply {
instances[synchronizerKey] = InstanceState.Active instances[synchronizerKey] = InstanceState.Active
start()
start() }
} }
} }
@ -158,10 +163,12 @@ class SdkSynchronizer private constructor(
): Boolean { ): Boolean {
val key = SynchronizerKey(network, alias) val key = SynchronizerKey(network, alias)
waitForShutdown(key) return mutex.withLock {
checkForExistingSynchronizers(key) waitForShutdown(key)
checkForExistingSynchronizers(key)
return DatabaseCoordinator.getInstance(appContext).deleteDatabases(network, alias) DatabaseCoordinator.getInstance(appContext).deleteDatabases(network, alias)
}
} }
} }