title: Puzzled by the spawned actor from a Spray route
body: <p>I am doing some Http request processing using Spray. For a request I spin up an actor and send the payload to the actor for processing and after the actor is done working on the payload, I call <code>context.stop(self)</code> on the actor to wind the actor down.The idea is to prevent oversaturation of actors on the physical machine.</p>
  
  <p>This is how I have things set up..</p>
  
  <p>In <code>httphandler.scala</code>, I have the route set up as follows:</p>
  
  <pre><code>path("users"){
     get{
        requestContext =&gt; {
           val userWorker = actorRefFactory.actorOf(Props(new UserWorker(userservice,requestContext)))
          userWorker ! getusers //get user is a case object 
        }
     }
  } ~ path("users"){
       post{
         entity(as[UserInfo]){
            requestContext =&gt; {
            userInfo =&gt; {
               val userWorker = actorRefFactory.actorOf(Props(new UserWorker(userservice,requestContext)))
               userWorker ! userInfo
             }
           }
         }
       }
   }
  </code></pre>
  
  <p>My <code>UserWorker</code> actor is defined as follows:</p>
  
  <pre><code>trait RouteServiceActor extends Actor{
    implicit val system = context.system
    import system.dispatcher
    def processRequest[responseModel:ToResponseMarshaller](requestContex:RequestContext)(processFunc: =&gt; responseModel):Unit = {
         Future{
           processFunc
         } onComplete {
            case Success(result) =&gt; {
              requestContext.complete(result)
            }
            case Failure(error) =&gt; requestContext.complete(error)
         }
    }
  }
  
  class UserWorker(userservice: UserServiceComponent#UserService,requestContext:RequestContext) extends RouteServiceActor{
   def receive = {
      case getusers =&gt; processRequest(requestContext){
           userservice.getAllUsers
      }
      context.stop(self)
    }
    case userInfo:UserInfo =&gt; {
      processRequest(requestContext){
           userservice.createUser(userInfo)
      }
      context.stop(self) 
   }  
  
  }
  </code></pre>
  
  <p>My first question is, am I handling the request in a true asynchronous fashion? What are some of the pitfalls with my code?</p>
  
  <p>My second question is how does the <code>requestContext.complete</code> work? Since the original request processing thread is no longer there, how does the <code>requestContext</code> send the result of the computation back to the client.</p>
  
  <p>My third question is that since I am calling <code>context.stop(self)</code> after each of my partial methods, is it possible that I terminate the worker while it is in the midst of processing a different message.</p>
  
  <p>What I mean is that while the Actor receives a message to process <code>getusers</code>, the same actor is done processing <code>UserInfo</code> and terminates the Actor before it can get to the "getusers" message. I am creating new actors upon every request, but is it possible that under the covers, the <code>actorRefFactory</code> provides a reference to a previously created actor, instead of a new one?</p>
  
  <p>I am pretty confused by all the abstractions and it would be great if somebody could break it down for me.</p>
  
  <p>Thanks</p>