summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-09-29 20:54:33 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-09-29 21:45:27 +0000
commita8248038c1a32e725cde9c960f442b11b7c9bd56 (patch)
tree9e66a91112fcaeca35e34536c0cc5ff3d18c808e /android/src
parent4a8e56fe204fb3d2fff6bd9770ae73b6c45d5203 (diff)
downloadmullvadvpn-a8248038c1a32e725cde9c960f442b11b7c9bd56.tar.xz
mullvadvpn-a8248038c1a32e725cde9c960f442b11b7c9bd56.zip
Use `ExponentialBackoff` helper in `AccountCache`
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/AccountCache.kt35
1 files changed, 16 insertions, 19 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/AccountCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/AccountCache.kt
index 55342ea166..cc13ec125a 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/AccountCache.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/AccountCache.kt
@@ -1,23 +1,23 @@
package net.mullvad.mullvadvpn.service
-import kotlin.math.min
import kotlinx.coroutines.delay
import net.mullvad.mullvadvpn.model.GetAccountDataResult
+import net.mullvad.mullvadvpn.util.ExponentialBackoff
import net.mullvad.mullvadvpn.util.JobTracker
import net.mullvad.talpid.util.EventNotifier
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
-// Number of retry attempts to check for a changed expiry before giving up.
-// Current value will force the cache to keep fetching for about four minutes or until a new expiry
-// value is received.
-// This is only used if the expiry was invalidated and fetching a new expiry returns the same value
-// as before the invalidation.
-const val MAX_INVALIDATED_RETRIES = 7
-
class AccountCache(val daemon: MullvadDaemon, val settingsListener: SettingsListener) {
companion object {
public val EXPIRY_FORMAT = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss z")
+
+ // Number of retry attempts to check for a changed expiry before giving up.
+ // Current value will force the cache to keep fetching for about four minutes or until a new
+ // expiry value is received.
+ // This is only used if the expiry was invalidated and fetching a new expiry returns the
+ // same value as before the invalidation.
+ private const val MAX_INVALIDATED_RETRIES = 7
}
val onAccountNumberChange = EventNotifier<String?>(null)
@@ -60,21 +60,25 @@ class AccountCache(val daemon: MullvadDaemon, val settingsListener: SettingsList
synchronized(this) {
accountNumber?.let { account ->
jobTracker.newBackgroundJob("fetch") {
- var retryAttempt = 0
+ val delays = ExponentialBackoff().apply {
+ cap = 2 /* h */ * 60 /* min */ * 60 /* s */ * 1000 /* ms */
+ }
do {
val result = daemon.getAccountData(account)
if (result is GetAccountDataResult.Ok) {
- if (handleNewExpiry(account, result.accountData.expiry, retryAttempt)) {
+ val expiry = result.accountData.expiry
+ val retryAttempt = delays.iteration
+
+ if (handleNewExpiry(account, expiry, retryAttempt)) {
break
}
} else if (result is GetAccountDataResult.InvalidAccount) {
break
}
- retryAttempt += 1
- delay(calculateRetryFetchDelay(retryAttempt))
+ delay(delays.next())
} while (onAccountExpiryChange.hasListeners())
}
}
@@ -153,11 +157,4 @@ class AccountCache(val daemon: MullvadDaemon, val settingsListener: SettingsList
return false
}
}
-
- private fun calculateRetryFetchDelay(retryAttempt: Int): Long {
- // delay in seconds = 2 ^ retryAttempt capped at 2^13 (8192)
- val exponent = min(retryAttempt, 13)
-
- return (1L shl exponent) * 1000L
- }
}