fix(network): Don't persist last DoH refresh.

lastAlternativesRefresh shouldn't be persisted as it's a value coming
from monotonic clock (which is not consistent between app launches or
reboots).
This commit is contained in:
Mateusz Markowicz
2022-05-19 13:23:08 +02:00
parent 28bdcafcf7
commit 537d36372d
8 changed files with 14 additions and 13 deletions
-2
View File
@@ -45,12 +45,10 @@ public final class me/proton/core/network/data/NetworkPrefsImpl : me/proton/core
public fun <init> (Landroid/content/Context;)V
public fun getActiveAltBaseUrl ()Ljava/lang/String;
public fun getAlternativeBaseUrls ()Ljava/util/List;
public fun getLastAlternativesRefresh ()J
public fun getLastPrimaryApiFail ()J
public fun getPreferences ()Landroid/content/SharedPreferences;
public fun setActiveAltBaseUrl (Ljava/lang/String;)V
public fun setAlternativeBaseUrls (Ljava/util/List;)V
public fun setLastAlternativesRefresh (J)V
public fun setLastPrimaryApiFail (J)V
}
@@ -33,7 +33,6 @@ class NetworkPrefsImpl(context: Context) : NetworkPrefs, PreferencesProvider {
override var activeAltBaseUrl: String? by string()
override var lastPrimaryApiFail: Long by long(default = Long.MIN_VALUE)
override var alternativeBaseUrls: List<String>? by list()
override var lastAlternativesRefresh: Long by long(default = Long.MIN_VALUE)
companion object {
private const val PREFS_NAME = "me.proton.core.network"
@@ -174,6 +174,7 @@ internal class ApiManagerTests {
null,
dohAlternativesListener
)
DohProvider.lastAlternativesRefresh = Long.MIN_VALUE
dohApiHandler = DohApiHandler(apiClient, backend, dohProvider, prefs, ::wallTime, ::time) {
altBackend1
}
@@ -82,7 +82,6 @@ class MockNetworkPrefs : NetworkPrefs {
override var activeAltBaseUrl: String? = null
override var lastPrimaryApiFail: Long = Long.MIN_VALUE
override var alternativeBaseUrls: List<String>? = null
override var lastAlternativesRefresh: Long = Long.MIN_VALUE
}
class MockLogger : Logger {
+2 -2
View File
@@ -193,7 +193,9 @@ public final class me/proton/core/network/domain/DohProvider {
}
public final class me/proton/core/network/domain/DohProvider$Companion {
public final fun getLastAlternativesRefresh ()J
public final fun getMIN_REFRESH_INTERVAL_MS ()J
public final fun setLastAlternativesRefresh (J)V
}
public abstract interface class me/proton/core/network/domain/DohService {
@@ -221,11 +223,9 @@ public abstract class me/proton/core/network/domain/NetworkManager {
public abstract interface class me/proton/core/network/domain/NetworkPrefs {
public abstract fun getActiveAltBaseUrl ()Ljava/lang/String;
public abstract fun getAlternativeBaseUrls ()Ljava/util/List;
public abstract fun getLastAlternativesRefresh ()J
public abstract fun getLastPrimaryApiFail ()J
public abstract fun setActiveAltBaseUrl (Ljava/lang/String;)V
public abstract fun setAlternativeBaseUrls (Ljava/util/List;)V
public abstract fun setLastAlternativesRefresh (J)V
public abstract fun setLastPrimaryApiFail (J)V
}
+5 -1
View File
@@ -17,6 +17,7 @@
*/
import studio.forface.easygradle.dsl.*
import studio.forface.easygradle.dsl.android.`android-annotation`
plugins {
protonKotlinLibrary
@@ -38,7 +39,10 @@ dependencies {
// Kotlin
`coroutines-core`,
`serialization-json`
`serialization-json`,
// Other
`android-annotation`,
)
testImplementation(project(Module.kotlinTest))
@@ -17,6 +17,7 @@
*/
package me.proton.core.network.domain
import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
@@ -49,9 +50,9 @@ class DohProvider(
) {
suspend fun refreshAlternatives() = withContext(networkMainScope.coroutineContext) {
if (monoClockMs() >= prefs.lastAlternativesRefresh + MIN_REFRESH_INTERVAL_MS) {
if (monoClockMs() >= lastAlternativesRefresh + MIN_REFRESH_INTERVAL_MS) {
val allServicesFailed = tryDohServices()
prefs.lastAlternativesRefresh = monoClockMs()
lastAlternativesRefresh = monoClockMs()
if (allServicesFailed && dohAlternativesListener != null) {
dohAlternativesListener.onAlternativesUnblock {
@@ -90,5 +91,8 @@ class DohProvider(
companion object {
val MIN_REFRESH_INTERVAL_MS = TimeUnit.MINUTES.toMillis(10)
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
@Volatile var lastAlternativesRefresh = Long.MIN_VALUE
}
}
@@ -31,8 +31,4 @@ interface NetworkPrefs {
/** List of base urls for Proton API proxies returned in the last DoH query. */
var alternativeBaseUrls: List<String>?
/** Timestamp for last alternatives refresh. Will used with [DohProvider.MIN_REFRESH_INTERVAL_MS]
* and compared to current timestamp */
var lastAlternativesRefresh: Long
}