Skip to content

Commit 4f99c4a

Browse files
committed
feat: add sync time out
1 parent f7322d0 commit 4f99c4a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.first
1717
import kotlinx.coroutines.flow.update
1818
import kotlinx.coroutines.tasks.await
1919
import kotlinx.coroutines.withContext
20+
import kotlinx.coroutines.withTimeout
2021
import kotlinx.coroutines.withTimeoutOrNull
2122
import org.lightningdevkit.ldknode.Address
2223
import org.lightningdevkit.ldknode.BalanceDetails
@@ -57,6 +58,8 @@ import kotlin.time.Duration
5758
import kotlin.time.Duration.Companion.minutes
5859
import kotlin.time.Duration.Companion.seconds
5960

61+
private const val SYNC_TIMEOUT_MS = 10_000L
62+
6063
@Singleton
6164
class LightningRepo @Inject constructor(
6265
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
@@ -260,7 +263,10 @@ class LightningRepo @Inject constructor(
260263
if (_lightningState.value.isSyncingWallet) {
261264
Logger.warn("Sync already in progress, waiting for existing sync.", context = TAG)
262265
}
263-
_lightningState.first { !it.isSyncingWallet }
266+
267+
withTimeout(SYNC_TIMEOUT_MS) {
268+
_lightningState.first { !it.isSyncingWallet }
269+
}
264270

265271
_lightningState.update { it.copy(isSyncingWallet = true) }
266272
lightningService.sync()

app/src/main/java/to/bitkit/repositories/WalletRepo.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.synonym.bitkitcore.decode
66
import kotlinx.coroutines.CoroutineDispatcher
77
import kotlinx.coroutines.CoroutineScope
88
import kotlinx.coroutines.SupervisorJob
9+
import kotlinx.coroutines.TimeoutCancellationException
910
import kotlinx.coroutines.flow.MutableStateFlow
1011
import kotlinx.coroutines.flow.asStateFlow
1112
import kotlinx.coroutines.flow.filter
@@ -152,6 +153,9 @@ class WalletRepo @Inject constructor(
152153
syncBalances()
153154
return@withContext Result.success(Unit)
154155
}.onFailure { e ->
156+
if (e is TimeoutCancellationException) {
157+
syncBalances()
158+
}
155159
return@withContext Result.failure(e)
156160
}
157161
}

0 commit comments

Comments
 (0)