gpt4 book ai didi

scala - Akka-http 在一个流中处理来自不同连接的 HttpRequests

转载 作者:行者123 更新时间:2023-12-01 06:09:27 26 4
gpt4 key购买 nike

Akka-http documentation说:

Apart from regarding a socket bound on the server-side as a Source[IncomingConnection] and each connection as a Source[HttpRequest] with a Sink[HttpResponse]



假设我们得到了包含来自许多 Source[IncomingConnection] 的传入连接的合并源。

然后,假设我们从 Source[IncomingConnection] 得到 Source[HttpRequest](见下面的代码)。

那么,没问题,我们可以提供一个流程来将 HttpRequest 转换为 HttpResponse。

这就是问题 - 我们如何正确接收响应?我们如何加入对连接的响应?

用例背后的整个想法是可以优先处理来自不同连接的传入请求。我想在很多情况下应该很有用......

提前致谢!

编辑:
基于@RamonJRomeroyVigil 回答的解决方案:

服务器代码:
val in1 = Http().bind(interface = "localhost", port = 8200)
val in2 = Http().bind(interface = "localhost", port = 8201)

val connSrc = Source.fromGraph(FlowGraph.create() { implicit b =>
import FlowGraph.Implicits._

val merge = b.add(Merge[IncomingConnection](2))

in1 ~> print("in1") ~> merge.in(0)
in2 ~> print("in2") ~> merge.in(1)

SourceShape(merge.out)
})

val reqSrc : Source[(HttpRequest, IncomingConnection), _] =
connSrc.flatMapConcat { conn =>
Source.empty[HttpResponse]
.via(conn.flow)
.map(request => (request, conn))
}

val flow: Flow[(HttpRequest, IncomingConnection), (HttpResponse, IncomingConnection), _] =
Flow[(HttpRequest, IncomingConnection)].map{
case (HttpRequest(HttpMethods.GET, Uri.Path("/ping"), _, entity, _), conn: IncomingConnection) =>
println(s"${System.currentTimeMillis()}: " +
s"process request from ${conn.remoteAddress.getHostName}:${conn.remoteAddress.getPort}")
(HttpResponse(entity = "pong"), conn)
}

reqSrc.via(flow).to(Sink.foreach { case (resp, conn) =>
Source.single(resp).via(conn.flow).runWith(Sink.ignore)
}).run()

def print(prefix: String) = Flow[IncomingConnection].map { s =>
println(s"$prefix [ ${System.currentTimeMillis()} ]: ${s.remoteAddress}"); s
}

所以,我从控制台使用 curl 并看到以下内容:
% curl http://localhost:8200/ping
curl: (52) Empty reply from server

第二个 curl 请求失败:
% curl http://localhost:8200/ping
curl: (7) Failed to connect to localhost port 8200: Connection refused

在服务器控制台上,我在发送第一个请求时看到以下内容:
in1 [ 1450287301512 ]: /127.0.0.1:52461
1450287301626: process request from localhost:52461
[INFO] [12/16/2015 20:35:01.641] [default-akka.actor.default-dispatcher-6] [akka://default/system/IO-TCP-STREAM/server-1-localhost%2F127.0.0.1%3A8200] Message [akka.io.Tcp$Unbound$] from Actor[akka://default/system/IO-TCP/selectors/$a/0#119537130] to Actor[akka://default/system/IO-TCP-STREAM/server-1-localhost%2F127.0.0.1%3A8200#-1438663077] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [12/16/2015 20:35:01.641] [default-akka.actor.default-dispatcher-6] [akka://default/system/IO-TCP-STREAM/server-2-localhost%2F127.0.0.1%3A8201] Message [akka.io.Tcp$Unbound$] from Actor[akka://default/system/IO-TCP/selectors/$a/1#679898594] to Actor[akka://default/system/IO-TCP-STREAM/server-2-localhost%2F127.0.0.1%3A8201#1414174163] was not delivered. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

发送第二个请求时什么也没有。

因此,内部连接流(如@RamonJRomeroyVigil 所述)或其他问题似乎存在一些问题...

基本上代码不起作用。

还在排查问题。

最佳答案

以下解决方案基于问题评论中提供的更多信息。

给定的

val connSrc : Source[IncomingConnection,_] = ???
flatMapConcat方法解决了所述的具体问题:
val reqSrc : Source[(HttpRequest, IncomingConnection), _] =
connSrc.flatMapConcat { conn =>
Source.empty[HttpResponse]
.via(conn.flow)
.map(request => (request, conn))
}

这提供了 (HttpRequest, IncomingConnection) 的来源元组。

假设您有一个将请求转换为响应的处理步骤
val flow : Flow[(HttpRequest, IncomingConnection), (HttpResponse, IncomingConnection), _] = ???

您可以将响应发送回客户端:
reqSrc.via(flow).to(Sink.foreach { case (resp, conn) =>
Source.single(resp).via(conn.flow).runWith(Sink.ignore)
})

警告:此解决方案调用 conn.flow两次:一次创建一个生成请求的流,再次创建一个发送响应的流。我不知道这种类型的用例是否会破坏 IncomingConnection 中的某些内容。逻辑。

关于scala - Akka-http 在一个流中处理来自不同连接的 HttpRequests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com