Akka http 请求 StreamTcpException

Akka http request StreamTcpException

我正在学习 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 将通过发送心跳来保持连接,
我正在尝试了解此异常的确切原因并进行处理。

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

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