Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What dispatcher to use for blocking IO, with Netty? #1288

Closed
glasser opened this issue Aug 17, 2019 · 3 comments
Closed

What dispatcher to use for blocking IO, with Netty? #1288

glasser opened this issue Aug 17, 2019 · 3 comments
Assignees
Labels

Comments

@glasser
Copy link

glasser commented Aug 17, 2019

I'm a little confused about what the current recommendation for doing blocking IO is, both in Kotlin Coroutines as a whole, and in ktor. I've read Kotlin/kotlinx.coroutines#261 and a bunch of issues that link to it. I assume it's withContext(Dispatchers.IO), but that doesn't actually seem to be in any documentation for either Kotlin Coroutines or Ktor!

One specific confusion I have — my understanding is that the cool thing about Dispatchers.IO (vs just Executors.newFixedThreadPool().asCoroutineDispatcher()) is that individual threads aren't permanently assigned to Dispatchers.Default vs Dispatchers.IO, and withContext(Dispatchers.IO) can often just mean adjusting the thread assignment counts without a full context switch. (I've also discovered that with the internal API (Dispatchers.Default as ExperimentalCoroutineDispatcher).blocking(N) I can create my own Dispatchers.IO-like dispatchers so that I can limit particular types of IO (sql, etc) to a specific number of threads in that thread pool.)

However, I also note that in the Netty engine (which we are using kind of by default, porting our app from Netty-based Ratpack), the dispatcher used for ApplicationCalls isn't Dispatchers.Default at all, but a Netty-specific dispatcher that just sends the coroutine directly to Netty.

I assume I still need to use some alternative dispatcher for blocking code, right? Netty's event loop also wants to not block?

But in this case is there any real point with Ktor/Netty to using Dispatchers.IO instead of Executors.newFixedThreadPool().asCoroutineDispatcher()? If the advantage of Dispatchers.IO over Executors.newFixedThreadPool().asCoroutineDispatcher() is that it can reuse a thread from Dispatchers.Default but we're not using Dispatchers.Default, should I even bother? I suppose I do still get to share threads between multiple dispatchers made with .blocking().

@glasser
Copy link
Author

glasser commented Aug 18, 2019

Or should we just use CIO? :)

@glasser
Copy link
Author

glasser commented Aug 18, 2019

Although, heh, looks like CIO uses the same code as Dispatchers.Default/IO but its own version? And doesn't expose them in an easy way to make your own separate blocking subpool either?

@e5l
Copy link
Member

e5l commented Jun 5, 2020

You can use Dispatchers.IO. It's a preferred way right know.

@e5l e5l closed this as completed Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants