title: Throttle HTTP request on Akka/Spray
body: <p>I'm using Akka actors in Scala to download resources from external service (HTTP get request). Response from external service is JSON and I have to use paging (provider is very slow). I want to download all paged results concurrently in 10 threads. I use an URL such as this to download chunk: <a href="http://service.com/itmes?limit=50&amp;offset=1000" rel="nofollow">http://service.com/itmes?limit=50&amp;offset=1000</a></p>
  
  <p>I have created following pipeline:</p>
  
  <pre><code>ScatterActor =&gt; RoundRobinPool[10](LoadChunkActor) =&gt; Aggreator
  </code></pre>
  
  <p>ScatterActor takes total count of items to download and divides it into chunks. I created 10 LoadChunkActor's to process tasks concurrently.</p>
  
  <pre><code>  override def receive: Receive = {
      case LoadMessage(limit) =&gt;
      val offsets: IndexedSeq[Int] = 0 until limit by chunkSize
      offsets.foreach(offset =&gt; context.system.actorSelection(pipe) !
      LoadMessage(chunkSize, offset))
   }
  </code></pre>
  
  <p>LoadChunkActor uses Spray to send request. Actor looks like this:</p>
  
  <pre><code>val pipeline = sendReceive ~&gt; unmarshal[List[Items]]
  override def receive: Receive = {
    case LoadMessage(limit, offset) =&gt;
      val uri: String = s"http://service.com/items?limit=50&amp;offset=$offset"
      val responseFuture = pipeline {Get(uri)}
      responseFuture onComplete {
        case Success(items) =&gt; aggregator ! Loaded(items)
      }
   }
  </code></pre>
  
  <p>As you can see, LoadChunkActor is requesting chunk from external service and adding callback to be run onComplete. Actor is now ready to take another message and he is requesting another chunk. Spray is using nonblocking API to download chunks. In result external service is flooded with my requests and I get timeouts.</p>
  
  <p>How can I schedule list of tasks but I want to process maximum 10 at the same time?</p>