Akka HTTP:如何让流 Http().superPool() 在遇到错误 URL 时不抛出异常?
Akka HTTP: How to have the flow Http().superPool() not throw exceptions when encountering a bad URL?
我有很多 URL,我想使用 Akka HTTP 和流来获取这些 URL。
URL 的格式可能不正确,或者 DNS 可能无法解析主机名。在那些情况下,我希望 superPool() 流简单地 return 失败作为 Try[HTTPResponse] 而不是抛出异常并终止整个流。
我应该怎么做?我已经搜索过了,但一无所获,找不到一种方法来处理文档本身中的错误。
编辑:在稍微摆弄了一下之后,我发现唯一的问题是 HTTPS 网址,即使这样流也没有关闭,但我在控制台上打印了不应该的错误发生。这是一个小例子来重现:
import akka.stream.scaladsl.{Sink, Source}
import akka.NotUsed
import scala.util.Success
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.Http
import akka.actor.ActorSystem
implicit val system = ActorSystem()
Source.single((HttpRequest(uri = "https://a.com"), NotUsed))
.via(Http().superPool[NotUsed]())
.map(_ match {
case (Success(v), _) => {v.discardEntityBytes(); println("Success")}
case _ => println("Failure")
})
.runWith(Sink.ignore)
会不会是 Http().superPool
需要 HTTP URL 而有其他方法可以使用 HTTPS?
编辑 2:我已经打开了项目的问题:https://github.com/akka/akka-http/issues/3138
因为 superPool
应该已经 return Try
我会认为这是错误并报告问题。至少我还没有找到文档说无效的请求应该终止流或者 returning Failure
.
作为解决方法,我会尝试使用监督策略:
val decider: Supervision.Decider = {
case _: Malformed URL Exception => Supervision.Resume // put your exception type here
case _ => Supervision.Stop
}
Http().superPool()
.withAttributes(ActorAttributes.supervisionStrategy(decider))
这是 akka-http 10.1.11 的问题,已在 10.1.12 中修复。
在后一个版本中,默认情况下不会记录 HTTP 和 HTTPS url 的错误。
我有很多 URL,我想使用 Akka HTTP 和流来获取这些 URL。
URL 的格式可能不正确,或者 DNS 可能无法解析主机名。在那些情况下,我希望 superPool() 流简单地 return 失败作为 Try[HTTPResponse] 而不是抛出异常并终止整个流。
我应该怎么做?我已经搜索过了,但一无所获,找不到一种方法来处理文档本身中的错误。
编辑:在稍微摆弄了一下之后,我发现唯一的问题是 HTTPS 网址,即使这样流也没有关闭,但我在控制台上打印了不应该的错误发生。这是一个小例子来重现:
import akka.stream.scaladsl.{Sink, Source}
import akka.NotUsed
import scala.util.Success
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.Http
import akka.actor.ActorSystem
implicit val system = ActorSystem()
Source.single((HttpRequest(uri = "https://a.com"), NotUsed))
.via(Http().superPool[NotUsed]())
.map(_ match {
case (Success(v), _) => {v.discardEntityBytes(); println("Success")}
case _ => println("Failure")
})
.runWith(Sink.ignore)
会不会是 Http().superPool
需要 HTTP URL 而有其他方法可以使用 HTTPS?
编辑 2:我已经打开了项目的问题:https://github.com/akka/akka-http/issues/3138
因为 superPool
应该已经 return Try
我会认为这是错误并报告问题。至少我还没有找到文档说无效的请求应该终止流或者 returning Failure
.
作为解决方法,我会尝试使用监督策略:
val decider: Supervision.Decider = {
case _: Malformed URL Exception => Supervision.Resume // put your exception type here
case _ => Supervision.Stop
}
Http().superPool()
.withAttributes(ActorAttributes.supervisionStrategy(decider))
这是 akka-http 10.1.11 的问题,已在 10.1.12 中修复。 在后一个版本中,默认情况下不会记录 HTTP 和 HTTPS url 的错误。