2018-10-29 17:19:37 +08:00
|
|
|
|
/*
|
2021-01-08 17:55:38 +01:00
|
|
|
|
* Copyright (C) 2009-2021 Lightbend Inc. <https://www.lightbend.com>
|
2012-01-18 11:52:35 +01:00
|
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
|
2012-01-18 11:52:35 +01:00
|
|
|
|
package akka.pattern
|
|
|
|
|
|
|
2020-07-08 17:43:52 +02:00
|
|
|
|
import java.net.URLEncoder
|
2012-01-18 11:52:35 +01:00
|
|
|
|
import java.util.concurrent.TimeoutException
|
2015-03-30 16:17:12 +02:00
|
|
|
|
|
2020-04-27 20:32:18 +08:00
|
|
|
|
import scala.annotation.tailrec
|
|
|
|
|
|
import scala.concurrent.{ Future, Promise }
|
|
|
|
|
|
import scala.language.implicitConversions
|
|
|
|
|
|
import scala.util.{ Failure, Success }
|
2021-02-01 15:38:29 +00:00
|
|
|
|
import scala.annotation.nowarn
|
|
|
|
|
|
import scala.util.control.NoStackTrace
|
|
|
|
|
|
|
2012-03-24 23:02:37 +01:00
|
|
|
|
import akka.actor._
|
2020-05-05 11:13:21 -04:00
|
|
|
|
import akka.annotation.{ InternalApi, InternalStableApi }
|
2020-03-10 15:39:30 +01:00
|
|
|
|
import akka.dispatch.ExecutionContexts
|
2013-03-05 16:19:54 +01:00
|
|
|
|
import akka.dispatch.sysmsg._
|
2012-07-22 15:33:18 +02:00
|
|
|
|
import akka.util.{ Timeout, Unsafe }
|
2021-02-01 15:38:29 +00:00
|
|
|
|
import akka.util.ByteString
|
2020-05-05 11:13:21 -04:00
|
|
|
|
import akka.util.unused
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This is what is used to complete a Future that is returned from an ask/? call,
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* when it times out. A typical reason for `AskTimeoutException` is that the recipient
|
|
|
|
|
|
* actor didn't send a reply.
|
2012-01-18 11:52:35 +01:00
|
|
|
|
*/
|
2020-07-13 13:37:28 +02:00
|
|
|
|
class AskTimeoutException(message: String, cause: Throwable) extends TimeoutException(message) with NoStackTrace {
|
2012-01-18 11:52:35 +01:00
|
|
|
|
def this(message: String) = this(message, null: Throwable)
|
2012-03-06 14:56:34 +01:00
|
|
|
|
override def getCause(): Throwable = cause
|
2012-01-18 11:52:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-23 18:23:34 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* This object contains implementation details of the “ask” pattern.
|
|
|
|
|
|
*/
|
2012-02-01 13:37:57 +01:00
|
|
|
|
trait AskSupport {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain `?` and `ask` methods on
|
|
|
|
|
|
* [[akka.actor.ActorRef]], which will defer to the
|
|
|
|
|
|
* `ask(actorRef, message)(timeout)` method defined here.
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.ask
|
|
|
|
|
|
*
|
|
|
|
|
|
* val future = actor ? message // => ask(actor, message)
|
|
|
|
|
|
* val future = actor ask message // => ask(actor, message)
|
|
|
|
|
|
* val future = actor.ask(message)(timeout) // => ask(actor, message)(timeout)
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
2013-07-05 12:46:39 +02:00
|
|
|
|
* All of the above use an implicit [[akka.util.Timeout]].
|
2012-02-01 13:37:57 +01:00
|
|
|
|
*/
|
|
|
|
|
|
implicit def ask(actorRef: ActorRef): AskableActorRef = new AskableActorRef(actorRef)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-07-04 15:25:30 +02:00
|
|
|
|
* Sends a message asynchronously and returns a [[scala.concurrent.Future]]
|
2012-02-01 13:37:57 +01:00
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* needs to send the result to the `sender` reference provided.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The Future will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2012-02-01 13:37:57 +01:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* `Await.result(..., timeout)`). A typical reason for `AskTimeoutException` is that the
|
|
|
|
|
|
* recipient actor didn't send a reply.
|
2012-02-01 13:37:57 +01:00
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* val f = ask(worker, request)(timeout)
|
2013-12-11 12:57:31 +01:00
|
|
|
|
* f.map { response =>
|
|
|
|
|
|
* EnrichedMessage(response)
|
2012-02-01 13:37:57 +01:00
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2015-04-30 09:23:18 +02:00
|
|
|
|
def ask(actorRef: ActorRef, message: Any)(implicit timeout: Timeout): Future[Any] =
|
|
|
|
|
|
actorRef.internalAsk(message, timeout, ActorRef.noSender)
|
2014-11-27 11:54:02 +03:00
|
|
|
|
def ask(actorRef: ActorRef, message: Any, sender: ActorRef)(implicit timeout: Timeout): Future[Any] =
|
2015-04-30 09:23:18 +02:00
|
|
|
|
actorRef.internalAsk(message, timeout, sender)
|
2013-04-29 22:01:14 +02:00
|
|
|
|
|
2020-07-09 16:57:53 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Use for messages whose response is known to be a [[akka.pattern.StatusReply]]. When a [[akka.pattern.StatusReply.Success]] response
|
|
|
|
|
|
* arrives the future is completed with the wrapped value, if a [[akka.pattern.StatusReply.Error]] arrives the future is instead
|
|
|
|
|
|
* failed.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def askWithStatus(actorRef: ActorRef, message: Any)(implicit timeout: Timeout): Future[Any] =
|
|
|
|
|
|
actorRef.internalAskWithStatus(message)(timeout, Actor.noSender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Use for messages whose response is known to be a [[akka.pattern.StatusReply]]. When a [[akka.pattern.StatusReply.Success]] response
|
|
|
|
|
|
* arrives the future is completed with the wrapped value, if a [[akka.pattern.StatusReply.Error]] arrives the future is instead
|
|
|
|
|
|
* failed.
|
|
|
|
|
|
*/
|
|
|
|
|
|
def askWithStatus(actorRef: ActorRef, message: Any, sender: ActorRef)(implicit timeout: Timeout): Future[Any] =
|
|
|
|
|
|
actorRef.internalAskWithStatus(message)(timeout, sender)
|
|
|
|
|
|
|
2013-04-29 22:01:14 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain `?` and `ask` methods on
|
|
|
|
|
|
* [[akka.actor.ActorSelection]], which will defer to the
|
|
|
|
|
|
* `ask(actorSelection, message)(timeout)` method defined here.
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.ask
|
|
|
|
|
|
*
|
|
|
|
|
|
* val future = selection ? message // => ask(selection, message)
|
|
|
|
|
|
* val future = selection ask message // => ask(selection, message)
|
|
|
|
|
|
* val future = selection.ask(message)(timeout) // => ask(selection, message)(timeout)
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
2013-07-05 12:46:39 +02:00
|
|
|
|
* All of the above use an implicit [[akka.util.Timeout]].
|
2013-04-29 22:01:14 +02:00
|
|
|
|
*/
|
|
|
|
|
|
implicit def ask(actorSelection: ActorSelection): AskableActorSelection = new AskableActorSelection(actorSelection)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sends a message asynchronously and returns a [[scala.concurrent.Future]]
|
|
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* needs to send the result to the `sender` reference provided.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The Future will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2013-04-29 22:01:14 +02:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* `Await.result(..., timeout)`). A typical reason for `AskTimeoutException` is that the
|
|
|
|
|
|
* recipient actor didn't send a reply.
|
2013-04-29 22:01:14 +02:00
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
2013-12-11 12:57:31 +01:00
|
|
|
|
* val f = ask(worker, request)(timeout)
|
|
|
|
|
|
* f.map { response =>
|
|
|
|
|
|
* EnrichedMessage(response)
|
2013-04-29 22:01:14 +02:00
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2015-04-30 09:23:18 +02:00
|
|
|
|
def ask(actorSelection: ActorSelection, message: Any)(implicit timeout: Timeout): Future[Any] =
|
|
|
|
|
|
actorSelection.internalAsk(message, timeout, ActorRef.noSender)
|
2014-11-27 11:54:02 +03:00
|
|
|
|
def ask(actorSelection: ActorSelection, message: Any, sender: ActorRef)(implicit timeout: Timeout): Future[Any] =
|
2015-04-30 09:23:18 +02:00
|
|
|
|
actorSelection.internalAsk(message, timeout, sender)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-25 14:38:10 +03:00
|
|
|
|
/**
|
|
|
|
|
|
* This object contains implementation details of the “ask” pattern,
|
|
|
|
|
|
* which can be combined with "replyTo" pattern.
|
|
|
|
|
|
*/
|
|
|
|
|
|
trait ExplicitAskSupport {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain `?` and `ask` methods on
|
|
|
|
|
|
* [[akka.actor.ActorRef]], which will defer to the
|
|
|
|
|
|
* `ask(actorRef, askSender => message)(timeout)` method defined here.
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.ask
|
|
|
|
|
|
*
|
|
|
|
|
|
* // same as `ask(actor, askSender => Request(askSender))`
|
|
|
|
|
|
* val future = actor ? { askSender => Request(askSender) }
|
|
|
|
|
|
*
|
|
|
|
|
|
* // same as `ask(actor, Request(_))`
|
|
|
|
|
|
* val future = actor ? (Request(_))
|
|
|
|
|
|
*
|
|
|
|
|
|
* // same as `ask(actor, Request(_))(timeout)`
|
|
|
|
|
|
* val future = actor ? (Request(_))(timeout)
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* All of the above use a required implicit [[akka.util.Timeout]] and optional implicit
|
|
|
|
|
|
* sender [[akka.actor.ActorRef]].
|
|
|
|
|
|
*/
|
|
|
|
|
|
implicit def ask(actorRef: ActorRef): ExplicitlyAskableActorRef = new ExplicitlyAskableActorRef(actorRef)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sends a message asynchronously and returns a [[scala.concurrent.Future]]
|
|
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* needs to send the result to the `sender` reference provided.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The Future will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2015-10-25 14:38:10 +03:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* `Await.result(..., timeout)`). A typical reason for `AskTimeoutException` is that the
|
|
|
|
|
|
* recipient actor didn't send a reply.
|
2015-10-25 14:38:10 +03:00
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* val f = ask(worker, replyTo => Request(replyTo))(timeout)
|
|
|
|
|
|
* f.map { response =>
|
|
|
|
|
|
* EnrichedMessage(response)
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
|
def ask(actorRef: ActorRef, messageFactory: ActorRef => Any)(implicit timeout: Timeout): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
actorRef.internalAsk(messageFactory, timeout, ActorRef.noSender)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
def ask(actorRef: ActorRef, messageFactory: ActorRef => Any, sender: ActorRef)(
|
|
|
|
|
|
implicit timeout: Timeout): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
actorRef.internalAsk(messageFactory, timeout, sender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain `?` and `ask` methods on
|
|
|
|
|
|
* [[akka.actor.ActorSelection]], which will defer to the
|
|
|
|
|
|
* `ask(actorSelection, message)(timeout)` method defined here.
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.ask
|
|
|
|
|
|
*
|
|
|
|
|
|
* // same as `ask(selection, askSender => Request(askSender))`
|
|
|
|
|
|
* val future = selection ? { askSender => Request(askSender) }
|
|
|
|
|
|
*
|
|
|
|
|
|
* // same as `ask(selection, Request(_))`
|
|
|
|
|
|
* val future = selection ? (Request(_))
|
|
|
|
|
|
*
|
|
|
|
|
|
* // same as `ask(selection, Request(_))(timeout)`
|
|
|
|
|
|
* val future = selection ? (Request(_))(timeout)
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* All of the above use a required implicit [[akka.util.Timeout]] and optional implicit
|
|
|
|
|
|
* sender [[akka.actor.ActorRef]].
|
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
|
implicit def ask(actorSelection: ActorSelection): ExplicitlyAskableActorSelection =
|
|
|
|
|
|
new ExplicitlyAskableActorSelection(actorSelection)
|
2015-10-25 14:38:10 +03:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sends a message asynchronously and returns a [[scala.concurrent.Future]]
|
|
|
|
|
|
* holding the eventual reply message; this means that the target actor
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* needs to send the result to the `sender` reference provided.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The Future will be completed with an [[akka.pattern.AskTimeoutException]] after the
|
2015-10-25 14:38:10 +03:00
|
|
|
|
* given timeout has expired; this is independent from any timeout applied
|
|
|
|
|
|
* while awaiting a result for this future (i.e. in
|
2018-09-19 17:38:17 +02:00
|
|
|
|
* `Await.result(..., timeout)`). A typical reason for `AskTimeoutException` is that the
|
|
|
|
|
|
* recipient actor didn't send a reply.
|
2015-10-25 14:38:10 +03:00
|
|
|
|
*
|
|
|
|
|
|
* <b>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s object, i.e. do not call methods or access mutable state
|
|
|
|
|
|
* on the enclosing actor from within the callback. This would break the actor
|
|
|
|
|
|
* encapsulation and may introduce synchronization bugs and race conditions because
|
|
|
|
|
|
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
|
|
|
|
|
|
* there is not yet a way to detect these illegal accesses at compile time.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* val f = ask(worker, replyTo => Request(replyTo))(timeout)
|
|
|
|
|
|
* f.map { response =>
|
|
|
|
|
|
* EnrichedMessage(response)
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2019-02-09 15:25:39 +01:00
|
|
|
|
def ask(actorSelection: ActorSelection, messageFactory: ActorRef => Any)(implicit timeout: Timeout): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
actorSelection.internalAsk(messageFactory, timeout, ActorRef.noSender)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
def ask(actorSelection: ActorSelection, messageFactory: ActorRef => Any, sender: ActorRef)(
|
|
|
|
|
|
implicit timeout: Timeout): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
actorSelection.internalAsk(messageFactory, timeout, sender)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
|
object AskableActorRef {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
|
2018-09-19 17:38:17 +02:00
|
|
|
|
private def messagePartOfException(message: Any, sender: ActorRef): String = {
|
|
|
|
|
|
val msg = if (message == null) "unknown" else message
|
|
|
|
|
|
val wasSentBy = if (sender == ActorRef.noSender) "" else s" was sent by [$sender]"
|
|
|
|
|
|
s"Message of type [${msg.getClass.getName}]$wasSentBy."
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
|
@InternalApi private[akka] def negativeTimeoutException(
|
|
|
|
|
|
recipient: Any,
|
|
|
|
|
|
message: Any,
|
|
|
|
|
|
sender: ActorRef): IllegalArgumentException = {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
new IllegalArgumentException(
|
|
|
|
|
|
s"Timeout length must be positive, question not sent to [$recipient]. " +
|
2018-09-19 17:38:17 +02:00
|
|
|
|
messagePartOfException(message, sender))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
|
@InternalApi private[akka] def recipientTerminatedException(
|
|
|
|
|
|
recipient: Any,
|
|
|
|
|
|
message: Any,
|
|
|
|
|
|
sender: ActorRef): AskTimeoutException = {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
new AskTimeoutException(
|
|
|
|
|
|
s"Recipient [$recipient] had already been terminated. " +
|
2018-09-19 17:38:17 +02:00
|
|
|
|
messagePartOfException(message, sender))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
|
@InternalApi private[akka] def unsupportedRecipientType(
|
|
|
|
|
|
recipient: Any,
|
|
|
|
|
|
message: Any,
|
|
|
|
|
|
sender: ActorRef): IllegalArgumentException = {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
new IllegalArgumentException(
|
|
|
|
|
|
s"Unsupported recipient type, question not sent to [$recipient]. " +
|
2018-09-19 17:38:17 +02:00
|
|
|
|
messagePartOfException(message, sender))
|
|
|
|
|
|
}
|
2012-12-17 14:22:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Implementation class of the “ask” pattern enrichment of ActorRef
|
|
|
|
|
|
*/
|
|
|
|
|
|
final class AskableActorRef(val actorRef: ActorRef) extends AnyVal {
|
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected def ask(message: Any, timeout: Timeout): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, ActorRef.noSender)
|
|
|
|
|
|
|
|
|
|
|
|
def ask(message: Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
2020-07-09 16:57:53 +02:00
|
|
|
|
def askWithStatus(message: Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
|
|
|
|
|
internalAskWithStatus(message)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
|
|
|
|
|
@InternalApi
|
|
|
|
|
|
private[pattern] def internalAskWithStatus(
|
|
|
|
|
|
message: Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
|
|
|
|
|
StatusReply.flattenStatusFuture[Any](internalAsk(message, timeout, sender).mapTo[StatusReply[Any]])
|
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected def ?(message: Any)(implicit timeout: Timeout): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, ActorRef.noSender)
|
|
|
|
|
|
|
|
|
|
|
|
def ?(message: Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
|
|
|
|
|
private[pattern] def internalAsk(message: Any, timeout: Timeout, sender: ActorRef) = actorRef match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case ref: InternalActorRef if ref.isTerminated =>
|
2012-09-19 23:55:53 +02:00
|
|
|
|
actorRef ! message
|
2019-02-11 17:25:05 +01:00
|
|
|
|
Future.failed[Any](AskableActorRef.recipientTerminatedException(actorRef, message, sender))
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case ref: InternalActorRef =>
|
2013-04-29 22:01:14 +02:00
|
|
|
|
if (timeout.duration.length <= 0)
|
2018-09-19 17:38:17 +02:00
|
|
|
|
Future.failed[Any](AskableActorRef.negativeTimeoutException(actorRef, message, sender))
|
2012-07-23 13:52:48 +02:00
|
|
|
|
else {
|
2020-07-08 17:43:52 +02:00
|
|
|
|
PromiseActorRef(ref.provider, timeout, targetName = actorRef, message.getClass.getName, ref.path.name, sender)
|
2020-05-05 11:13:21 -04:00
|
|
|
|
.ask(actorRef, message, timeout)
|
2012-02-01 13:37:57 +01:00
|
|
|
|
}
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case _ => Future.failed[Any](AskableActorRef.unsupportedRecipientType(actorRef, message, sender))
|
2013-04-29 22:01:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-25 14:38:10 +03:00
|
|
|
|
/*
|
|
|
|
|
|
* Implementation class of the “ask” with explicit sender pattern enrichment of ActorRef
|
|
|
|
|
|
*/
|
|
|
|
|
|
final class ExplicitlyAskableActorRef(val actorRef: ActorRef) extends AnyVal {
|
|
|
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
|
def ask(message: ActorRef => Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
|
def ?(message: ActorRef => Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
|
private[pattern] def internalAsk(messageFactory: ActorRef => Any, timeout: Timeout, sender: ActorRef): Future[Any] =
|
|
|
|
|
|
actorRef match {
|
|
|
|
|
|
case ref: InternalActorRef if ref.isTerminated =>
|
2015-10-25 14:38:10 +03:00
|
|
|
|
val message = messageFactory(ref.provider.deadLetters)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
actorRef ! message
|
|
|
|
|
|
Future.failed[Any](AskableActorRef.recipientTerminatedException(actorRef, message, sender))
|
|
|
|
|
|
case ref: InternalActorRef =>
|
|
|
|
|
|
if (timeout.duration.length <= 0) {
|
|
|
|
|
|
val message = messageFactory(ref.provider.deadLetters)
|
|
|
|
|
|
Future.failed[Any](AskableActorRef.negativeTimeoutException(actorRef, message, sender))
|
|
|
|
|
|
} else {
|
2020-07-08 17:43:52 +02:00
|
|
|
|
val a = PromiseActorRef(ref.provider, timeout, targetName = actorRef, "unknown", ref.path.name, sender)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
val message = messageFactory(a)
|
|
|
|
|
|
a.messageClassName = message.getClass.getName
|
2020-05-05 11:13:21 -04:00
|
|
|
|
a.ask(actorRef, message, timeout)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
case _ if sender eq null =>
|
|
|
|
|
|
Future.failed[Any](
|
|
|
|
|
|
new IllegalArgumentException(
|
|
|
|
|
|
"No recipient for the reply was provided, " +
|
|
|
|
|
|
s"question not sent to [$actorRef]."))
|
|
|
|
|
|
case _ =>
|
|
|
|
|
|
val message =
|
|
|
|
|
|
if (sender == null) null else messageFactory(sender.asInstanceOf[InternalActorRef].provider.deadLetters)
|
|
|
|
|
|
Future.failed[Any](AskableActorRef.unsupportedRecipientType(actorRef, message, sender))
|
|
|
|
|
|
}
|
2015-10-25 14:38:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-29 22:01:14 +02:00
|
|
|
|
/*
|
|
|
|
|
|
* Implementation class of the “ask” pattern enrichment of ActorSelection
|
|
|
|
|
|
*/
|
|
|
|
|
|
final class AskableActorSelection(val actorSel: ActorSelection) extends AnyVal {
|
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected def ask(message: Any, timeout: Timeout): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, ActorRef.noSender)
|
|
|
|
|
|
|
|
|
|
|
|
def ask(message: Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected def ?(message: Any)(implicit timeout: Timeout): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, ActorRef.noSender)
|
|
|
|
|
|
|
|
|
|
|
|
def ?(message: Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
|
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
|
private[pattern] def internalAsk(message: Any, timeout: Timeout, sender: ActorRef): Future[Any] =
|
|
|
|
|
|
actorSel.anchor match {
|
|
|
|
|
|
case ref: InternalActorRef =>
|
|
|
|
|
|
if (timeout.duration.length <= 0)
|
|
|
|
|
|
Future.failed[Any](AskableActorRef.negativeTimeoutException(actorSel, message, sender))
|
|
|
|
|
|
else {
|
2020-07-08 17:43:52 +02:00
|
|
|
|
val refPrefix = URLEncoder.encode(actorSel.pathString.replace("/", "_"), ByteString.UTF_8)
|
|
|
|
|
|
PromiseActorRef(ref.provider, timeout, targetName = actorSel, message.getClass.getName, refPrefix, sender)
|
2020-05-05 11:13:21 -04:00
|
|
|
|
.ask(actorSel, message, timeout)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
case _ => Future.failed[Any](AskableActorRef.unsupportedRecipientType(actorSel, message, sender))
|
|
|
|
|
|
}
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-25 14:38:10 +03:00
|
|
|
|
/*
|
|
|
|
|
|
* Implementation class of the “ask” with explicit sender pattern enrichment of ActorSelection
|
|
|
|
|
|
*/
|
|
|
|
|
|
final class ExplicitlyAskableActorSelection(val actorSel: ActorSelection) extends AnyVal {
|
|
|
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
|
def ask(message: ActorRef => Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
|
def ?(message: ActorRef => Any)(implicit timeout: Timeout, sender: ActorRef = Actor.noSender): Future[Any] =
|
2015-10-25 14:38:10 +03:00
|
|
|
|
internalAsk(message, timeout, sender)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API: for binary compatibility
|
|
|
|
|
|
*/
|
2019-03-11 10:38:24 +01:00
|
|
|
|
private[pattern] def internalAsk(messageFactory: ActorRef => Any, timeout: Timeout, sender: ActorRef): Future[Any] =
|
|
|
|
|
|
actorSel.anchor match {
|
|
|
|
|
|
case ref: InternalActorRef =>
|
|
|
|
|
|
if (timeout.duration.length <= 0) {
|
|
|
|
|
|
val message = messageFactory(ref.provider.deadLetters)
|
|
|
|
|
|
Future.failed[Any](AskableActorRef.negativeTimeoutException(actorSel, message, sender))
|
|
|
|
|
|
} else {
|
2020-07-08 17:43:52 +02:00
|
|
|
|
val refPrefix = URLEncoder.encode(actorSel.pathString.replace("/", "_"), ByteString.UTF_8)
|
|
|
|
|
|
val a = PromiseActorRef(ref.provider, timeout, targetName = actorSel, "unknown", refPrefix, sender)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
val message = messageFactory(a)
|
|
|
|
|
|
a.messageClassName = message.getClass.getName
|
2020-05-05 11:13:21 -04:00
|
|
|
|
a.ask(actorSel, message, timeout)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
case _ if sender eq null =>
|
|
|
|
|
|
Future.failed[Any](
|
|
|
|
|
|
new IllegalArgumentException(
|
|
|
|
|
|
"No recipient for the reply was provided, " +
|
|
|
|
|
|
s"question not sent to [$actorSel]."))
|
|
|
|
|
|
case _ =>
|
|
|
|
|
|
val message =
|
|
|
|
|
|
if (sender == null) null else messageFactory(sender.asInstanceOf[InternalActorRef].provider.deadLetters)
|
|
|
|
|
|
Future.failed[Any](AskableActorRef.unsupportedRecipientType(actorSel, message, sender))
|
|
|
|
|
|
}
|
2015-10-25 14:38:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Akka private optimized representation of the temporary actor spawned to
|
|
|
|
|
|
* receive the reply to an "ask" operation.
|
2012-05-18 16:41:19 +02:00
|
|
|
|
*
|
|
|
|
|
|
* INTERNAL API
|
2012-03-23 21:35:52 +01:00
|
|
|
|
*/
|
2019-03-13 10:56:20 +01:00
|
|
|
|
private[akka] final class PromiseActorRef private (
|
|
|
|
|
|
val provider: ActorRefProvider,
|
|
|
|
|
|
val result: Promise[Any],
|
2020-07-08 17:43:52 +02:00
|
|
|
|
_mcn: String,
|
|
|
|
|
|
refPathPrefix: String)
|
2019-03-11 10:38:24 +01:00
|
|
|
|
extends MinimalActorRef {
|
2015-03-30 16:17:12 +02:00
|
|
|
|
import AbstractPromiseActorRef.{ stateOffset, watchedByOffset }
|
2012-03-23 21:35:52 +01:00
|
|
|
|
import PromiseActorRef._
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
add akka-typed project with generic ActorRef
This is the first step towards more type-safety in Actor interactions,
comprising:
* generic ActorRef[T] that only accepts T messages
* generic ActorSystem[T] extends ActorRef[T] (sending to the guardian,
whose Props[T] are provided for ActorSystem construction)
* removed the Actor trait: everything in there has been made into
messages and signals
* new Behavior[T] abstraction that consumes messages (of type T) or
Signals (lifecycle hooks, Terminated, ReceiveTimeout, Failed),
producing the next Behavior[T] as the result each time
* the ask pattern is provided and yields properly typed Futures
* variants of ActorContext are provided for synchronous testing of
Behaviors
All of this is implemented without touching code outside akka-typed
(apart from making guardianProps configurable), creating wrapper objects
around ActorRef, ActorContext, ActorSystem, Props and providing an Actor
implementation that just runs a Behavior.
2015-01-28 20:45:21 +01:00
|
|
|
|
// This is necessary for weaving the PromiseActorRef into the asked message, i.e. the replyTo pattern.
|
|
|
|
|
|
@volatile var messageClassName = _mcn
|
|
|
|
|
|
|
2012-01-18 11:52:35 +01:00
|
|
|
|
/**
|
2012-03-23 21:35:52 +01:00
|
|
|
|
* As an optimization for the common (local) case we only register this PromiseActorRef
|
|
|
|
|
|
* with the provider when the `path` member is actually queried, which happens during
|
2012-03-24 23:02:37 +01:00
|
|
|
|
* serialization (but also during a simple call to `toString`, `equals` or `hashCode`!).
|
2012-03-23 21:35:52 +01:00
|
|
|
|
*
|
|
|
|
|
|
* Defined states:
|
2012-03-24 23:02:37 +01:00
|
|
|
|
* null => started, path not yet created
|
|
|
|
|
|
* Registering => currently creating temp path and registering it
|
|
|
|
|
|
* path: ActorPath => path is available and was registered
|
|
|
|
|
|
* StoppedWithPath(path) => stopped, path available
|
|
|
|
|
|
* Stopped => stopped, path not yet created
|
2012-01-18 11:52:35 +01:00
|
|
|
|
*/
|
2012-03-23 21:35:52 +01:00
|
|
|
|
@volatile
|
2021-02-01 15:38:29 +00:00
|
|
|
|
@nowarn("msg=never used")
|
2012-03-27 09:28:54 +02:00
|
|
|
|
private[this] var _stateDoNotCallMeDirectly: AnyRef = _
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-05-30 13:24:38 +02:00
|
|
|
|
@volatile
|
2021-02-01 15:38:29 +00:00
|
|
|
|
@nowarn("msg=never used")
|
2012-05-30 13:24:38 +02:00
|
|
|
|
private[this] var _watchedByDoNotCallMeDirectly: Set[ActorRef] = ActorCell.emptyActorRefSet
|
|
|
|
|
|
|
2021-02-01 15:38:29 +00:00
|
|
|
|
@nowarn private def _preventPrivateUnusedErasure = {
|
|
|
|
|
|
_stateDoNotCallMeDirectly
|
|
|
|
|
|
_watchedByDoNotCallMeDirectly
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-05-30 13:24:38 +02:00
|
|
|
|
@inline
|
2019-03-11 10:38:24 +01:00
|
|
|
|
private[this] def watchedBy: Set[ActorRef] =
|
|
|
|
|
|
Unsafe.instance.getObjectVolatile(this, watchedByOffset).asInstanceOf[Set[ActorRef]]
|
2012-05-30 13:24:38 +02:00
|
|
|
|
|
|
|
|
|
|
@inline
|
|
|
|
|
|
private[this] def updateWatchedBy(oldWatchedBy: Set[ActorRef], newWatchedBy: Set[ActorRef]): Boolean =
|
|
|
|
|
|
Unsafe.instance.compareAndSwapObject(this, watchedByOffset, oldWatchedBy, newWatchedBy)
|
|
|
|
|
|
|
|
|
|
|
|
@tailrec // Returns false if the Promise is already completed
|
|
|
|
|
|
private[this] final def addWatcher(watcher: ActorRef): Boolean = watchedBy match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case null => false
|
|
|
|
|
|
case other => updateWatchedBy(other, other + watcher) || addWatcher(watcher)
|
2012-05-30 13:24:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
private[this] final def remWatcher(watcher: ActorRef): Unit = watchedBy match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case null => ()
|
|
|
|
|
|
case other => if (!updateWatchedBy(other, other - watcher)) remWatcher(watcher)
|
2012-05-30 13:24:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
private[this] final def clearWatchers(): Set[ActorRef] = watchedBy match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case null => ActorCell.emptyActorRefSet
|
|
|
|
|
|
case other => if (!updateWatchedBy(other, null)) clearWatchers() else other
|
2012-05-30 13:24:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
@inline
|
2012-05-30 13:24:38 +02:00
|
|
|
|
private[this] def state: AnyRef = Unsafe.instance.getObjectVolatile(this, stateOffset)
|
2012-03-24 23:02:37 +01:00
|
|
|
|
|
|
|
|
|
|
@inline
|
2012-05-30 13:24:38 +02:00
|
|
|
|
private[this] def updateState(oldState: AnyRef, newState: AnyRef): Boolean =
|
|
|
|
|
|
Unsafe.instance.compareAndSwapObject(this, stateOffset, oldState, newState)
|
2012-03-24 23:02:37 +01:00
|
|
|
|
|
|
|
|
|
|
@inline
|
2012-05-30 13:24:38 +02:00
|
|
|
|
private[this] def setState(newState: AnyRef): Unit = Unsafe.instance.putObjectVolatile(this, stateOffset, newState)
|
2012-03-23 15:52:36 +01:00
|
|
|
|
|
2012-05-18 16:41:19 +02:00
|
|
|
|
override def getParent: InternalActorRef = provider.tempContainer
|
2012-03-27 09:28:54 +02:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Contract of this method:
|
2012-03-24 23:02:37 +01:00
|
|
|
|
* Must always return the same ActorPath, which must have
|
2012-03-23 21:35:52 +01:00
|
|
|
|
* been registered if we haven't been stopped yet.
|
|
|
|
|
|
*/
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
def path: ActorPath = state match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case null =>
|
2012-03-24 23:02:37 +01:00
|
|
|
|
if (updateState(null, Registering)) {
|
2012-03-23 21:35:52 +01:00
|
|
|
|
var p: ActorPath = null
|
|
|
|
|
|
try {
|
2020-07-08 17:43:52 +02:00
|
|
|
|
p = provider.tempPath(refPathPrefix)
|
2012-03-23 15:52:36 +01:00
|
|
|
|
provider.registerTempActor(this, p)
|
|
|
|
|
|
p
|
2019-03-11 10:38:24 +01:00
|
|
|
|
} finally {
|
|
|
|
|
|
setState(p)
|
|
|
|
|
|
}
|
2012-03-23 21:35:52 +01:00
|
|
|
|
} else path
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case p: ActorPath => p
|
|
|
|
|
|
case StoppedWithPath(p) => p
|
2019-03-11 10:38:24 +01:00
|
|
|
|
case Stopped =>
|
2012-03-23 21:35:52 +01:00
|
|
|
|
// even if we are already stopped we still need to produce a proper path
|
2012-03-24 23:02:37 +01:00
|
|
|
|
updateState(Stopped, StoppedWithPath(provider.tempPath()))
|
2012-03-23 21:35:52 +01:00
|
|
|
|
path
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case Registering => path // spin until registration is completed
|
2021-03-30 20:57:23 +02:00
|
|
|
|
case unexpected => throw new IllegalStateException(s"Unexpected state: $unexpected")
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-10-06 00:13:42 +02:00
|
|
|
|
override def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit = state match {
|
2020-05-05 11:13:21 -04:00
|
|
|
|
case Stopped | _: StoppedWithPath =>
|
|
|
|
|
|
provider.deadLetters ! message
|
|
|
|
|
|
onComplete(message, alreadyCompleted = true)
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case _ =>
|
2017-01-13 12:06:09 +03:00
|
|
|
|
if (message == null) throw InvalidMessageException("Message is null")
|
2019-05-09 00:51:15 +08:00
|
|
|
|
val promiseResult = message match {
|
|
|
|
|
|
case Status.Success(r) => Success(r)
|
|
|
|
|
|
case Status.Failure(f) => Failure(f)
|
|
|
|
|
|
case other => Success(other)
|
|
|
|
|
|
}
|
2020-05-05 11:13:21 -04:00
|
|
|
|
val alreadyCompleted = !result.tryComplete(promiseResult)
|
|
|
|
|
|
if (alreadyCompleted)
|
2019-05-09 00:51:15 +08:00
|
|
|
|
provider.deadLetters ! message
|
2020-05-05 11:13:21 -04:00
|
|
|
|
onComplete(message, alreadyCompleted)
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-05-29 14:09:22 +02:00
|
|
|
|
override def sendSystemMessage(message: SystemMessage): Unit = message match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case _: Terminate => stop()
|
|
|
|
|
|
case DeathWatchNotification(a, ec, at) => this.!(Terminated(a)(existenceConfirmed = ec, addressTerminated = at))
|
|
|
|
|
|
case Watch(watchee, watcher) =>
|
2012-06-01 14:49:12 +02:00
|
|
|
|
if (watchee == this && watcher != this) {
|
2013-03-26 13:59:46 +01:00
|
|
|
|
if (!addWatcher(watcher))
|
|
|
|
|
|
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
|
2019-03-11 10:38:24 +01:00
|
|
|
|
watcher.sendSystemMessage(
|
|
|
|
|
|
DeathWatchNotification(watchee, existenceConfirmed = true, addressTerminated = false))
|
2012-06-01 14:49:12 +02:00
|
|
|
|
} else System.err.println("BUG: illegal Watch(%s,%s) for %s".format(watchee, watcher, this))
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case Unwatch(watchee, watcher) =>
|
2012-06-01 14:49:12 +02:00
|
|
|
|
if (watchee == this && watcher != this) remWatcher(watcher)
|
|
|
|
|
|
else System.err.println("BUG: illegal Unwatch(%s,%s) for %s".format(watchee, watcher, this))
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case _ =>
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2015-05-08 10:37:41 +02:00
|
|
|
|
override private[akka] def isTerminated: Boolean = state match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case Stopped | _: StoppedWithPath => true
|
|
|
|
|
|
case _ => false
|
2012-03-24 23:02:37 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-03-23 21:35:52 +01:00
|
|
|
|
@tailrec
|
2012-03-24 23:02:37 +01:00
|
|
|
|
override def stop(): Unit = {
|
2012-05-30 13:24:38 +02:00
|
|
|
|
def ensureCompleted(): Unit = {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
result.tryComplete(ActorStopResult)
|
2012-05-30 13:24:38 +02:00
|
|
|
|
val watchers = clearWatchers()
|
2019-05-09 00:51:15 +08:00
|
|
|
|
if (watchers.nonEmpty) {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
watchers.foreach { watcher =>
|
2013-03-26 13:59:46 +01:00
|
|
|
|
// ➡➡➡ NEVER SEND THE SAME SYSTEM MESSAGE OBJECT TO TWO ACTORS ⬅⬅⬅
|
2019-03-11 10:38:24 +01:00
|
|
|
|
watcher
|
|
|
|
|
|
.asInstanceOf[InternalActorRef]
|
2015-03-30 16:17:12 +02:00
|
|
|
|
.sendSystemMessage(DeathWatchNotification(this, existenceConfirmed = true, addressTerminated = false))
|
2013-03-26 13:59:46 +01:00
|
|
|
|
}
|
2012-05-30 13:24:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-03-24 23:02:37 +01:00
|
|
|
|
state match {
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case null => // if path was never queried nobody can possibly be watching us, so we don't have to publish termination either
|
2012-05-28 16:49:49 +02:00
|
|
|
|
if (updateState(null, Stopped)) ensureCompleted() else stop()
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case p: ActorPath =>
|
2019-03-11 10:38:24 +01:00
|
|
|
|
if (updateState(p, StoppedWithPath(p))) {
|
|
|
|
|
|
try ensureCompleted()
|
|
|
|
|
|
finally provider.unregisterTempActor(p)
|
|
|
|
|
|
} else stop()
|
2019-02-09 15:25:39 +01:00
|
|
|
|
case Stopped | _: StoppedWithPath => // already stopped
|
|
|
|
|
|
case Registering => stop() // spin until registration is completed before stopping
|
2021-03-30 20:57:23 +02:00
|
|
|
|
case unexpected => throw new IllegalStateException(s"Unexpected state: $unexpected")
|
2012-03-24 23:02:37 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
}
|
2020-05-05 11:13:21 -04:00
|
|
|
|
|
|
|
|
|
|
@InternalStableApi
|
|
|
|
|
|
private[akka] def ask(actorSel: ActorSelection, message: Any, @unused timeout: Timeout): Future[Any] = {
|
|
|
|
|
|
actorSel.tell(message, this)
|
|
|
|
|
|
result.future
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@InternalStableApi
|
|
|
|
|
|
private[akka] def ask(actorRef: ActorRef, message: Any, @unused timeout: Timeout): Future[Any] = {
|
|
|
|
|
|
actorRef.tell(message, this)
|
|
|
|
|
|
result.future
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@InternalStableApi
|
|
|
|
|
|
private[akka] def onComplete(@unused message: Any, @unused alreadyCompleted: Boolean): Unit = {}
|
|
|
|
|
|
|
|
|
|
|
|
@InternalStableApi
|
|
|
|
|
|
private[akka] def onTimeout(@unused timeout: Timeout): Unit = {}
|
2012-03-23 21:35:52 +01:00
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
|
2012-05-18 16:41:19 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2018-01-19 18:13:24 +01:00
|
|
|
|
@InternalApi
|
2012-03-23 21:35:52 +01:00
|
|
|
|
private[akka] object PromiseActorRef {
|
2012-03-24 23:02:37 +01:00
|
|
|
|
private case object Registering
|
|
|
|
|
|
private case object Stopped
|
2014-03-07 13:20:01 +01:00
|
|
|
|
private final case class StoppedWithPath(path: ActorPath)
|
2012-03-23 21:35:52 +01:00
|
|
|
|
|
2017-01-13 12:06:09 +03:00
|
|
|
|
private val ActorStopResult = Failure(ActorKilledException("Stopped"))
|
2019-02-09 15:25:39 +01:00
|
|
|
|
private val defaultOnTimeout: String => Throwable = str => new AskTimeoutException(str)
|
2014-12-22 11:35:28 +01:00
|
|
|
|
|
2019-03-13 10:56:20 +01:00
|
|
|
|
def apply(
|
|
|
|
|
|
provider: ActorRefProvider,
|
|
|
|
|
|
timeout: Timeout,
|
|
|
|
|
|
targetName: Any,
|
|
|
|
|
|
messageClassName: String,
|
2020-07-08 17:43:52 +02:00
|
|
|
|
refPathPrefix: String,
|
2019-03-13 10:56:20 +01:00
|
|
|
|
sender: ActorRef = Actor.noSender,
|
|
|
|
|
|
onTimeout: String => Throwable = defaultOnTimeout): PromiseActorRef = {
|
2020-07-08 17:43:52 +02:00
|
|
|
|
if (refPathPrefix.indexOf('/') > -1)
|
|
|
|
|
|
throw new IllegalArgumentException(s"refPathPrefix must not contain slash, was: $refPathPrefix")
|
2012-07-04 15:25:30 +02:00
|
|
|
|
val result = Promise[Any]()
|
2013-03-30 01:03:17 +01:00
|
|
|
|
val scheduler = provider.guardian.underlying.system.scheduler
|
2020-07-08 17:43:52 +02:00
|
|
|
|
val a = new PromiseActorRef(provider, result, messageClassName, refPathPrefix)
|
2020-03-10 15:39:30 +01:00
|
|
|
|
implicit val ec = ExecutionContexts.parasitic
|
2013-10-16 14:47:17 +02:00
|
|
|
|
val f = scheduler.scheduleOnce(timeout.duration) {
|
2020-05-05 11:13:21 -04:00
|
|
|
|
val timedOut = result.tryComplete {
|
2018-09-19 17:38:17 +02:00
|
|
|
|
val wasSentBy = if (sender == ActorRef.noSender) "" else s" was sent by [$sender]"
|
|
|
|
|
|
val messagePart = s"Message of type [${a.messageClassName}]$wasSentBy."
|
|
|
|
|
|
Failure(
|
2019-03-11 10:38:24 +01:00
|
|
|
|
onTimeout(
|
|
|
|
|
|
s"Ask timed out on [$targetName] after [${timeout.duration.toMillis} ms]. " +
|
2018-09-19 17:38:17 +02:00
|
|
|
|
messagePart +
|
|
|
|
|
|
" A typical reason for `AskTimeoutException` is that the recipient actor didn't send a reply."))
|
|
|
|
|
|
}
|
2020-05-05 11:13:21 -04:00
|
|
|
|
if (timedOut) {
|
|
|
|
|
|
a.onTimeout(timeout)
|
|
|
|
|
|
}
|
2013-10-16 14:47:17 +02:00
|
|
|
|
}
|
2019-03-11 10:38:24 +01:00
|
|
|
|
result.future.onComplete { _ =>
|
|
|
|
|
|
try a.stop()
|
|
|
|
|
|
finally f.cancel()
|
|
|
|
|
|
}
|
2012-01-18 11:52:35 +01:00
|
|
|
|
a
|
|
|
|
|
|
}
|
2015-04-30 09:23:18 +02:00
|
|
|
|
|
2013-01-09 01:47:48 +01:00
|
|
|
|
}
|