-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for automatic resetting incremental backoff when network status changes #1491
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f43451b
Add support for automatic resetting incremental backoff when network …
8b62406
Formatting
72a3002
Rework how to detect and trigger reconnect
f2d51ca
Formatting
b30ef43
improved documentation
5098cf8
More cleanup
4fca3b2
Formatting
40dd940
Remove waiting for connections to close
67870d7
Try to find problem with tests
0489ad5
Increase wait
10c6d83
Formatting
f01d7b2
Disable new tests
36e2fb6
Disable network listener to see if it makes a difference
eeaf7a8
Detekt
e6ad49d
Refactor thread check tests to reduce flakyness.
5543f9e
Add debugging ids to all test apps.
997ece2
Formatting
cb435d1
Fix test
fe40d5a
PR feedback
d6738d7
PR feedback + formatting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import io.realm.kotlin.internal.interop.RealmAppPointer | |
import io.realm.kotlin.internal.interop.RealmInterop | ||
import io.realm.kotlin.internal.interop.RealmUserPointer | ||
import io.realm.kotlin.internal.interop.sync.NetworkTransport | ||
import io.realm.kotlin.internal.toDuration | ||
import io.realm.kotlin.internal.util.DispatcherHolder | ||
import io.realm.kotlin.internal.util.Validation | ||
import io.realm.kotlin.internal.util.use | ||
|
@@ -36,6 +37,8 @@ import kotlinx.coroutines.channels.BufferOverflow | |
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
internal typealias AppResources = Triple<DispatcherHolder, NetworkTransport, RealmAppPointer> | ||
|
||
|
@@ -48,10 +51,10 @@ public class AppImpl( | |
internal val appNetworkDispatcher: DispatcherHolder | ||
private val networkTransport: NetworkTransport | ||
|
||
private var lastOnlineStateReportedMs: Long? = null | ||
private var lastOnlineStateReported: Duration? = null | ||
private var lastConnectedState: Boolean? = null // null = unknown, true = connected, false = disconnected | ||
@Suppress("MagicNumber") | ||
private val reconnectThresholdMs = 5_000 // 5 seconds | ||
private val reconnectThreshold = 5.seconds | ||
|
||
@Suppress("invisible_member", "invisible_reference", "MagicNumber") | ||
private val connectionListener = NetworkStateObserver.ConnectionListener { connectionAvailable -> | ||
|
@@ -65,18 +68,16 @@ public class AppImpl( | |
// "isOnline" messages in short order. So in order to prevent resetting the network | ||
// too often we throttle messages, so a reconnect can only happen ever 5 seconds. | ||
RealmLog.debug("Network state change detected. ConnectionAvailable = $connectionAvailable") | ||
val nowMs: Long = RealmInstant.now().let { timestamp -> | ||
timestamp.epochSeconds * 1000L + timestamp.nanosecondsOfSecond / 1_000_000L | ||
} | ||
if (connectionAvailable && (lastOnlineStateReportedMs == null || nowMs - lastOnlineStateReportedMs!! > reconnectThresholdMs) | ||
val now: Duration = RealmInstant.now().toDuration() | ||
if (connectionAvailable && (lastOnlineStateReported == null || now.minus(lastOnlineStateReported!!) > reconnectThreshold) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 ... I guess you could make it more Kotlin idiomatic with something like
but don't mind keeping it as is. |
||
) { | ||
RealmLog.info("Trigger network reconnect.") | ||
try { | ||
sync.reconnect() | ||
} catch (ex: Exception) { | ||
RealmLog.error(ex.toString()) | ||
} | ||
lastOnlineStateReportedMs = nowMs | ||
lastOnlineStateReported = now | ||
} | ||
lastConnectedState = connectionAvailable | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR, but we could consider to make this a public-public extension.