【发布时间】: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