The following experiment registers for termination of the internal actor 
used by the ask pattern. 

The Termination message is received when the route completes normally or 
times out. Closing the connection from the client has no impact on the 
internal actor, which survives until the timeout finally expires. 

This is sufficient to avoid a leak of the resources used by the internal 
actor.

However, it means that the actor (Tracker) that is actually responding to 
the HTML request is unaware that the response cannot be delivered. That is 
unfortunate for use cases with expensive response processing and lengthy 
timeouts to cover the worst case.


import java.util.concurrent.TimeUnit

import akka.actor.{Actor, Props, Terminated}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.util.Timeout

object DeathWatcher extends App {

  import akka.actor.ActorSystem
  import akka.http.scaladsl.Http
  import akka.http.scaladsl.model._
  import akka.stream.ActorMaterializer

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  implicit val executionContext = system.dispatcher

  class Tracker extends Actor {
    override def receive: Receive = {
      case Terminated(c) =>
        println("terminated", c)
      case _: String =>
        context.watch(sender)
    }
  }

  val tracker = system.actorOf(Props[Tracker])

  import akka.pattern.ask

  implicit val timeout = Timeout(5, TimeUnit.SECONDS)

  val route: Route =
    path("success") {
      onComplete(tracker ? "xxx") { r =>
        complete(StatusCodes.OK)
      }
    }

  Http().bindAndHandle(route, "localhost", 8080)
}








-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to