Colton Idle
08/11/2025, 3:12 PMfun sendSync(req: Req): Res
return runBlocking {
sendAsync()
}
suspend fun sendAsync() {...}
kevin.cianfarini
08/11/2025, 3:19 PMsendAsync
to delegate to sendSync
and not the reverse. OkHttp isn't non-blocking so a runBlocking
call wrapping a sendAsync
will perform much worse than the reverse.kevin.cianfarini
08/11/2025, 3:19 PMColton Idle
08/11/2025, 3:22 PMColton Idle
08/11/2025, 3:26 PMkevin.cianfarini
08/11/2025, 3:28 PMfun sendSync(request: Request): Response {
// Call OkHttp.
}
suspend fun sendAsync(request: Request, context: CoroutineContext): Response {
withContext(context) { sendSync(...) }
}
This should go suspend -> blocking just once.gildor
08/12/2025, 7:27 AMgildor
08/12/2025, 7:51 AMgildor
08/12/2025, 7:51 AMkevin.cianfarini
08/12/2025, 9:30 PMgildor
08/13/2025, 5:06 AMRequest
(any implementation) abstract and have extension function for suspend/blocking usage is the best way to implement it without compromise of ux and efficiency, though it works only for simple cases when only http request is necessary, if you add other code on top of it, which can be suspend/blocking, it causes the same dilema again.
Anyway, I think suspend must be prioritized, it's more safe (structured concurrency, no accidental thread blocking), flexible, supports cancellation, and blocking can be a wrapper, if it not even the most efficient oneColton Idle
08/13/2025, 12:41 PMColton Idle
08/13/2025, 12:44 PM