I know that I can transform a Seq[Future[T]]
into a Future[Seq[T]]
via
val seqFuture = Future.sequence(seqOfFutures)
seqFuture.map((seqT: Seq[T]) => {...})
My problem now is, that I have 700 futures in that sequence and I want to be able to control how many of them are resolved in parallel as each future will call an internal rest api, and having 700 requests at the same time is like fireing a dos-attack against that server.
I rather only have something like 10 futures being resolved at a time.
How can I achieve that?
Trying pamu's answer I see the error:
[error] /home/philipp/src/bluebat/src/main/scala/com/dreamlines/metronome/service/JobFetcher.scala:32:44: com.dreamlines.commons.LazyFuture[A] does not take parameters
[error] val batch = Future.sequence(c.map(_()))
[error] ^
[error] /home/philipp/src/bluebat/src/main/scala/com/dreamlines/metronome/service/JobFetcher.scala:32:28: no type parameters for method sequence: (in: M[scala.concurrent.Future[A]])(implicit cbf: scala.collection.generic.CanBuildFrom[M[scala.concurrent.Future[A]],A,M[A]], implicit executor: scala.concurrent.ExecutionContext)scala.concurrent.Future[M[A]] exist so that it can be applied to arguments (List[Nothing])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : List[Nothing]
[error] required: ?M[scala.concurrent.Future[?A]]
[error] val batch = Future.sequence(c.map(_()))
[error] ^
[error] /home/philipp/src/bluebat/src/main/scala/com/dreamlines/metronome/service/JobFetcher.scala:32:42: type mismatch;
[error] found : List[Nothing]
[error] required: M[scala.concurrent.Future[A]]
[error] val batch = Future.sequence(c.map(_()))
[error] ^
[error] /home/philipp/src/bluebat/src/main/scala/com/dreamlines/metronome/service/JobFetcher.scala:32:36: Cannot construct a collection of type M[A] with elements of type A based on a collection of type M[scala.concurrent.Future[A]].
[error] val batch = Future.sequence(c.map(_()))
[error] ^
[error] four errors found