【问题标题】:Akka http request StreamTcpExceptionAkka http 请求 StreamTcpException
【发布时间】:2020-10-29 22:27:02
【问题描述】:

我正在学习 Akka 库,我已经使用 Play 框架在我的系统中设置了一个本地服务器,我正在 使用 Akka 库编写客户端应用程序。

implicit val system = ActorSystem("testing")
implicit val materializer = ActorMaterializer()
import system.dispatcher

  val serverHttpRequests = files.map { file =>
    (
    HttpRequest (
      HttpMethods.POST,
      uri = Uri(Url),
      entity = HttpEntity(
        ContentTypes.`text/plain(UTF-8)`,
        getChartData(file)                  // reading file from local file system
      )
    ), file.getName
   )
  }

  val res = Source(serverHttpRequests)
    .via(Http().cachedHostConnectionPool[String]("localhost", 9001))
    .runForeach {
      case (Success(response), fileName) => logger.info(s"$fileName, $response")
        response.discardEntityBytes()
      case (Failure(ex), fileName) => logger.info(s"$fileName failed with $ex")
    }

文件很少,需要1分钟以上才能得到本地服务器的响应, 对于这些文件,我收到以下异常
fileName failed with akka.stream.StreamTcpException: The connection closed with error: An existing connection was forcibly closed by the remote host

我尝试将客户端和服务器的超时时间增加到 300 秒。
客户application.conf

akka.http.client.idle-timeout = 300s
akka.http.client.connecting-timeout = 300s

服务器application.conf

akka.http.host-connection-pool.idle-timeout = 300s
akka.http.host-connection-pool.client.idle-timeout = 300s
play.server.akka.https.idleTimeout = 300s
play.server.akka.requestTimeout = 300s
play.server.akka.terminationTimeout = 300s
akka.http.server.bind-timeout = 300s

对于上面的代码,我正在尝试处理 10 个文件,每个文件的大小都小于20 KB,并且响应 从服务器,平均每个文件将小于120 KB。 我还读到 Akka IO.selectors 将通过发送心跳来保持连接,
我正在尝试了解此异常的确切原因并进行处理。

【问题讨论】:

    标签: scala playframework akka akka-http connection-timeout


    【解决方案1】:

    如果您查看 akka.stream.impl.io.TcpStages,您会看到当连接因 IO 错误而关闭时会引发 StreamTcpException(s"The connection closed with error: $cause") 异常。

    也许你可以试试akka.http.host-connection-pool.max-connections

    【讨论】:

    • 我尝试将akka.http.host-connection-pool.max-connections从默认值4增加到6、8和12,仍然出现异常
    • hlrmm,你有没有考虑尝试卷曲端点?
    猜你喜欢
    • 2016-08-15
    • 1970-01-01
    • 2014-09-30
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多