Skip to content

Commit

Permalink
Merge pull request #7198 from vector-im/feature/adm/configurable-sync…
Browse files Browse the repository at this point in the history
…-timeout

Allow configurable sync timeout
  • Loading branch information
ouchadam authored Sep 22, 2022
2 parents 151f624 + 600588d commit 65156a8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.d/7198.sdk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow the sync timeout to be configured (mainly useful for testing)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.SyncConfig
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.registration.RegistrationResult
import org.matrix.android.sdk.api.session.Session
Expand Down Expand Up @@ -103,7 +104,8 @@ class CommonTestHelper internal constructor(context: Context) {
context,
MatrixConfiguration(
applicationFlavor = "TestFlavor",
roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider()
roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider(),
syncConfig = SyncConfig(longPollTimeout = 5_000L),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ data class MatrixConfiguration(
* List of network interceptors, they will be added when building an OkHttp client.
*/
val networkInterceptors: List<Interceptor> = emptyList(),
/**
* Sync configuration.
*/
val syncConfig: SyncConfig = SyncConfig(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2022 The Matrix.org Foundation C.I.C.
*
* 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 org.matrix.android.sdk.api

data class SyncConfig(
/**
* Time to keep sync connection alive for before making another request in milliseconds.
*/
val longPollTimeout: Long = 30_000L,
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.isTokenError
Expand All @@ -52,7 +53,6 @@ import javax.inject.Inject
import kotlin.concurrent.schedule

private const val RETRY_WAIT_TIME_MS = 10_000L
private const val DEFAULT_LONG_POOL_TIMEOUT = 30_000L

private val loggerTag = LoggerTag("SyncThread", LoggerTag.SYNC)

Expand All @@ -61,7 +61,8 @@ internal class SyncThread @Inject constructor(
private val networkConnectivityChecker: NetworkConnectivityChecker,
private val backgroundDetectionObserver: BackgroundDetectionObserver,
private val activeCallHandler: ActiveCallHandler,
private val lightweightSettingsStorage: DefaultLightweightSettingsStorage
private val lightweightSettingsStorage: DefaultLightweightSettingsStorage,
private val matrixConfiguration: MatrixConfiguration,
) : Thread("Matrix-SyncThread"), NetworkConnectivityChecker.Listener, BackgroundDetectionObserver.Listener {

private var state: SyncState = SyncState.Idle
Expand Down Expand Up @@ -181,7 +182,7 @@ internal class SyncThread @Inject constructor(
val timeout = when {
previousSyncResponseHasToDevice -> 0L /* Force timeout to 0 */
afterPause -> 0L /* No timeout after a pause */
else -> DEFAULT_LONG_POOL_TIMEOUT
else -> matrixConfiguration.syncConfig.longPollTimeout
}
Timber.tag(loggerTag.value).d("Execute sync request with timeout $timeout")
val presence = lightweightSettingsStorage.getSyncPresenceStatus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import androidx.test.platform.app.InstrumentationRegistry
import im.vector.app.features.room.VectorRoomDisplayNameFallbackProvider
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.SyncConfig

fun getMatrixInstance(): Matrix {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val configuration = MatrixConfiguration(
roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context)
roomDisplayNameFallbackProvider = VectorRoomDisplayNameFallbackProvider(context),
syncConfig = SyncConfig(longPollTimeout = 5_000L),
)
return Matrix(context, configuration)
}

0 comments on commit 65156a8

Please sign in to comment.