ActorInterfaces.jl
ActorInterfaces.jl
is a formalisation of the actor model family in Julia. Its main goal is to allow writing actor programs independently from any specific implementation of the model. Programs written on top of ActorInterfaces.jl
will potentially run on several actor systems.
There is no such thing as The Actor Model, there are interpretations and extensions of it. ActorInterfaces tries to handle this diversity by defining a minimalistic base called the Classic Model, and extensions to it.
Current State: Early stage. The Classic Model is usable but not yet settled. No extension is defined yet. If you have an idea to discuss, please send a PR or create an issue.
Known implementations:
QuickActors.jl
, reference implementation.
ActorInterfaces.Classic
ActorInterfaces.Classic.Addr
ActorInterfaces.MissingActorSystem
ActorInterfaces.Classic.become
ActorInterfaces.Classic.onmessage
ActorInterfaces.Classic.self
ActorInterfaces.Classic.send
ActorInterfaces.Classic.spawn
ActorInterfaces._actorsystem
ActorInterfaces.actorsystem
ActorInterfaces.Classic.@ctx
Please refer the source for more details.
ActorInterfaces.MissingActorSystem
— TypeMissingActorSystem <: ActorException
No ActorInterface implementation is loaded.
ActorInterfaces._actorsystem
— Function_actorsystem
ActorInterfaces.actorsystem
— Methodactorsystem()::Module
Return the currently active implementation of ActorInterfaces, or throw MissingActorSystem
.
ActorInterfaces.Classic
— Modulemodule ActorInterfaces.Classic
The Classic model is the one described by Gul Agha in the book "ACTORS: A Model of Concurrent Computation in Distributed Systems"
ActorInterfaces.Classic.Addr
— TypeAddr
Addr
uniquely identifies an actor, and can be used as the target of messages.
ActorInterfaces.Classic.become
— Functionbecome(behavior, [ctx])
Replace the behavior of the current actor with behavior
.
The new behavior will be effective at the processing of the next message.
The ctx argument can be automatically injected by @ctx
.
ActorInterfaces.Classic.self
— Functionself([ctx]) :: Addr
Get the address of the current actor.
The ctx argument can be automatically injected by @ctx
.
ActorInterfaces.Classic.send
— Functionsend(recipient::Addr, msg, [ctx])
Send the message msg
to a recipient
actor address.
The ctx argument can be automatically injected by @ctx
.
ActorInterfaces.Classic.spawn
— Functionspawn(behavior, [ctx]) :: Addr
Create a new actor from the given behavior and schedule it.
The returned address can be used to send messages to the newly created actor. The actor itself is not accessible directly.
The ctx argument can be automatically injected by @ctx
.
ActorInterfaces.Classic.@ctx
— Macro@ctx
Inject the actor context into onmessage
methods automatically.
The "actor context" allows the runtime to identify the current actor efficiently. It is passed to onmessage
and must be provided to every actor primitive call. It can either be handled explicitly by the user or implicitly by the @ctx macro.
When an onmessage
method definition is marked with @ctx
, calls to send
, spawn
, etc. from it need not to handle the ctx
argument, it will be injected by the macro.
ActorInterfaces.Classic.onmessage
— FunctionClassic.onmessage(me, msg, ctx)
@ctx Classic.onmessage(me, msg)
Handle the incoming message msg
received by an actor with behavior me
.
Messages will be dispatched to methods of this function.
Note on async and blocking: @async
is allowed in onmessage
, but async code should not operate directly on the actor state, only through messages. Blocking operations will also work inside onmessage
, but in the Classic model it is up to the implementation to provide any or no concurrency of blocked actors, so blocking should generally be avoided if possible.
Examples
mutable struct Counter
counter::Int
end
struct Increment end
@ctx function Classic.onmessage(me::Counter, msg::Increment)
me.counter += 1
end