How to implement a limited call with the lockout client and coroutines

I have the following code:

val context = newFixedThreadPoolContext(nThreads = 10, name="myThreadPool")
val total = 1_000_000 //can be other number as well
val maxLimit = 1_000
return runBlocking {
  (0..total step maxLimit).map {
    async(context) {
      val offset = it
      val limit = it + maxLimit
      blockingHttpCall(offset, limit)
    }
  }.flatMap {
    it.await()
  }.associateBy {
    ...
  }.toMutableMap()
}

I would like only 10 calls to be made at the same time as api blocking. However, the above code does not seem to do this as I expect (I think all calls start immediately), or at least I don't understand if that is the case. What is the correct way to implement it? Will the same solution work if I use async api for refitting?

+6
source share
1 answer

, - OkHttp API concurrency, , default concurrency OkHttp

, Dispatcher OkHttpClient.Builder

,

, , , , newFixedThreadPoolContext ( 1000 10 ).

+ , OkHttp concurrency ( ) ( , kotlin-coroutines-retrofit). HTTP- .

, API + OkHttp internal concurrency, concurrency, , , ( ), , , concurrency, .

API ( ) .

, concurrency OkHttpClient ( DI, Retrofit + OkHttp concurrency). , , , .

+2

All Articles