diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-03-25 15:23:43 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-03-25 18:19:59 +0000 |
| commit | 82fb4fea6b0fe9089a8ed0a05d6f8f8bbdf845ad (patch) | |
| tree | d9fadd902498eb29d750ef1502307db87b51a7e3 /android/src | |
| parent | 9d537ce034c0a3dfc9d73491d4803fa9dc290737 (diff) | |
| download | mullvadvpn-82fb4fea6b0fe9089a8ed0a05d6f8f8bbdf845ad.tar.xz mullvadvpn-82fb4fea6b0fe9089a8ed0a05d6f8f8bbdf845ad.zip | |
Refactor to simplify fetcher loop
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt index 5b0f613d0f..2f118ede6d 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt @@ -3,13 +3,16 @@ package net.mullvad.mullvadvpn.service.endpoint import kotlin.properties.Delegates.observable import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ClosedReceiveChannelException import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.actor import kotlinx.coroutines.channels.sendBlocking -import kotlinx.coroutines.withTimeout +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.receiveAsFlow import net.mullvad.mullvadvpn.ipc.Event import net.mullvad.mullvadvpn.model.Constraint import net.mullvad.mullvadvpn.model.GeoIpLocation @@ -100,39 +103,27 @@ class LocationInfoCache(private val endpoint: ServiceEndpoint) { } private suspend fun fetcherLoop(channel: ReceiveChannel<RequestFetch>) { - while (true) { - var fetchType = channel.receive() - var newLocation = daemon.await().getCurrentLocation() - - while (newLocation == null || !channel.isEmpty) { - fetchType = delayOrReceive(fetchRetryDelays, channel, fetchType) - newLocation = daemon.await().getCurrentLocation() - } - - handleNewLocation(newLocation, fetchType) - fetchRetryDelays.reset() - } + channel.receiveAsFlow() + .flatMapLatest(::fetchCurrentLocation) + .collect(::handleFetchedLocation) } - private suspend fun delayOrReceive( - delays: ExponentialBackoff, - channel: ReceiveChannel<RequestFetch>, - currentValue: RequestFetch - ): RequestFetch { - try { - val newValue = withTimeout(delays.next()) { - channel.receive() - } + private fun fetchCurrentLocation(fetchType: RequestFetch) = flow { + var newLocation = daemon.await().getCurrentLocation() - delays.reset() + fetchRetryDelays.reset() - return newValue - } catch (timeOut: TimeoutCancellationException) { - return currentValue + while (newLocation == null) { + delay(fetchRetryDelays.next()) + newLocation = daemon.await().getCurrentLocation() } + + emit(Pair(newLocation, fetchType)) } - private fun handleNewLocation(newLocation: GeoIpLocation, fetchType: RequestFetch) { + private suspend fun handleFetchedLocation(pairItem: Pair<GeoIpLocation, RequestFetch>) { + val (newLocation, fetchType) = pairItem + if (fetchType == RequestFetch.ForRealLocation) { lastKnownRealLocation = newLocation } |
