-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start daemon and management service on main thread
- Loading branch information
Showing
4 changed files
with
57 additions
and
12 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/RunBlocking.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.mullvad.mullvadvpn.lib.common.util | ||
|
||
import kotlin.coroutines.CoroutineContext | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withTimeout | ||
|
||
fun <T> runBlockingWithTimeout( | ||
context: CoroutineContext, | ||
timeout: Long, | ||
block: suspend CoroutineScope.() -> T, | ||
): T = | ||
runBlocking(context = context) { | ||
val d = async { block() } | ||
withTimeout(timeout) { d.await() } | ||
} |
29 changes: 29 additions & 0 deletions
29
android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/ApiEndpointResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package net.mullvad.mullvadvpn.service | ||
|
||
import co.touchlab.kermit.Logger | ||
import kotlin.coroutines.cancellation.CancellationException | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import net.mullvad.mullvadvpn.lib.common.util.runBlockingWithTimeout | ||
import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpoint | ||
import net.mullvad.mullvadvpn.lib.endpoint.ApiEndpointConfiguration | ||
|
||
class ApiEndpointResolver(private val dispatcher: CoroutineDispatcher = Dispatchers.IO) { | ||
fun resolve(apiEndpointConfiguration: ApiEndpointConfiguration): ApiEndpoint? = | ||
try { | ||
runBlockingWithTimeout( | ||
context = CoroutineScope(dispatcher).coroutineContext, | ||
timeout = MAX_RESOLVE_WAIT_TIME_MS | ||
) { | ||
apiEndpointConfiguration.apiEndpoint() | ||
} | ||
} catch (e: CancellationException) { | ||
Logger.e("Could not resolve api endpoint configuration") | ||
null | ||
} | ||
|
||
companion object { | ||
const val MAX_RESOLVE_WAIT_TIME_MS = 5000L | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters