Akka and Akka.NET actors (potentially with Rx)

I started playing with an actor model and, in particular, with Akka.NET . In general, I think that I know well what everything is about, but, of course, the devil is in the details. I was thinking about using Akka.NET in an existing codebase, and therefore I would like to estimate how many existing abstractions can be saved. The idea is that some specific high-level interfaces can be saved, and some adapter implementations will be written to seamlessly go back and forth around the world to actors and users of existing interfaces, but I'm not sure if this is recommended or what types of specific problems. that I have to face.

Trivial example:

public interface IGetSet<in TK, TV> { TV Get(TK key); void Set(TK key, TV value); } public class GetSet<TK, TV> : IGetSet<TK, TV> { private readonly ActorRef _getSetActor; public GetSet(ActorRefFactory system) { _getSetActor = system.ActorOf(Props.Create(() => new GetSetActor())); } public TV Get(TK key) { var x = _getSetActor.Ask<TV>(new GetSetActor.Get(key)); return x.Result; //blocking, I know, it just an example } public void Set(TK key, TV value) { _getSetActor.Tell(new GetSetActor.Set(key, value)); } class GetSetActor : ReceiveActor { readonly Dictionary<TK, TV> _values = new Dictionary<TK, TV>(); public class Get { public Get(TK key) { Key = key; } public TK Key { get; private set; } } public class Set { public Set(TK key, TV value) { Key = key; Value = value; } public TK Key { get; private set; } public TV Value { get; private set; } } public GetSetActor() { Receive<Get>(g => Sender.Tell(_values[g.Key], Self)); Receive<Set>(g => _values[g.Key] = g.Value); } } } ... var gs = new GetSet<string, int>(ActorSystem.Create("Wasp")); gs.Set("a", 42); var i = gs.Get("a"); 

In this case, the IGetSet interface comes from the traditional world, and its implementation allows us to move back and forth with the acting world. I tried to be beautiful with actors who are never used in ways other than messaging, so overall this (trivial, of course) exercise looks promising, but I would like to know if there is anything else that I should pay attention to from day 1.

I read about avoiding additional actor-based asynchronous code with closing the state of the actors, which is clear, and I don’t do it, but maybe there is more that I don’t see. My ultimate goal is to use this template quite widely, right down to the point. I would write actor-oriented implementations of Rx ISubject (which BTW I already did and it was easy, but again I'm not sure that I paid enough attention to everything that I should).

I also read a little about typed actors , but I'm not a Scala expert, so maybe I don’t understand all the details from the code samples, and I'm not sure that they are already available in Akka.NET ( doc page - 404 )

+8
c # actor system.reactive
source share
1 answer

It looks good. What you should take into account is that by default the participants have a “no more than one time” delivery guarantee, so you should take into account that when communicating with your actor you cannot get an answer. (Network failure, remote remote hosts, etc.)

In the local system, it is very unlikely that the message will be lost, but, theoretically, the actor system can crash if someone does something too wild with it, and thus the actors die.

Thus, when communicating with an actor using Ask , it is better to be safe and provide a timeout and handle this exception, rather than blocking / waiting forever.

Async / Await is supported with the latest bits of pre release (pre 1.0). However, this is not recommended. it’s best to stick with PipeTo and be explicit.

Another thing that iffy can get is that in your example, you see the actor as a repository of key values, and that's all right. And your posts are also unchanged, which is also good. But if the Key or Value properties are ref types, and people can mutate them from the outside, for example. IGetSet consumers who can cause RC problems inside the actor, because the actor can read these values ​​when another thread mutates them.

ActorSystems also quite expensive, try to avoid the reversal of many systems that focus on one system per process.

In addition, you are good to go.

+6
source share

All Articles