-
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 18 commits
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
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
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
6 changes: 6 additions & 0 deletions
6
...rary-sync/src/androidMain/kotlin/io/realm/kotlin/mongodb/internal/NetworkStateObserver.kt
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.realm.kotlin.mongodb.internal | ||
|
||
internal actual fun registerSystemNetworkObserver() { | ||
// Registering network state listeners are done in io.realm.kotlin.mongodb.RealmSyncInitializer | ||
// so we do not have to store the Android Context. | ||
} |
118 changes: 118 additions & 0 deletions
118
...rary-sync/src/androidMain/kotlin/io/realm/kotlin/mongodb/internal/RealmSyncInitializer.kt
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2021 Realm Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.realm.kotlin.mongodb.internal | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.ConnectivityManager | ||
import android.net.ConnectivityManager.NetworkCallback | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkInfo | ||
import android.net.NetworkRequest | ||
import android.os.Build | ||
import androidx.startup.Initializer | ||
import io.realm.kotlin.internal.RealmInitializer | ||
import io.realm.kotlin.log.RealmLog | ||
|
||
/** | ||
* An **initializer** for Sync specific functionality that does not fit into the `RealmInitializer` | ||
* in cinterop.o allow Realm to access context properties. | ||
*/ | ||
class RealmSyncInitializer : Initializer<Context> { | ||
|
||
companion object { | ||
@Suppress("DEPRECATION") // Should only be called below API 21 | ||
fun isConnected(cm: ConnectivityManager?): Boolean { | ||
return cm?.let { | ||
val networkInfo: NetworkInfo? = cm.activeNetworkInfo | ||
networkInfo != null && networkInfo.isConnectedOrConnecting || isEmulator() | ||
} ?: true | ||
} | ||
|
||
// Credit: http://stackoverflow.com/questions/2799097/how-can-i-detect-when-an-android-application-is-running-in-the-emulator | ||
fun isEmulator(): Boolean { | ||
return Build.FINGERPRINT.startsWith("generic") || | ||
Build.FINGERPRINT.startsWith("unknown") || | ||
Build.MODEL.contains("google_sdk") || | ||
Build.MODEL.contains("Emulator") || | ||
Build.MODEL.contains("Android SDK built for x86") || | ||
Build.MANUFACTURER.contains("Genymotion") || | ||
(Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) || | ||
"google_sdk" == Build.PRODUCT | ||
} | ||
} | ||
|
||
private var connectivityManager: ConnectivityManager? = null | ||
|
||
override fun create(context: Context): Context { | ||
connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? | ||
// There has been a fair amount of changes and deprecations with regard to how to listen | ||
// to the network status. ConnectivityManager#CONNECTIVITY_ACTION was deprecated in API 28 | ||
// but ConnectivityManager.NetworkCallback became available a lot sooner in API 21, so | ||
// we default to this as soon as possible. | ||
// | ||
// On later versions of Android (need reference), these callbacks will also only trigger | ||
// if the app is in the foreground. | ||
// | ||
// The current implementation is a best-effort in detecting when the network is available | ||
// again. | ||
// | ||
// See https://developer.android.com/training/basics/network-ops/reading-network-state | ||
// See https://developer.android.com/reference/android/net/ConnectivityManager#CONNECTIVITY_ACTION | ||
// See https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /* 21 */) { | ||
val request = NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /* 23 */) { | ||
request.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
} | ||
@Suppress("invisible_member", "invisible_reference") | ||
RealmLog.info("Register ConnectivityManager network callbacks") | ||
connectivityManager?.registerNetworkCallback( | ||
request.build(), | ||
object : NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
NetworkStateObserver.notifyConnectionChange(true) | ||
} | ||
override fun onUnavailable() { | ||
NetworkStateObserver.notifyConnectionChange(false) | ||
} | ||
} | ||
) | ||
} else { | ||
@Suppress("invisible_member", "invisible_reference") | ||
RealmLog.info("Register BroadcastReceiver connectivity callbacks") | ||
@Suppress("DEPRECATION") | ||
context.registerReceiver( | ||
object : BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
val isConnected: Boolean = isConnected(connectivityManager) | ||
NetworkStateObserver.notifyConnectionChange(isConnected) | ||
} | ||
}, | ||
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) | ||
) | ||
} | ||
return context | ||
} | ||
|
||
override fun dependencies(): MutableList<Class<out Initializer<*>>> { | ||
return mutableListOf(RealmInitializer::class.java) | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
...brary-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/NetworkStateObserver.kt
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.realm.kotlin.mongodb.internal | ||
|
||
import io.realm.kotlin.internal.interop.SynchronizableObject | ||
|
||
// Register a system specific network listener (if supported) | ||
internal expect fun registerSystemNetworkObserver() | ||
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 understand the idea, but since all implementations are no-ops, does it then make sense to have it at all 🤔 |
||
|
||
/** | ||
* This class is responsible for keeping track of system events related to the network so it can | ||
* delegate them to interested parties. | ||
*/ | ||
internal object NetworkStateObserver { | ||
|
||
/** | ||
* This interface is used in a thread-safe manner, i.e. implementers do not have to think | ||
* about race conditions. | ||
*/ | ||
internal fun interface ConnectionListener { | ||
fun onChange(connectionAvailable: Boolean) | ||
} | ||
|
||
private val mutex = SynchronizableObject() | ||
private val listeners = mutableListOf<ConnectionListener>() | ||
|
||
init { | ||
registerSystemNetworkObserver() | ||
} | ||
|
||
/** | ||
* Called by each custom network implementation whenever a network change is detected. | ||
*/ | ||
fun notifyConnectionChange(isOnline: Boolean) { | ||
mutex.withLock { | ||
listeners.forEach { | ||
it.onChange(isOnline) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add a listener to be notified about any network changes. | ||
* This method is thread safe. | ||
* IMPORTANT: Not removing it again will result in leaks. | ||
* @param listener the listener to add. | ||
*/ | ||
fun addListener(listener: ConnectionListener) { | ||
mutex.withLock { | ||
listeners.add(listener) | ||
} | ||
} | ||
|
||
/** | ||
* Removes a network listener. | ||
* This method is thread safe. | ||
* | ||
* @param listener the listener to remove. | ||
* @return `true` if the listener was removed. | ||
*/ | ||
fun removeListener(listener: ConnectionListener): Boolean { | ||
mutex.withLock { | ||
return listeners.remove(listener) | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Why are emulator always connected?
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.
No idea 😅 ... I pulled this code from somewhere else and has never really questioned it 🙈