Mostly used akka primitive is Actor - any state changes or state queries are done exclusively via async messages.
Akka also provides less-known primitive Agent, which is updated asynchronously by new value (or by function modifying his value) ans can be read synchronously from any thread.
Question: In single-JVM Actor system, how to combine these approaches: have primitive which is updated by async. messages only (like Actor
) but which state can be read directly in thread-safe manner (like Agent
) ?
I would really appreciate link to any relevant discussion/project/sample.
Solutions I found so far:
Actor
which updates Agent
in receive()
. But I am wondering if there is more elegant & performant solution. Actor
separation by publishing (in outgoing message) function for reading actor's state, similar to Agent.get()
. Thread-safety must be taken care of manually.Actor
update some external shared state in receive()
. Thread-safety must be taken care of manually.Note that agents have been flagged as deprecated by the Akka Team, and will be removed at the next major release (probably 2.5.0). This is the corresponding ticket.
Indeed, the core idea of Akka is to encourage asynchronous communication, so I guess ditching Agents makes sense from this perspective.
An alternative solution might be to use an Actor and asynchronously access its state. If this scenario concerns you as a Get
message might need to wait until a few Update
messages are processed, you can consider using a Priority Mailbox to process Get
messages first. Example here.