title: RxJava: Specifying maximum concurrency for a list of Completables
body: <p>I'm trying to query an API that provides me with a list of files to download (as follows). I then proceed to download these files while also re-querying the API for anything that may have been missed in the initial call. </p>
  
  <p><code>Completable#mergeDelayError(Iterable&lt;? extends CompletableSource&gt; sources)</code> is used to ensure I can perform multiple tasks in parallel and get notified when everything has been completed.</p>
  
  <pre><code>fun fetchAndDownload(details: List&lt;String&gt;): Completable = 
      exampleApi.fetchPackages(details) // This is a Single
          .flatMapCompletable { (results, retry) -&gt; 
              val completables = mutableListOf&lt;Completable&gt;()
              results.mapTo(completables) { value -&gt;
                  exampleApi.download(value).subscribeOn(Schedulers.io())
              }
  
              if (retry.isNotEmpty()) { 
                  completables += fetchAndDownload(retry)
                      .delay(3L, TimeUnit.SECONDS)
                      .subscribeOn(Schedulers.io())
              }
              Completable.mergeDelayError(completables)
          }
  </code></pre>
  
  <p>However, this implementation has the possibility of overwhelming the network and/or thread count by executing too many things at once. I am therefore wondering what the best approach would be to limit the number of <code>completables</code> that are running at once. </p>
  
  <p>I am aware of <code>Completable#mergeDelayError(Publisher&lt;? extends CompletableSource&gt; sources, int maxConcurrency)</code> but am not sure how to convert my <code>List&lt;Completable&gt;</code> to the required <code>Publisher</code>. An alternative solution is to provide a custom <code>Scheduler</code> that has a maximum thread count, but I am also not sure how to provide such a <code>Schduler</code> (that I can clean and discard when it is no longer needed).</p>