7

I have a question.

I have Akka HTTP Server and using Akka HTTP Client. The Client does a lot of external calls basically with Http.singleRequest(). This takes a lot of time and sometimes User closes the connection to my Server.

So the question is - how can I detect that User has closed the connection ?

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()

You can simply do a request via cURL and cancel it immediately you will still see "reply" in the console.

Thanks

  • TCP connections and HTTP requests a concerns of different abstraction levels. Normally an http handler shouldn't care about connection's state. If it does - probably the design should be reconsidered. What problem are you solving? – simpadjo Sep 29 '19 at 07:53
  • @simpadjo actually stopping Source processing with killswitch when user disconnect (reloads/refreshes the page, etc) – Maxim Dvoryanchenko Sep 29 '19 at 17:05
  • I'm doing a lot of computations using Akka Streams and I want to stop doing that when I don't need the result anymore. – Maxim Dvoryanchenko Sep 29 '19 at 17:31
  • Websockets could be a solution. – simpadjo Sep 29 '19 at 18:53
  • that is true, but i can't use websockets ( I was thinking about creating the distributed actor system (because I have 3 workers with the same code running) and detect if I get the same request. In this case it will be possible to terminate the running stream. – Maxim Dvoryanchenko Sep 29 '19 at 19:14

0 Answers0