Solution: DiscNewsRepository

You need to wrap the blocking function with withContext, which uses the appropriate dispatcher. In other cases, this dispatcher might have been Dispatchers.IO, but in this case you need to create an independent dispatcher with a specific thread limit. The best choice is to use Dispatchers.IO with the limitedParallelism parameter set to 20.

class DiscNewsRepository( private val discReader: DiscReader ) : NewsRepository { private val dispatcher = Dispatchers.IO .limitedParallelism(20) override suspend fun getNews( newsId: String ): News = withContext(dispatcher) { val (title, content) = discReader.read("user/$newsId") News(title, content) } }

Example solution in playground

import kotlinx.coroutines.* import org.junit.Test import kotlin.system.measureTimeMillis import kotlin.test.assertEquals class DiscNewsRepository( private val discReader: DiscReader ) : NewsRepository { private val dispatcher = Dispatchers.IO .limitedParallelism(20) override suspend fun getNews( newsId: String ): News = withContext(dispatcher) { val (title, content) = discReader.read("user/$newsId") News(title, content) } } interface DiscReader { fun read(key: String): List<String> } interface NewsRepository { suspend fun getNews(newsId: String): News } data class News(val title: String, val content: String) class DiscNewsRepositoryTests { @Test fun `should read data from disc using DiscReader`() = runBlocking { val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content"))) val res = repo.getNews("SomeUserId") assertEquals("Some title", res.title) assertEquals("Some content", res.content) } @Test fun `should be prepared for many reads at the same time`() = runBlocking<Unit> { val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content"))) val time = measureTimeMillis { coroutineScope { repeat(10) { id -> launch { repo.getNews("SomeUserId$id") } } } } assert(time < 2000) { "Should take less than 200, took $time" } } @Test(timeout = 2000) fun `should be prepared for 20 parallel reads`() = runBlocking<Unit> { val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content"))) val time = measureTimeMillis { coroutineScope { repeat(20) { id -> launch { repo.getNews("SomeUserId$id") } } } } assert(time < 1900) { "Should take less than 2000, took $time" } } @Test(timeout = 3000) fun `should not allow more than 20 parallel reads`() = runBlocking<Unit> { val repo = DiscNewsRepository(OneSecDiscReader(listOf("Some title", "Some content"))) val time = measureTimeMillis { coroutineScope { repeat(21) { id -> launch { repo.getNews("SomeUserId$id") } } } } assert(time > 2000) { "Should take longer than 2000, took $time" } } class OneSecDiscReader(private val response: List<String>) : DiscReader { override fun read(key: String): List<String> { Thread.sleep(1000) return response } } }