Skip to content

Commit 3c77848

Browse files
committed
fix(model-client): polling for versions didn't continue after an exception
1 parent 148606c commit 3c77848

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

model-client/src/commonMain/kotlin/org/modelix/model/client2/ReplicatedModel.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.modelix.model.client2
33
import kotlinx.coroutines.CoroutineScope
44
import kotlinx.coroutines.Dispatchers
55
import kotlinx.coroutines.cancel
6+
import kotlinx.coroutines.delay
67
import kotlinx.coroutines.launch
78
import kotlinx.coroutines.sync.Mutex
89
import kotlinx.coroutines.sync.withLock
@@ -50,13 +51,21 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
5051
otBranch = OTBranch(rawBranch, client.getIdGenerator(), lastRemoteVersion.store)
5152

5253
scope.launch {
54+
var nextDelayMs: Long = 0
5355
while (state != State.Disposed) {
54-
val newRemoteVersion = if (query == null) {
55-
client.poll(branchRef, lastRemoteVersion)
56-
} else {
57-
client.poll(branchRef, lastRemoteVersion, query)
58-
} as CLVersion
59-
remoteVersionReceived(newRemoteVersion)
56+
if (nextDelayMs > 0) delay(nextDelayMs)
57+
try {
58+
val newRemoteVersion = if (query == null) {
59+
client.poll(branchRef, lastRemoteVersion)
60+
} else {
61+
client.poll(branchRef, lastRemoteVersion, query)
62+
} as CLVersion
63+
remoteVersionReceived(newRemoteVersion)
64+
nextDelayMs = 0
65+
} catch (ex: Throwable) {
66+
LOG.error(ex) { "Failed to poll branch $branchRef" }
67+
nextDelayMs = (nextDelayMs * 3 / 2).coerceIn(1000, 30000)
68+
}
6069
}
6170
}
6271

0 commit comments

Comments
 (0)