Use retry through executeRequest instead of Task.executeRetry

This commit is contained in:
ganfra 2022-06-09 10:55:59 +02:00
parent 52ed7c019b
commit e5ce77de34
14 changed files with 65 additions and 54 deletions

View File

@ -241,7 +241,7 @@ internal class SecretShareManager @Inject constructor(
) )
try { try {
withContext(coroutineDispatchers.io) { withContext(coroutineDispatchers.io) {
sendToDeviceTask.executeRetry(params, 3) sendToDeviceTask.execute(params)
} }
Timber.tag(loggerTag.value) Timber.tag(loggerTag.value)
.d("Secret request sent for $secretName to ${cryptoDeviceInfo.shortDebugString()}") .d("Secret request sent for $secretName to ${cryptoDeviceInfo.shortDebugString()}")

View File

@ -33,7 +33,7 @@ internal class DefaultGetKeysBackupLastVersionTask @Inject constructor(
override suspend fun execute(params: Unit): KeysBackupLastVersionResult { override suspend fun execute(params: Unit): KeysBackupLastVersionResult {
return try { return try {
val keysVersionResult = executeRequest(globalErrorReceiver) { val keysVersionResult = executeRequest(globalErrorReceiver, canRetry = true) {
roomKeysApi.getKeysBackupLastVersion() roomKeysApi.getKeysBackupLastVersion()
} }
KeysBackupLastVersionResult.KeysBackup(keysVersionResult) KeysBackupLastVersionResult.KeysBackup(keysVersionResult)

View File

@ -31,7 +31,7 @@ internal class DefaultGetKeysBackupVersionTask @Inject constructor(
) : GetKeysBackupVersionTask { ) : GetKeysBackupVersionTask {
override suspend fun execute(params: String): KeysVersionResult { override suspend fun execute(params: String): KeysVersionResult {
return executeRequest(globalErrorReceiver) { return executeRequest(globalErrorReceiver, canRetry = true) {
roomKeysApi.getKeysBackupVersion(params) roomKeysApi.getKeysBackupVersion(params)
} }
} }

View File

@ -37,7 +37,7 @@ internal class DefaultStoreSessionsDataTask @Inject constructor(
) : StoreSessionsDataTask { ) : StoreSessionsDataTask {
override suspend fun execute(params: StoreSessionsDataTask.Params): BackupKeysResult { override suspend fun execute(params: StoreSessionsDataTask.Params): BackupKeysResult {
return executeRequest(globalErrorReceiver) { return executeRequest(globalErrorReceiver, canRetry = true) {
roomKeysApi.storeSessionsData( roomKeysApi.storeSessionsData(
params.version, params.version,
params.keysBackupData params.keysBackupData

View File

@ -36,7 +36,7 @@ internal class DefaultUpdateKeysBackupVersionTask @Inject constructor(
) : UpdateKeysBackupVersionTask { ) : UpdateKeysBackupVersionTask {
override suspend fun execute(params: UpdateKeysBackupVersionTask.Params) { override suspend fun execute(params: UpdateKeysBackupVersionTask.Params) {
return executeRequest(globalErrorReceiver) { return executeRequest(globalErrorReceiver, canRetry = true) {
roomKeysApi.updateKeysBackupVersion(params.version, params.keysBackupVersionBody) roomKeysApi.updateKeysBackupVersion(params.version, params.keysBackupVersionBody)
} }
} }

View File

@ -59,6 +59,7 @@ import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
import org.matrix.android.sdk.internal.crypto.tasks.UploadSignaturesTask import org.matrix.android.sdk.internal.crypto.tasks.UploadSignaturesTask
import org.matrix.android.sdk.internal.crypto.tasks.UploadSigningKeysTask import org.matrix.android.sdk.internal.crypto.tasks.UploadSigningKeysTask
import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.internal.network.DEFAULT_REQUEST_RETRY_COUNT
import org.matrix.android.sdk.internal.network.parsing.CheckNumberType import org.matrix.android.sdk.internal.network.parsing.CheckNumberType
import org.matrix.android.sdk.internal.session.room.send.SendResponse import org.matrix.android.sdk.internal.session.room.send.SendResponse
import timber.log.Timber import timber.log.Timber
@ -87,13 +88,10 @@ internal class RequestSender @Inject constructor(
private val getRoomSessionDataTask: GetRoomSessionDataTask, private val getRoomSessionDataTask: GetRoomSessionDataTask,
private val moshi: Moshi, private val moshi: Moshi,
) { ) {
companion object {
const val REQUEST_RETRY_COUNT = 3
}
suspend fun claimKeys(request: Request.KeysClaim): String { suspend fun claimKeys(request: Request.KeysClaim): String {
val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys) val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys)
val response = oneTimeKeysForUsersDeviceTask.executeRetry(claimParams, REQUEST_RETRY_COUNT) val response = oneTimeKeysForUsersDeviceTask.execute(claimParams)
val adapter = MoshiProvider val adapter = MoshiProvider
.providesMoshi() .providesMoshi()
.adapter(KeysClaimResponse::class.java) .adapter(KeysClaimResponse::class.java)
@ -102,7 +100,7 @@ internal class RequestSender @Inject constructor(
suspend fun queryKeys(request: Request.KeysQuery): String { suspend fun queryKeys(request: Request.KeysQuery): String {
val params = DownloadKeysForUsersTask.Params(request.users, null) val params = DownloadKeysForUsersTask.Params(request.users, null)
val response = downloadKeysForUsersTask.executeRetry(params, REQUEST_RETRY_COUNT) val response = downloadKeysForUsersTask.execute(params)
val adapter = moshi.adapter(KeysQueryResponse::class.java) val adapter = moshi.adapter(KeysQueryResponse::class.java)
return adapter.toJson(response)!! return adapter.toJson(response)!!
} }
@ -111,35 +109,35 @@ internal class RequestSender @Inject constructor(
val body = moshi.adapter<JsonDict>(Map::class.java).fromJson(request.body)!! val body = moshi.adapter<JsonDict>(Map::class.java).fromJson(request.body)!!
val params = UploadKeysTask.Params(body) val params = UploadKeysTask.Params(body)
val response = uploadKeysTask.executeRetry(params, REQUEST_RETRY_COUNT) val response = uploadKeysTask.execute(params)
val adapter = moshi.adapter(KeysUploadResponse::class.java) val adapter = moshi.adapter(KeysUploadResponse::class.java)
return adapter.toJson(response)!! return adapter.toJson(response)!!
} }
suspend fun sendVerificationRequest(request: OutgoingVerificationRequest) { suspend fun sendVerificationRequest(request: OutgoingVerificationRequest, retryCount: Int = DEFAULT_REQUEST_RETRY_COUNT) {
when (request) { when (request) {
is OutgoingVerificationRequest.InRoom -> sendRoomMessage(request) is OutgoingVerificationRequest.InRoom -> sendRoomMessage(request, retryCount)
is OutgoingVerificationRequest.ToDevice -> sendToDevice(request) is OutgoingVerificationRequest.ToDevice -> sendToDevice(request, retryCount)
} }
} }
private suspend fun sendRoomMessage(request: OutgoingVerificationRequest.InRoom): SendResponse { private suspend fun sendRoomMessage(request: OutgoingVerificationRequest.InRoom, retryCount: Int): SendResponse {
return sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId) return sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId, retryCount)
} }
suspend fun sendRoomMessage(request: Request.RoomMessage): String { suspend fun sendRoomMessage(request: Request.RoomMessage, retryCount: Int = DEFAULT_REQUEST_RETRY_COUNT): String {
val sendResponse = sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId) val sendResponse = sendRoomMessage(request.eventType, request.roomId, request.content, request.requestId, retryCount)
val responseAdapter = moshi.adapter(SendResponse::class.java) val responseAdapter = moshi.adapter(SendResponse::class.java)
return responseAdapter.toJson(sendResponse) return responseAdapter.toJson(sendResponse)
} }
suspend fun sendRoomMessage(eventType: String, roomId: String, content: String, transactionId: String): SendResponse { suspend fun sendRoomMessage(eventType: String, roomId: String, content: String, transactionId: String, retryCount: Int = DEFAULT_REQUEST_RETRY_COUNT): SendResponse {
val paramsAdapter = moshi.adapter<Content>(Map::class.java) val paramsAdapter = moshi.adapter<Content>(Map::class.java)
val jsonContent = paramsAdapter.fromJson(content) val jsonContent = paramsAdapter.fromJson(content)
val event = Event(eventType, transactionId, jsonContent, roomId = roomId) val event = Event(eventType, transactionId, jsonContent, roomId = roomId)
val params = SendVerificationMessageTask.Params(event) val params = SendVerificationMessageTask.Params(event, retryCount)
return sendVerificationMessageTask.get().executeRetry(params, REQUEST_RETRY_COUNT) return sendVerificationMessageTask.get().execute(params)
} }
suspend fun sendSignatureUpload(request: Request.SignatureUpload): String { suspend fun sendSignatureUpload(request: Request.SignatureUpload): String {
@ -154,7 +152,7 @@ internal class RequestSender @Inject constructor(
val paramsAdapter = moshi.adapter<Map<String, Map<String, Any>>>(Map::class.java) val paramsAdapter = moshi.adapter<Map<String, Map<String, Any>>>(Map::class.java)
val signatures = paramsAdapter.fromJson(body)!! val signatures = paramsAdapter.fromJson(body)!!
val params = UploadSignaturesTask.Params(signatures) val params = UploadSignaturesTask.Params(signatures)
val response = signaturesUploadTask.executeRetry(params, REQUEST_RETRY_COUNT) val response = signaturesUploadTask.execute(params)
val responseAdapter = moshi.adapter(SignatureUploadResponse::class.java) val responseAdapter = moshi.adapter(SignatureUploadResponse::class.java)
return responseAdapter.toJson(response)!! return responseAdapter.toJson(response)!!
} }
@ -183,9 +181,8 @@ internal class RequestSender @Inject constructor(
failure = failure, failure = failure,
interceptor = interactiveAuthInterceptor, interceptor = interactiveAuthInterceptor,
retryBlock = { authUpdate -> retryBlock = { authUpdate ->
uploadSigningKeysTask.executeRetry( uploadSigningKeysTask.execute(
uploadSigningKeysParams.copy(userAuthParam = authUpdate), uploadSigningKeysParams.copy(userAuthParam = authUpdate)
REQUEST_RETRY_COUNT
) )
} }
) != UiaResult.SUCCESS ) != UiaResult.SUCCESS
@ -196,15 +193,15 @@ internal class RequestSender @Inject constructor(
} }
} }
suspend fun sendToDevice(request: Request.ToDevice) { suspend fun sendToDevice(request: Request.ToDevice, retryCount: Int = DEFAULT_REQUEST_RETRY_COUNT) {
sendToDevice(request.eventType, request.body, request.requestId) sendToDevice(request.eventType, request.body, request.requestId, retryCount)
} }
suspend fun sendToDevice(request: OutgoingVerificationRequest.ToDevice) { suspend fun sendToDevice(request: OutgoingVerificationRequest.ToDevice, retryCount: Int = DEFAULT_REQUEST_RETRY_COUNT) {
sendToDevice(request.eventType, request.body, request.requestId) sendToDevice(request.eventType, request.body, request.requestId, retryCount)
} }
suspend fun sendToDevice(eventType: String, body: String, transactionId: String) { private suspend fun sendToDevice(eventType: String, body: String, transactionId: String, retryCount: Int) {
val adapter = moshi val adapter = moshi
.newBuilder() .newBuilder()
.add(CheckNumberType.JSON_ADAPTER_FACTORY) .add(CheckNumberType.JSON_ADAPTER_FACTORY)
@ -215,16 +212,16 @@ internal class RequestSender @Inject constructor(
val userMap = MXUsersDevicesMap<Any>() val userMap = MXUsersDevicesMap<Any>()
userMap.join(jsonBody) userMap.join(jsonBody)
val sendToDeviceParams = SendToDeviceTask.Params(eventType, userMap, transactionId) val sendToDeviceParams = SendToDeviceTask.Params(eventType, userMap, transactionId, retryCount)
sendToDeviceTask.executeRetry(sendToDeviceParams, REQUEST_RETRY_COUNT) sendToDeviceTask.execute(sendToDeviceParams)
} }
suspend fun getKeyBackupVersion(version: String): KeysVersionResult? = getKeyBackupVersion { suspend fun getKeyBackupVersion(version: String): KeysVersionResult? = getKeyBackupVersion {
getKeysBackupVersionTask.executeRetry(version, 3) getKeysBackupVersionTask.execute(version)
} }
suspend fun getKeyBackupLastVersion(): KeysBackupLastVersionResult? = getKeyBackupVersion { suspend fun getKeyBackupLastVersion(): KeysBackupLastVersionResult? = getKeyBackupVersion {
getKeysBackupLastVersionTask.executeRetry(Unit, 3) getKeysBackupLastVersionTask.execute(Unit)
} }
private inline fun <reified T> getKeyBackupVersion(block: () -> T?): T? { private inline fun <reified T> getKeyBackupVersion(block: () -> T?): T? {
@ -261,27 +258,32 @@ internal class RequestSender @Inject constructor(
Map::class.java, Map::class.java,
String::class.java, String::class.java,
RoomKeysBackupData::class.java RoomKeysBackupData::class.java
)) )
)
val keys = adapter.fromJson(request.rooms)!! val keys = adapter.fromJson(request.rooms)!!
val params = StoreSessionsDataTask.Params(request.version, KeysBackupData(keys)) val params = StoreSessionsDataTask.Params(request.version, KeysBackupData(keys))
val response = backupRoomKeysTask.executeRetry(params, REQUEST_RETRY_COUNT) val response = backupRoomKeysTask.execute(params)
val responseAdapter = moshi.adapter(BackupKeysResult::class.java) val responseAdapter = moshi.adapter(BackupKeysResult::class.java)
return responseAdapter.toJson(response)!! return responseAdapter.toJson(response)!!
} }
suspend fun updateBackup(keysBackupVersion: KeysVersionResult, body: UpdateKeysBackupVersionBody) { suspend fun updateBackup(keysBackupVersion: KeysVersionResult, body: UpdateKeysBackupVersionBody) {
val params = UpdateKeysBackupVersionTask.Params(keysBackupVersion.version, body) val params = UpdateKeysBackupVersionTask.Params(keysBackupVersion.version, body)
updateKeysBackupVersionTask.executeRetry(params, REQUEST_RETRY_COUNT) updateKeysBackupVersionTask.execute(params)
} }
suspend fun downloadBackedUpKeys(version: String, roomId: String, sessionId: String): KeysBackupData { suspend fun downloadBackedUpKeys(version: String, roomId: String, sessionId: String): KeysBackupData {
val data = getRoomSessionDataTask.execute(GetRoomSessionDataTask.Params(roomId, sessionId, version)) val data = getRoomSessionDataTask.execute(GetRoomSessionDataTask.Params(roomId, sessionId, version))
return KeysBackupData(mutableMapOf( return KeysBackupData(
roomId to RoomKeysBackupData(mutableMapOf( mutableMapOf(
roomId to RoomKeysBackupData(
mutableMapOf(
sessionId to data sessionId to data
)) )
)) )
)
)
} }
suspend fun downloadBackedUpKeys(version: String, roomId: String): KeysBackupData { suspend fun downloadBackedUpKeys(version: String, roomId: String): KeysBackupData {

View File

@ -39,7 +39,7 @@ internal class DefaultClaimOneTimeKeysForUsersDevice @Inject constructor(
override suspend fun execute(params: ClaimOneTimeKeysForUsersDeviceTask.Params): KeysClaimResponse { override suspend fun execute(params: ClaimOneTimeKeysForUsersDeviceTask.Params): KeysClaimResponse {
val body = KeysClaimBody(oneTimeKeys = params.usersDevicesKeyTypesMap) val body = KeysClaimBody(oneTimeKeys = params.usersDevicesKeyTypesMap)
return executeRequest(globalErrorReceiver) { return executeRequest(globalErrorReceiver, canRetry = true) {
cryptoApi.claimOneTimeKeysForUsersDevices(body) cryptoApi.claimOneTimeKeysForUsersDevices(body)
} }
} }

View File

@ -97,7 +97,7 @@ internal class DefaultDownloadKeysForUsers @Inject constructor(
) )
} else { } else {
// No need to chunk, direct request // No need to chunk, direct request
executeRequest(globalErrorReceiver) { executeRequest(globalErrorReceiver, canRetry = true) {
cryptoApi.downloadKeysForUsers( cryptoApi.downloadKeysForUsers(
KeysQueryBody( KeysQueryBody(
deviceKeys = params.userIds.associateWith { emptyList() }, deviceKeys = params.userIds.associateWith { emptyList() },

View File

@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.crypto.tasks
import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap import org.matrix.android.sdk.api.session.crypto.model.MXUsersDevicesMap
import org.matrix.android.sdk.internal.crypto.api.CryptoApi import org.matrix.android.sdk.internal.crypto.api.CryptoApi
import org.matrix.android.sdk.internal.crypto.model.rest.SendToDeviceBody import org.matrix.android.sdk.internal.crypto.model.rest.SendToDeviceBody
import org.matrix.android.sdk.internal.network.DEFAULT_REQUEST_RETRY_COUNT
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.task.Task
@ -32,7 +33,9 @@ internal interface SendToDeviceTask : Task<SendToDeviceTask.Params, Unit> {
// the content to send. Map from user_id to device_id to content dictionary. // the content to send. Map from user_id to device_id to content dictionary.
val contentMap: MXUsersDevicesMap<Any>, val contentMap: MXUsersDevicesMap<Any>,
// the transactionId. If not provided, a transactionId will be created by the task // the transactionId. If not provided, a transactionId will be created by the task
val transactionId: String? = null val transactionId: String? = null,
// Number of retry before failing
val retryCount:Int = DEFAULT_REQUEST_RETRY_COUNT
) )
} }
@ -54,7 +57,7 @@ internal class DefaultSendToDeviceTask @Inject constructor(
return executeRequest( return executeRequest(
globalErrorReceiver, globalErrorReceiver,
canRetry = true, canRetry = true,
maxRetriesCount = 3 maxRetriesCount = params.retryCount
) { ) {
cryptoApi.sendToDevice( cryptoApi.sendToDevice(
eventType = params.eventType, eventType = params.eventType,

View File

@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.crypto.tasks
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.room.send.SendState
import org.matrix.android.sdk.internal.crypto.CryptoSessionInfoProvider import org.matrix.android.sdk.internal.crypto.CryptoSessionInfoProvider
import org.matrix.android.sdk.internal.network.DEFAULT_REQUEST_RETRY_COUNT
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.session.room.RoomAPI import org.matrix.android.sdk.internal.session.room.RoomAPI
@ -29,7 +30,10 @@ import javax.inject.Inject
internal interface SendVerificationMessageTask : Task<SendVerificationMessageTask.Params, SendResponse> { internal interface SendVerificationMessageTask : Task<SendVerificationMessageTask.Params, SendResponse> {
data class Params( data class Params(
val event: Event // The event to sent
val event: Event,
// Number of retry before failing
val retryCount: Int = DEFAULT_REQUEST_RETRY_COUNT
) )
} }
@ -45,7 +49,7 @@ internal class DefaultSendVerificationMessageTask @Inject constructor(
val localId = event.eventId!! val localId = event.eventId!!
try { try {
localEchoRepository.updateSendState(localId, event.roomId, SendState.SENDING) localEchoRepository.updateSendState(localId, event.roomId, SendState.SENDING)
val response = executeRequest(globalErrorReceiver) { val response = executeRequest(globalErrorReceiver, canRetry = true, maxRetriesCount = params.retryCount) {
roomAPI.send( roomAPI.send(
txId = localId, txId = localId,
roomId = event.roomId ?: "", roomId = event.roomId ?: "",

View File

@ -38,7 +38,7 @@ internal class DefaultUploadKeysTask @Inject constructor(
override suspend fun execute(params: UploadKeysTask.Params): KeysUploadResponse { override suspend fun execute(params: UploadKeysTask.Params): KeysUploadResponse {
Timber.i("## Uploading device keys -> $params.body") Timber.i("## Uploading device keys -> $params.body")
return executeRequest(globalErrorReceiver) { return executeRequest(globalErrorReceiver, canRetry = true) {
cryptoApi.uploadKeys(params.body) cryptoApi.uploadKeys(params.body)
} }
} }

View File

@ -60,7 +60,7 @@ internal class DefaultUploadSigningKeysTask @Inject constructor(
} }
private suspend fun doRequest(uploadQuery: UploadSigningKeysBody) { private suspend fun doRequest(uploadQuery: UploadSigningKeysBody) {
val keysQueryResponse = executeRequest(globalErrorReceiver) { val keysQueryResponse = executeRequest(globalErrorReceiver, canRetry = true) {
cryptoApi.uploadSigningKeys(uploadQuery) cryptoApi.uploadSigningKeys(uploadQuery)
} }
if (keysQueryResponse.failures?.isNotEmpty() == true) { if (keysQueryResponse.failures?.isNotEmpty() == true) {

View File

@ -39,10 +39,13 @@ import java.io.IOException
* @param maxRetriesCount the max number of retries * @param maxRetriesCount the max number of retries
* @param requestBlock a suspend lambda to perform the network request * @param requestBlock a suspend lambda to perform the network request
*/ */
internal const val DEFAULT_REQUEST_RETRY_COUNT = 4
internal suspend inline fun <DATA> executeRequest(globalErrorReceiver: GlobalErrorReceiver?, internal suspend inline fun <DATA> executeRequest(globalErrorReceiver: GlobalErrorReceiver?,
canRetry: Boolean = false, canRetry: Boolean = false,
maxDelayBeforeRetry: Long = 32_000L, maxDelayBeforeRetry: Long = 32_000L,
maxRetriesCount: Int = 4, maxRetriesCount: Int = DEFAULT_REQUEST_RETRY_COUNT,
noinline requestBlock: suspend () -> DATA): DATA { noinline requestBlock: suspend () -> DATA): DATA {
var currentRetryCount = 0 var currentRetryCount = 0
var currentDelay = 1_000L var currentDelay = 1_000L

View File

@ -37,13 +37,12 @@ internal class DefaultToDeviceService @Inject constructor(
} }
override suspend fun sendToDevice(eventType: String, contentMap: MXUsersDevicesMap<Any>, txnId: String?) { override suspend fun sendToDevice(eventType: String, contentMap: MXUsersDevicesMap<Any>, txnId: String?) {
sendToDeviceTask.executeRetry( sendToDeviceTask.execute(
SendToDeviceTask.Params( SendToDeviceTask.Params(
eventType = eventType, eventType = eventType,
contentMap = contentMap, contentMap = contentMap,
transactionId = txnId transactionId = txnId
), )
3
) )
} }