【问题标题】:When to materialize content of http response in akka-http?什么时候在akka-http中实现http响应的内容?
【发布时间】:2018-08-13 08:42:32
【问题描述】:

让我们想象一下基于 akka-streams 和 akka-http 的代理应用程序,它以某种本地格式接收(作为 TCP 服务器)消息,从它们发出 http 请求,询问其他 http 服务器,将 http 响应转换回 home-成长格式并回复给客户。简化代码如下:

// as Client part 
val connPool = Http().cachedHostConnectionPool[CustHttpReq](someHost, somePort)

val asClientFlow = Flow[CustHttpReq]
  .via (connPool)
  .map (procHttpResp)

def procHttpResp (p: (Try[HttpResponse], CustHttpReq)): Future[ByteString] = {
  val (rsp, src) = p
  rsp match {
    case Success(response: HttpResponse) =>
      for (buf <- cvtToHomeGrown (response, src)) 
          yield buf
    case Failure(ex) => ... 
  }
}

def cvtToHomeGrown (rsp: HttpResponse): Future[ByteString] = {
  rsp.entity.dataBytes.runWith (Sink.fold (ByteString.empty)(_ ++ _))
    .map (cvtToHomeGrownActually) // has signature String => ByteString
}

// as Server part
val parseAndAskFlow = Flow[ByteString]
  .via(Framing.delimiter(
    ByteString('\n'))
  .map (buf => cvtToCustHttpReq (buf))
  .via (asClientFlow)  // plug-in asClient part, the problem is here

val asServerConn: Source[IncomingConnection, Future[ServerBinding]] = Tcp().bind("localhost",port)

asServerConn.runForeach (conn => conn.handleWith(parseAndAskFlow)

问题是conn.handleWith需要Flow[ByteString,ByteString,],但是http客户端代码(rsp.entity.dataBytes...)返回Future[ByteSring],所以parseAndAskFlow有Flow[ByteString,Future[ByteString] ,] 类型,我不知道在哪里可以更好地完成它。我什至认为这根本不是一个好主意,因为所有这些都是流并且 Await somethere 会停止好的异步处理,但代码不会被编译。

【问题讨论】:

    标签: scala akka akka-stream akka-http


    【解决方案1】:

    使用mapAsync而不是mapasClientFlow的类型改为Flow[CustHttpReq, ByteString]

    val asClientFlow: Flow[CustHttpReq, ByteString] =
      Flow[CustHttpReq]
        .via(connPool)
        .mapAsync(1)(procHttpResp)
    

    那么parseAndAskFlow可以是Flow[ByteString, ByteString]类型:

    val parseAndAskFlow: Flow[ByteString, ByteString] =
      Flow[ByteString]
        .via(Framing.delimiter(ByteString("\n"))
        .map(cvtToCustHttpReq)
        .via(asClientFlow)
    

    【讨论】:

    • 宾果游戏!你让我今天一整天都感觉很好。通过了又一轮流学习螺旋。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2023-03-08
    • 2016-01-21
    相关资源
    最近更新 更多