简体   繁体   English

如何在超时的单独调度程序上运行Akka Streams图?

[英]How to run Akka Streams graph on a separate dispatcher with timeout?

This question is based on a pet project that I did and this SO thread. 这个问题基于我做过的宠物项目和这个SO线程。 Inside a Akka HTTP route definition, I start a long-running process, and naturally I want to do that without blocking the user. 在Akka HTTP路由定义中,我启动了一个长时间运行的进程,当然我想在不阻塞用户的情况下这样做。 I'm able to achieve this with the code snippet below: 我可以使用下面的代码片段实现此目的:

blocking-io-dispatcher {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    fixed-pool-size = 16
  }
  throughput = 1
}

complete {
  Try(new URL(url)) match {
    case scala.util.Success(u) => {
      val src = Source.fromIterator(() => parseMovies(u).iterator)

      src
        .via(findMovieByTitleAndYear)
        .via(persistMovies)
        .toMat(Sink.fold(Future(0))((acc, elem) => Applicative[Future].map2(acc, elem)(_ + _)))(Keep.right)
        // run the whole graph on a separate dispatcher
        .withAttributes(ActorAttributes.dispatcher("blocking-io-dispatcher"))
        .run.flatten
        .onComplete {
          _ match {
            case scala.util.Success(n) => logger.info(s"Created $n movies")
            case Failure(t) => logger.error(t, "Failed to process movies")
          }
        }

      Accepted
    }
    case Failure(t) => logger.error(t, "Bad URL"); BadRequest -> "Bad URL"
  }
}

What's the problem then if I've already solved it? 如果我已经解决了那么问题是什么? The problem is that I'm not sure how to set a timeout. 问题是我不确定如何设置超时。 The execution of the graph creates a Future that executes until complete on the dedicated blocking-io-dispatcher . 图表的执行创建了一个Future ,它在专用的blocking-io-dispatcher上执行直到完成。 If I add a Await call, the code blocks. 如果我添加一个Await调用,代码会阻塞。 Is there a way to put a timeout? 有没有办法暂停?

completionTimeout stage should help here. completionTimeout阶段应该有帮助。 Example below: 示例如下:

src
    .completionTimeout(5.seconds)
    ...
    .run.flatten
    .onComplete {
        case scala.util.Success(n) => logger.info(s"Created $n movies")
        case Failure(t: TimeoutException) => logger.error(t, "Timed out")
        case Failure(t) => logger.error(t, "Failed to process movies")
    }

Docs reference here . 文档在这里引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM