@@ -3,6 +3,7 @@ package org.modelix.model.client2
3
3
import kotlinx.coroutines.CoroutineScope
4
4
import kotlinx.coroutines.Dispatchers
5
5
import kotlinx.coroutines.cancel
6
+ import kotlinx.coroutines.delay
6
7
import kotlinx.coroutines.launch
7
8
import kotlinx.coroutines.sync.Mutex
8
9
import kotlinx.coroutines.sync.withLock
@@ -50,13 +51,21 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
50
51
otBranch = OTBranch (rawBranch, client.getIdGenerator(), lastRemoteVersion.store)
51
52
52
53
scope.launch {
54
+ var nextDelayMs: Long = 0
53
55
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
+ }
60
69
}
61
70
}
62
71
0 commit comments