将许多未来[Seq]连接成一个未来[Seq]

时间:2017-06-16 09:38:24

标签: scala future seq circuit-breaker

没有未来,这就是我将所有较小的Seq与 flatmap

组合成一个大型Seq的方式
category.getCategoryUrlKey(id: Int):Seq[Meta] // main method
val appDomains: Seq[Int]

val categories:Seq[Meta] = appDomains.flatMap(category.getCategoryUrlKey(_))

现在方法getCategoryUrlKey可能会失败。我在前面放了一个断路器,以避免在一定量的 maxFailures 后调用下一个元素。现在,断路器不会返回Seq而是返回Future[Seq]

lazy val breaker = new akka.pattern.CircuitBreaker(...)

private def getMeta(appDomainId: Int): Future[Seq[Meta]] = {
  breaker.withCircuitBreaker {
    category.getCategoryUrlKey(appDomainId)
  }
}

如何遍历List appDomains并将结果合并到一个Future [Seq]中,可能会进入Seq?

如果函数式编程适用,有没有办法在没有临时变量的情况下直接转换?

3 个答案:

答案 0 :(得分:5)

使用Future.sequence

挤压期货的seq

Future.sequenceSeq[Future[T]]转换为Future[Seq[T]]

在您的情况下,TSeq。在序列操作之后,您将得到Seq [Seq [T]]。因此,在使用展平后的序列操作之后,将其展平。

def squashFutures[T](list: Seq[Future[Seq[T]]]): Future[Seq[T]] =
  Future.sequence(list).map(_.flatten)

您的代码变为

Future.sequence(appDomains.map(getMeta)).map(_.flatten)

答案 1 :(得分:1)

从TraversableOnce [Future [A]]到Future [TraversableOnce [A]]

val categories = Future.successful(appDomains).flatMap(seq => {
    val fs = seq.map(i => getMeta(i))
    val sequenced = Future.sequence(fs)
    sequenced.map(_.flatten)
})
  • Future.successful(appDomains)appDomains提升到Future
  • 的上下文中

希望这有帮助。

答案 2 :(得分:0)

val metaSeqFutureSeq = appDomains.map(i => getMeta(i))
// Seq[Future[Seq[Meta]]]

val metaSeqSeqFuture = Future.sequence(metaSeqFutureSeq)
// Future[Seq[Seq[Meta]]]
// NOTE :: this future will fail if any of the futures in the sequence fails

val metaSeqFuture = metaSeqSeqFuture.map(seq => seq.flatten)
// Future[Seq[Meta]]

如果你想拒绝唯一失败的未来但保留成功的未来,那么我们必须有点创造力,并使用承诺建立我们的未来。

import java.util.concurrent.locks.ReentrantLock

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success}

def futureSeqToOptionSeqFuture[T](futureSeq: Seq[Future[T]]): Future[Seq[Option[T]]] = {
  val promise = Promise[Seq[Option[T]]]()

  var remaining = futureSeq.length

  val result = ArrayBuffer[Option[T]]()
  result ++ futureSeq.map(_ => None)

  val resultLock = new ReentrantLock()

  def handleFutureResult(option: Option[T], index: Int): Unit = {
    resultLock.lock()
    result(index) = option
    remaining = remaining - 1
    if (remaining == 0) {
      promise.success(result)
    }
    resultLock.unlock()
  }

  futureSeq.zipWithIndex.foreach({ case (future, index) => future.onComplete({
    case Success(t) => handleFutureResult(Some(t), index)
    case Failure(ex) => handleFutureResult(None, index)
  }) })

  promise.future
}

val metaSeqFutureSeq = appDomains.map(i => getMeta(i))
// Seq[Future[Seq[Meta]]]

val metaSeqOptionSeqFuture = futureSeqToOptionSeqFuture(metaSeqFutureSeq)
// Future[Seq[Option[Seq[Meta]]]]

val metaSeqFuture = metaSeqSeqFuture.map(seq => seq.flatten.flatten)
// Future[Seq[Meta]]