uli
11/23/2021, 9:02 AMlimitedParallelism
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?
val overallIODispatcher = Dispatchers.IO.limitedParallelism(64)
val endpoint1Dispatcher = overallIODispatcher.limitedParallelism(50)
val endpoint2Dispatcher = overallIODispatcher.limitedParallelism(50)
bezrukov
11/23/2021, 9:45 AMlimitedParallelism
, 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 detailsVsevolod Tolstopyatov [JB]
11/23/2021, 11:28 AMWould the following code do that?Yes, this is exactly the intended way.
if you will useIf you meant the pattern likedirectly, you may end up with 128 threads.<http://Dispatchers.IO|Dispatchers.IO>
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 demandbezrukov
11/23/2021, 12:31 PM(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
uli
11/23/2021, 12:34 PMval 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?Vsevolod Tolstopyatov [JB]
11/23/2021, 12:57 PMuli
11/23/2021, 12:57 PMuli
11/25/2021, 6:50 PM