I need block an OkHttp Interceptor to wait other a...
# announcements
I need block an OkHttp Interceptor to wait other async operation return another Response with the same Chain. Is runBlocking the better way to do that? Below is my current implementation. It's working but I'm in doubt if there is a better way without block many parallel requests.
Copy code
internal class CustomInterceptor(
    private val processor: Something
) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val response = chain.proceed(chain.request())
        // FIXME: is this fine? Can this throw Socket timeout?
        return runBlocking(Dispatchers.Default) {
            processor.suspendUtilHaveAnotherResponseOrThrow(response, chain)
already exist? If you have to call a suspend function from an
method and need its return value, then you need
, yes. However, there’s probably no need to specify a custom dispatcher – if the suspend function you’re calling needs to be on a particular dispatcher to do some work, that should be a private concern of that function. Zooming out, i’m not sure what exactly the implications of having an interceptor block for a long time is on general okhttp performance, but it seems like interceptors might be the wrong level of abstraction if you need to chain multiple requests a lot. Might be worth building your own abstraction on top of okhttp (instead of inside it) that can use coroutines properly. (FYI there’s #coroutines and #squarelibraries channels which might be more helpful in the future.)
👍 1