Merge pull request #7198 from vector-im/feature/adm/configurable-sync-timeout

Allow configurable sync timeout
This commit is contained in:
Adam Brown 2022-09-22 15:40:58 +01:00 committed by GitHub
commit 65156a8ece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 5 deletions

1
changelog.d/7198.sdk Normal file
View File

@ -0,0 +1 @@
Allow the sync timeout to be configured (mainly useful for testing)

View File

@ -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
@ -103,7 +104,8 @@ class CommonTestHelper internal constructor(context: Context) {
context,
MatrixConfiguration(
applicationFlavor = "TestFlavor",
roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider()
roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider(),
syncConfig = SyncConfig(longPollTimeout = 5_000L),
)
)
}

View File

@ -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(),
)

View File

@ -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,
)

View File

@ -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
@ -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)
@ -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
@ -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()

View File

@ -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)
}