Hi there, `limitedParallelism` in 1.6.0 looks very...
# coroutines
u
Hi there,
limitedParallelism
in 1.6.0 looks very promising. One question that jumped immediately to my mind: Is this feature recursive? i.e. let’s say I have 2 endpoints I want to serve and I want to limit thread usage to 64 overall, but only allow 50 threads per endpoint. Would the following code do that?
Copy code
val overallIODispatcher = Dispatchers.IO.limitedParallelism(64)
val endpoint1Dispatcher = overallIODispatcher.limitedParallelism(50)
val endpoint2Dispatcher = overallIODispatcher.limitedParallelism(50)
b
yes, it is. However, Dispatchers.IO has special implementation for
limitedParallelism
, so if you will use
<http://Dispatchers.IO|Dispatchers.IO>
directly, you may end up with 128 threads. See https://github.com/Kotlin/kotlinx.coroutines/issues/2943 for more details
v
Would the following code do that?
Yes, this is exactly the intended way.
if you will use 
<http://Dispatchers.IO|Dispatchers.IO>
 directly, you may end up with 128 threads.
If you meant the pattern like
Copy code
val endpoint1Dispatcher = Dispatchers.IO.limitedParallelism(50)
val endpoint2Dispatcher = Dispatchers.IO.limitedParallelism(50)
Then the number of threads can be as high as 100 (as long as Dispatchers.IO is not used anywhere else). Though all these threads will be created lazily on demand
b
(as long as Dispatchers.IO is not used anywhere else)
yep, I meant this ☝️ that using IO dispatcher anywhere else may lead to spawn 64 more threads
u
No. I meant
Copy code
val endpoint1Dispatcher = overallIODispatcher.limitedParallelism(50)
val endpoint2Dispatcher = overallIODispatcher.limitedParallelism(50)
i.e. using an already limited dispatcher as host The question is, will
endpoint1Dispatcher
+
endpoint2Dispatcher
together be limited to 64 threads, as this is what their host
overallIODispatcher
is limited to?
v
Yes, they will be limited to 64 threads in total
u
After reading again, I guess the answer is already in your comments above. So it is, Yes it does exactly what you want. But note Dispatcher.IO as a host for limitedParallelism itself is unlimited.
And I guess the fix has been forward ported to Pannenhilfe StaticContentActivity (by accident?)