Skip to content

Commit

Permalink
Merge pull request #1747 from Infomaniak/threads-algo-opti
Browse files Browse the repository at this point in the history
Small optimisation of duration in RefreshThreads algo
  • Loading branch information
KevinBoulongne authored Mar 14, 2024
2 parents 6e6971f + 30b0387 commit 784e6cc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.data.cache.mailboxContent

import com.infomaniak.lib.core.models.ApiResponse
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.models.getMessages.GetMessagesByUidsResult
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import okhttp3.OkHttpClient
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.abs

@Singleton
class DelayApiCallManager @Inject constructor() {

private var previousTime = 0L

/**
* Realm really doesn't like to be written on too frequently.
* So we want to be sure that we don't write twice in less than 500 ms.
* Appreciable side effect: it will also reduce the stress on the API.
*/
suspend fun getMessagesByUids(
scope: CoroutineScope,
mailboxUuid: String,
folderId: String,
uids: List<Int>,
okHttpClient: OkHttpClient?,
): ApiResponse<GetMessagesByUidsResult> {

val duration = abs(System.currentTimeMillis() - previousTime)
val delay = MAX_DELAY_BETWEEN_API_CALLS - duration

if (delay > 0L) {
delay(delay)
scope.ensureActive()
}

previousTime = System.currentTimeMillis()

return ApiRepository.getMessagesByUids(mailboxUuid, folderId, uids, okHttpClient)
}

companion object {
private const val MAX_DELAY_BETWEEN_API_CALLS = 500L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import kotlin.math.max
class RefreshController @Inject constructor(
private val localSettings: LocalSettings,
private val mailboxController: MailboxController,
private val delayApiCallManager: DelayApiCallManager,
) {

private var refreshThreadsJob: Job? = null
Expand Down Expand Up @@ -525,9 +526,7 @@ class RefreshController @Inject constructor(

val impactedThreads = mutableSetOf<Thread>()

val before = System.currentTimeMillis()
val apiResponse = ApiRepository.getMessagesByUids(mailbox.uuid, folder.id, uids, okHttpClient)
val after = System.currentTimeMillis()
val apiResponse = delayApiCallManager.getMessagesByUids(scope, mailbox.uuid, folder.id, uids, okHttpClient)
if (!apiResponse.isSuccess()) apiResponse.throwErrorAsException()
scope.ensureActive()

Expand All @@ -552,18 +551,6 @@ class RefreshController @Inject constructor(
impactedThreads += allImpactedThreads.filter { it.folderId == folder.id }
}
}

/**
* Realm really doesn't like to be written on too frequently.
* So we want to be sure that we don't write twice in less than 500 ms.
* Appreciable side effect: it will also reduce the stress on the API.
*/
val delay = Utils.MAX_DELAY_BETWEEN_API_CALLS - (after - before)
if (delay > 0L) {
delay(delay)
scope.ensureActive()
}

}

return impactedThreads
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/infomaniak/mail/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ object Utils {
const val NUMBER_OF_OLD_MESSAGES_TO_FETCH = 500
/** Beware: the API refuses a PAGE_SIZE bigger than 200. */
const val PAGE_SIZE: Int = 50
const val MAX_DELAY_BETWEEN_API_CALLS = 500L
const val DELAY_BEFORE_FETCHING_ACTIVITIES_AGAIN = 500L

const val TAG_SEPARATOR = " "
Expand Down

0 comments on commit 784e6cc

Please sign in to comment.