Open BenHenning opened 3 years ago
/cc @jcqli
I dug into this a bit more last night and the findings are mixed.
Good news Retrofit apparently leans on OkHttp for asynchronous threading management, but does perform callback invocation in background threads (which is configurable by passing in a custom ExecutorService). OkHttp lets us configure the threads used for asynchronous processing by passing in a custom dispatcher class (which uses ExecutorService under the hood). Fortunately, we also leverage ExecutorServices as the base concurrency primitives for all of our coroutine dispatchers making it easy to centralize our threading across OkHttp, Retrofit, and the rest of the app (only Glide won't be part of this since they intentionally use their own fine-tuned ExecutorServices).
Not-so-good news We expect all threading controls to go through Kotlin coroutines which means we have no way currently to synchronize threading across the Java world with ExecutorServices and the Kotlin world with coroutines. There are two options to fix this:
(1) seems particularly scary since coroutines are difficult to make block, and ExecutorService has blocking APIs that must be implemented (see warning at the top of CoroutineExecutorService & the implementation for a lot more details on this). This has resulted in very challenging situations that aren't obvious to fix. Moving toward an ExecutorService-based synchronization model will make a lot more sense (since then we're coordinating all threading to a single ExecutorService rather than via a wrapper ExecutorService through corotouines to an ExecutorService). This will also nicely let us replace the existing CoroutineExecutorService when testing Glide.
While (2) seems nice, it has some drawbacks:
Retrofit needs: