【问题标题】:Akka-Http WebSocket: handling incoming messages very slowAkka-Http WebSocket:处理传入消息非常慢
【发布时间】:2019-08-06 16:07:59
【问题描述】:

我有一个来自akka-http docs for WebSocket client-side 的稍微修改的示例:

package org.enso

import akka.actor.ActorSystem
import akka.{Done, NotUsed}
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.stream.scaladsl._
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.ws._
import akka.pattern.after

import scala.concurrent.duration._
import scala.concurrent.{Future, Promise}

object SingleWebSocketRequest {
  def main(args: Array[String]): Unit = {
    implicit val system: ActorSystem = ActorSystem()
    implicit val materializer: ActorMaterializer = ActorMaterializer()
    import system.dispatcher

    // print each incoming text message
    val printSink: Sink[Message, Future[Done]] =
      Sink.foreach { _ =>
        println("foo")
      }

    val s1: Source[Message, NotUsed] =
      Source(Stream.fill(100){
        TextMessage("""{"method":"ping","responseSize":4}""")
      })

    // the Future[Done] is the materialized value of Sink.foreach
    // and it is completed when the stream completes
    val flow: Flow[Message, Message, Future[Done]] =
    Flow.fromSinkAndSourceMat(printSink, s1)(Keep.left)

    // upgradeResponse is a Future[WebSocketUpgradeResponse] that
    // completes or fails when the connection succeeds or fails
    // and closed is a Future[Done] representing the stream completion from above
    val (upgradeResponse, closed) =
    Http().singleWebSocketRequest(WebSocketRequest("ws://localhost:8080"), flow)

    val connected = upgradeResponse.map { upgrade =>
      // just like a regular http request we can access response status which is available via upgrade.response.status
      // status code 101 (Switching Protocols) indicates that server support WebSockets
      if (upgrade.response.status == StatusCodes.SwitchingProtocols) {
        Done
      } else {
        throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
      }
    }

    // in a real application you would not side effect here
    // and handle errors more carefully
    connected.onComplete(println)

    closed.foreach {_ =>
      println("closed")
    }
  }
}

此代码将请求发送到 Websocket 服务器端,该服务器端可以与另一个 JS 客户端正常工作。服务器端工作正常,一次发送许多响应,从这个 Wireshark 转储中可以看出

E1@@ÿ   K^B!¹åª&
<·*<·#/{
  "response": "aaaa",
  "status": "success"
}/{
  "response": "aaaa",
  "status": "success"
}/{
  "response": "aaaa",
  "status": "success"
}/...

但是打印“foo”,这是在 Sink 中处理服务器消息的副作用,每 5 秒才发生一次,这与在 Wireshark 中观察到的相比非常慢。

我将如何解释/调试这个?会不会和缓冲/背压有关?

【问题讨论】:

    标签: scala akka akka-http


    【解决方案1】:

    好的,有了这个 Sink,一切都很好:

        val printSink: Sink[Message, Future[Done]] =
          Sink.foreachAsync(1) {
            case x: TextMessage => x.toStrict(FiniteDuration(100, "ms")).map { _ =>
              logger.debug("foo")
              receive = System.currentTimeMillis() :: receive
            }
          }
    

    事实证明,无论何时使用 akka - 总是消耗所有输入。这个 WS 客户端 API 实际上对我来说似乎很奇怪,因为对于这种情况,对于 http 客户端,您会获得一个像 discardResponseBytes 这样的辅助方法。无论如何——总是消耗输入

    【讨论】:

    • 我以后如何调试这个?有什么方法可以记录缓冲区、消耗或反向传播的状态?
    • log 阶段可用于跟踪沿蒸汽的流动,但它不提供有关缓冲区和背压状态的任何信息。
    猜你喜欢
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2011-03-05
    • 2016-09-08
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多