多个请求后Akka Http singleRequest流挂起

时间:2018-12-06 22:57:42

标签: scala akka akka-stream akka-http actorsystem

我正尝试使用akka-https Http().singleRequest一次请求多个链接。但是,在下面的演示代码中,foldLeft仅循环四次,并且应用程序永远不会到达println语句或终止。

我正在使用默认的akka​​配置,但是我认为Http().singleRequest正在消耗我的线程而不是释放它们。如果将链接列表更改为仅包括4个链接,则应用程序终止,并且可以看到println。但是,第五个和该应用将在第五个循环中挂起。

任何人以前都看过此书,或者我的印象有问题。该特定用例中的ActorSystem的功能。

val links = List(
  "https://www.google.com/",
  "https://www.google.com/",
  "https://www.google.com/",
  "https://www.google.com/",
  "https://www.google.com/",
  "https://www.google.com/",
  "https://www.google.com/",
  "https://www.google.com/"
)

implicit val as: ActorSystem = ActorSystem()
implicit val mat: ActorMaterializer = ActorMaterializer()

import as.dispatcher

def get(url: String): Future[Either[Throwable, Unit]] = {
  Http().singleRequest(HttpRequest(uri = url)) transformWith {
    case Success(_) =>
      Future.successful(Right(()))
    case Failure(e) =>
      Future.successful(Left(e))
  }
}

def getLinks(): Future[Seq[Unit]] = {
  links.foldLeft(Future.successful(Seq.empty[Unit])){
    case (f, e) => f.flatMap { ls =>
      get(e) map {
        case Right(i) => ls :+ i
        case Left(_) => ls
      }
    }
  }
}

getLinks() transformWith{
  case Success(ls) =>
    println("terminated")
    println(ls.length)
    Future.successful(())
  case Failure(e) =>
    println("terminated")
    println(e.getMessage)
    Future.successful(())
}

1 个答案:

答案 0 :(得分:1)

使用(或丢弃)请求实体是强制性的(来自https://doc.akka.io/docs/akka-http/current/implications-of-streaming-http-entity.html)。

尝试将response.discardEntityBytes()添加到您的代码中。