Kyuubi
Kyuubi

Reputation: 1240

Multiple streamed response combined in a single iterator in Scala

I have multiple replicas of a service from which I am receiving a stream of response:

// RPC call to server that returns a stream of response.
def getResult(input: Request): Iterator[ResultItem]

On the client side I call this method in parallel on multiple threads:

val results: Seq[Future[Iterator[ResultItem]]] = requests.map{ request => 
  Future {
    getResult(request)
  }(ec)
}

I want to return a single stream of Iterator[ResultItem] that has the entries that have already been processed. How can I do this in scala? Essentially, how do I implement this method?

def getResults(inputs: Seq[Request]): Iterator[ResultItem] = {
  val results: Future[Iterator[ResultItem]] = inputs.map{ request => 
    Future {
      getResult(request)
    }(ec)
  }
  
  <SCALA MAGIC to COMBINE MULTIPLE STREAMS>
}

NOTE: The returned iterator should have entries available as soon as they are processed, irrespective of the the stream they are in. It shouldn't be that first all entries from Stream 1 are accessed and then Stream 2. If an entry in stream 2 is available before Stream 1, then that should be returned first.

Upvotes: 0

Views: 22

Answers (0)

Related Questions