-
Notifications
You must be signed in to change notification settings - Fork 176
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
Create SyncOrchestrator
#4176
base: develop
Are you sure you want to change the base?
Create SyncOrchestrator
#4176
Conversation
📱 Scan the QR code below to install the build (arm64 only) for this PR. |
...sh/impl/src/main/kotlin/io/element/android/libraries/push/impl/push/SyncOnNotifiableEvent.kt
Outdated
Show resolved
Hide resolved
396fe48
to
801dd62
Compare
appnav/src/main/kotlin/io/element/android/appnav/SyncObserver.kt
Outdated
Show resolved
Hide resolved
@@ -147,6 +138,9 @@ class LoggedInFlowNode @AssistedInject constructor( | |||
|
|||
override fun onBuilt() { | |||
super.onBuilt() | |||
|
|||
syncOrchestrator.start() |
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.
I think this code will not be run if the app is killed and a Push is received. In this case the sync will not start?
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.
I think you're right, and I completely overlooked this... This complicates things, as we would have to use SyncOrchestrator
in the AppScope
and that means holding as many as MatrixClient
s there are in MatrixClientsHolder
, maybe following a similar approach 🫤 .
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.
Thanks! Please see my comments.
} | ||
} | ||
// Make sure we mark the call as ended in the app state | ||
if (appForegroundStateService.isInCall.value) { |
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.
Nit: I do not think it is useful to read the value before changing it, or do I miss something here?
@@ -199,7 +199,7 @@ class LoginFlowNode @AssistedInject constructor( | |||
|
|||
@Composable | |||
override fun View(modifier: Modifier) { | |||
activity = LocalContext.current as? Activity | |||
activity = LocalActivity.current |
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.
Maybe stick to the same line of code than for other classes:
val activity = requireNotNull(LocalActivity.current)
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.
Some remarks
/** | ||
* Observes the app state and network state to start/stop the sync service. | ||
*/ | ||
interface SyncOrchestrator { |
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 sure this class belongs the the matrix api, it's more an app thing to me
/** | ||
* Provides a [SyncOrchestrator] for a given [SessionId]. | ||
*/ | ||
fun interface SyncOrchestratorProvider { |
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.
Same ^
@@ -29,13 +30,14 @@ private const val SAVE_INSTANCE_KEY = "io.element.android.x.di.MatrixClientsHold | |||
@ContributesBinding(AppScope::class) | |||
class MatrixClientsHolder @Inject constructor( |
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.
Maybe we should rename this class so it's a bit clearer it's not just about MatrixClients
?
Also we might want to create a class instead of using Pair?
I don't think we need the DefaultSyncOrchestratorProvider
, we should let MatrixClientsHolder
(with a new name :P) implements the SyncOrchestratorProvider
@ganfra all the points should have been fixed in the latest commit. |
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.
LGTM, thanks!
…ugh the whole app. The decision is based on several inputs: sync state, network available, app in foreground, app in call, app needing to sync an event for a notification.
…fail instead This prevents an issue when using the offline mode of the SDK, which made the wrong UI states to be shown when the `SyncState` is `Idle` (that is, after the service being manually stopped).
…they're not easily mistaken with internet connectivity instead
3033b67
to
64abdf6
Compare
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.
A new bunch of comments, I have not run the code yet
@ContributesBinding(AppScope::class) | ||
class MatrixClientsHolder @Inject constructor( | ||
@ContributesBinding(AppScope::class, boundType = MatrixClientProvider::class) | ||
@ContributesBinding(AppScope::class, boundType = SyncOrchestratorProvider::class) |
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.
TIL we can have multiple @ContributesBinding
annotations
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.
Well, we don't have them anymore after the latest changes 😅 .
private val sessionIdsToMatrixClient = ConcurrentHashMap<SessionId, MatrixClient>() | ||
private val syncOrchestratorFactory: DefaultSyncOrchestrator.Factory, | ||
) : MatrixClientProvider, SyncOrchestratorProvider { | ||
private val sessionIdsToMatrixClient = ConcurrentHashMap<SessionId, InMemoryMatrixSession>() |
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.
Maybe rename this val now that it will contain InMemoryMatrixSession
?
/** | ||
* Stop observing the app state and network state. | ||
*/ | ||
override fun stop() { |
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.
Since this method is only used for test, you should remove it.
The background coroutine scope can be used from the TestScope, and can be provided in the constructor parameter.
|
||
// Start the sync if it wasn't already started | ||
syncOrchestratorProvider.getSyncOrchestrator(notifiableEvent.sessionId)?.start() ?: return@withContext | ||
|
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.
I am wondering if matrixClientProvider.getOrRestore
could also take care of starting the syncOrchestrator. Maybe the sync orchestrator should always be started? In this case the interface SyncOrchestrator
could just be private?
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.
Good call, this simplified the code quite a bit!
baseCoroutineScope.launch(dispatchers.io) { | ||
try { | ||
initialSyncMutex.lock() | ||
syncService.startSync() |
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 do we need a first startSync
here? The combine below is not enough to decide what to do depending on the status of all the observed flows?
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.
I was mimicking what we had before in LoggedInFlowNode
for the airgapped case. After the changes in the SDK it's probably not needed anymore.
initialSyncMutex.lock() | ||
|
||
combine( | ||
// small debounce to avoid spamming startSync when the state is changing quickly in case of error. |
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.
Maybe the debounced can be moved after the combine block (before .distinctUntilChanged())?
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.
This causes issues in the tests, since it's preventing some intermediate states from being generated. I'd rather keep it were it is, if possible.
...rc/main/kotlin/io/element/android/services/appnavstate/test/FakeAppForegroundStateService.kt
Show resolved
Hide resolved
… the existing loop when offline mode is enabled for the Client in the SDK
… to it, remove the existing provider interface.
Quality Gate passedIssues Measures |
Content
Centralise the start/stop sync logic in a single component of the app:
SyncOrchestrator
, which will act as the single point where sync start/stop can happen, based on several inputs (sync state, network available, app in foreground, app in call, app needing to sync an event for a notification).AppForegroundStateService
.SyncOrchestrator
.Motivation and context
Having too many points where the sync could be started and stopped could cause issues where the sync was unexpectedly stopped when it should instead be running or a deadlock may happen for several start/stop calls.
It should help with #4150
Tests
Just play with the sync state with network online/offline and app foreground state, receiving notifications, joining calls, etc.
Tested devices
Checklist