Actor

peloton.actor.Actor
See theActor companion object
abstract class Actor[M]

Actors are the basic building blocks of concurrent computation. In response to a message it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received. Actors may modify their own private state, but can only affect each other indirectly through messaging.

Messages sent to an actor are put into a private message inbox. The actor will take each of these messages and process them sequentially by applying them to the actor's message handler function. The result of the message handler function may be given back to the sender of the message (ASK pattern, see below).

There are two different patterns to communicate with an actor:

==The TELL pattern==

When using the TELL pattern, we just send a single message to the actor (which is very fast) and forget about it. No response is expected to be returned by the actor. This pattern is used for asynchronous communication, as it is not guaranteed when the message will be processed by the actor. The message is put at the back of the actor's message queue and processed when all other messages have been processed.

===The ASK pattern==

The ASK pattern is used when a response is required from the actor (synchronous communication). A single message is sent to the actor and the current process (the caller) is suspended until the actor has processed the message and returned a response.

Interacting with an actor is effectful. This is why all actor functions (tell, ask, terminate) are wrapped into an effect type.

Type parameters

M

The actor's base message type (message handler input)

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def ask[M2 <: M, R](message: M2, timeout: Duration)(using CanAsk[M2, R]): IO[R]

Send a message to the actor using the ASK pattern.

Send a message to the actor using the ASK pattern.

Type parameters

M2

A covariant subclass of the actor's message type M.

R

The expected response type

Value parameters

CanAsk

A given instance of CanAsk for the type parameters M2 and R. The existence of this instance ensures that the actor supports this ask and, if asked with an instance of type M2, it will answer with an instance of type R and not with some different type R2 or doesn't answer at all (like in the TELL pattern). In other words, if an actor provides an instance of CanAsk[M2, R], it guarantees to the client that the ASK pattern for M2 and R is supported by the Actor's protocol. As the existence of the CanAsk instance is checked by the compiler, this pattern serves as a compile-time guard to avoid invalid message/response usages of the ASK pattern.

message

A message of type M2

timeout

In the event that the specified time has passed without the actor returning a response, a TimeoutException is raised.

Attributes

Returns

An effect that, on evaluation, returns a value of the expected message response type R

def tell(message: M): IO[Unit]

Send a message to the actor using the TELL pattern.

Send a message to the actor using the TELL pattern.

Value parameters

message

A message of the actor's message type M

Attributes

Returns

An effect that, on evaluation, returns Unit

def terminate: IO[Unit]

Terminate the actor.

Terminate the actor.

This function is synchronous and effectful. It is guaranteed that the actor is terminated after evaluating the effect.

Attributes

Returns

an effect that, on evaluation, will terminate the given actor and returns Unit

Concrete methods

inline def !(message: M): IO[Unit]

Send a message to the actor using the TELL pattern.

Send a message to the actor using the TELL pattern.

An alias for the tell function.

Value parameters

message

A message of the actor's message type M

Attributes

Returns

An effect that, on evaluation, returns Unit

inline def ?[M2 <: M, R](message: M2, timeout: Duration)(using CanAsk[M2, R]): IO[R]

Send a message to the actor using the ASK pattern and convert the result to a specific narrower response type.

Send a message to the actor using the ASK pattern and convert the result to a specific narrower response type.

An alias for the ask function.

Type parameters

M2

A covariant subclass of the actor's message type.

R

The expected response type.

Value parameters

Ask

A given instance of Ask for type parameters M2 and R. This ensures that the actor supports the function from M2 and R, i.e., if asked with M2 it will answer with R and not with some different R2

message

A message of type M2

timeout

In the event that the specified time has passed without the actor returning a response, a TimeoutException is raised.

Attributes

Returns

An effect that, on evaluation, returns a value of the response type R