我有个问题。
我有 Akka HTTP Server 并使用 Akka HTTP Client。客户端基本上用 Http.singleRequest() 做了很多外部调用。这需要很多时间,有时用户会关闭与我的服务器的连接。
所以问题是 - 我如何检测用户已关闭连接?
server.scala:
val requestHandler: HttpRequest => HttpResponse = {
case HttpRequest(HttpMethods.GET, Uri.Path("/"), _, _, _) =>
Thread.sleep(5000)
println("reply")
HttpResponse(entity = HttpEntity(
ContentTypes.`text/html(UTF-8)`,
"Ok"))
}
val serverBinding: Future[Http.ServerBinding] = Http().bind(host, port)
.via(reactToTopLevelFailures)
.to(Sink.foreach { connection: IncomingConnection =>
println("Accepted new connection from " + connection.remoteAddress)
connection.handleWith(Flow[HttpRequest] map requestHandler)
})
.run()
Run Code Online (Sandbox Code Playgroud)
您可以简单地通过 cURL 发出请求并立即取消它,您仍然会在控制台中看到“回复”。
谢谢