Anand
Anand

Reputation: 551

What happens in Future.pipeTo within actor

I have a call to 3rd party library(which returs a future) within my actor receive. I am piping that future to Self, so that I can process the reply as a messsage in the mailbox.

class MyActor{
  def receive:Receive = {
    case x:Message => {
      val future = callToThridPartyLib(x)
      future pipeTo self
    }
    case x:Reply => {
      //process reply received on future completion
    }
  }
}

My questions are

  1. What is the overhead of this approach?

  2. Will there be a separate thread allocated for each such pipeTo statement?

  3. If so, should there be a Thread pool to manage pipeTo to avoid all threads getting used up?

Thank you

Upvotes: 0

Views: 609

Answers (1)

Haspemulator
Haspemulator

Reputation: 11308

What Victor says, but essentially: The overhead of pipeTo is very low: it creates one closure, which sends one message. That's it, no additional threading involved. The message is being sent from the thread on which the Future has been completed, and will be received, like all the actor messages, on the dispatcher that has been configured for the given actor.

You should take care of ExecutionContext for that callToThirdPartyLib though. If it uses blocking calls inside, and you use default Akka dispatcher to run it (what your code suggests), you might run out of threads quickly. However, this seems to be off-topic for the question.

Upvotes: 4

Related Questions