Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to send an actor a message from something which is not an actor?

Tags:

scala

actor

Suppose I have some class which has a property actor_ of type Actor. Is there a problem with me doing

def someMethod() = {
  actor_ ! "HELLO"
}

Or should sending a message always be done from another actor; e.g.

def someMethod() = {
  Actor.actor { actor_ ! "HELLO" }
}
like image 1000
oxbow_lakes Avatar asked Jun 29 '09 13:06

oxbow_lakes


People also ask

How do I send a message to another actor in Akka?

In Akka, actors communicate to each other by sending and receiving messages. Akka provides two predefined methods tell() and ask() for message exchange. An actor can send messages to another Actor through these following methods. It is used to send a message asynchronously.

Why is an actor not a lifeline?

An actor does not belong to the system: it should therefore not appear as a lifeline. Typically, what should appear as a lifeline instead, is an instance of some classifier that belongs to the UI and that would notify the user of the information.

Can I include an actor in a sequence diagram?

It is however a common and convenient practice to include an actor in a sequence diagram. After all, the UML standard sees the actor as a classifier (clause 18.1.2), although it doesn't belong to the system. As this is not foreseen by the standard, there are no clear rules about what can be done and the applicable semantics.

What was the threat actor’s goal?

The threat actor appeared to have a goal of network disruption and appeared to use a common security hack tool that overwhelmed a particular server with a large amount of traffic. This traffic rendered the server inoperable.


1 Answers

It depends. When you send a message to an actor from non-actor code, an ActorProxy is automatically created and stored in a thread local. This creates a potential memory leak, albeit a very small one, because the ActorProxy won't be GC'd until the thread is GC'd. The ActorProxy essentially enables the non-actor thread to in many ways behave like an Actor, including receiving message.

The bigger problem is if your thread being managed, similar to the way the actor library manages threads, so that what represents a logical context may at one time be on one thread, and at another time be on another thread. A good example of this would be a servlet container. Your logical context may be a servlet or a session, but the ActorProxy is going to be bound to the thread, and thus shared across logical contexts. If your actors aren't replying to the ActorProxy this isn't such a big deal, but if they are it will likely lead to problems because either (a) the replies will potentially be received by the wrong context, or (b) the messages are never received, and thus the previously mentioned small leak becomes a large one as the mailboxes of the ActorProxies fill up.

[Edit] Hmm...I seem to have a problem reading questions! Surrounding it in the actor block creates a new actor object that will be GC'd properly when it terminates. Keep in mind that putting the message send in an actor block means that the message send will be done in a new reaction on another thread, not in the thread creating the actor.

like image 148
Erik Engbrecht Avatar answered Oct 26 '22 07:10

Erik Engbrecht