我有一个问题。
我有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()
您只需通过cURL发出请求并立即将其取消,您仍然会在控制台中看到“答复”。
谢谢