perf: optimize synchronous I/O in AuthLoginViewModel (#699)
* perf: optimize synchronous I/O in AuthLoginViewModel Replace blocking OkHttp .execute() with a suspending .await() extension (CallExt.kt) backed by suspendCancellableCoroutine, freeing Dispatchers.IO threads during network waits. Probe/connect paths made suspend. - Call.await() guards both onResponse/onFailure with continuation.isActive to avoid AlreadyResume on cancellation. - Removes non-asserting AuthLoginViewModelBenchmark timing dump. * perf: optimize synchronous I/O in AuthLoginViewModel Replaced the blocking `.execute()` OkHttp calls with a suspending `.await()` extension function inside `AuthLoginViewModel`. This frees up the `Dispatchers.IO` threads by deferring the blocking operations to OkHttp's internal task runner, providing a safer async approach and preventing Thread pool starvation on slow network responses. Modified `probeDashboardInternal` and `connectBasicAuth` to be proper `suspend` functions and resolved corresponding unit tests. * fix(test): add missing import io.mockk.any in AuthLoginViewModelTest * fix(test): fix mockk imports and Log matchers in AuthLoginViewModelTest * fix(test): use persistent mockProbeClient in AuthLoginViewModelTest --------- Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
1 parent
152b7d5c0d
commit
8c13f9bed0
4 files changed
+116
-13
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package com.m57.hermescontrol.data.remote
|
||||
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
||||
suspend fun Call.await(): Response =
|
||||
suspendCancellableCoroutine { continuation ->
|
||||
continuation.invokeOnCancellation {
|
||||
cancel()
|
||||
}
|
||||
enqueue(
|
||||
object : Callback {
|
||||
override fun onFailure(
|
||||
call: Call,
|
||||
e: IOException,
|
||||
) {
|
||||
if (continuation.isActive) {
|
||||
continuation.resumeWithException(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResponse(
|
||||
call: Call,
|
||||
response: Response,
|
||||
) {
|
||||
if (continuation.isActive) {
|
||||
continuation.resume(response)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user