2

I try to create application using Akka.NET.

The main goal is to make a server that can handle many client connections and requests at the same time. I chose Akka.NET for this. I have a cluster, which now consists only 1 node.

I also have cluster clients (ClusterClient) that are simultaneously starting to connect to the server. The logic of the client is simple: it connects to the server and subscribe an actor there. There are no problems with publish yet, customers are getting everything, of course, if the connection is stable. Somewhere near ​​~4000-5000 client connections, reconnects begin and the connection is lost accordingly. I have tried to add a second node to the cluster and make 3000 connections for each node, but this was unsuccessful.

The question is how to make a server on AKKA.Net, which would hold a large number of connections (for example, 100 000 - 1 000 000). And could I use cluster for this purpose?

Server

using Akka.Actor;
using Akka.Configuration;
using System;
using System.Configuration;
using Akka.Cluster.Tools.Client;
using System.Threading;

namespace CoreSPServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Config config = ConfigurationFactory.ParseString(ConfigurationManager.AppSettings["ClusterConfig"]);
            ActorSystem system = ActorSystem.Create("ClusterSystem", config);
            var publisher = system.ActorOf(Props.Create(() => new Publisher()), "Publisher");
            var clientSub = system.ActorOf(Props.Create(() => new ClientSubscriber()), "Sub");

            ClusterClientReceptionist.Get(system).RegisterService(clientSub);

            if (Console.ReadLine() == "start")
            {
                    publisher.Tell("test");
                    Thread.Sleep(10);
            }

            Console.ReadKey();
        }
    }
}

ClientSubscriber and publisher

class ClientSubscriber : ReceiveActor
{
    public ClientSubscriber()
    {
        var mediator = DistributedPubSub.Get(Context.System).Mediator;

        Receive<IActorRef>(senderToSub =>
        {
            mediator.Tell(new Subscribe("content", senderToSub));
        });
    }
}

public class Publisher : ReceiveActor
    {
        public Publisher()
        {
            var mediator = DistributedPubSub.Get(Context.System).Mediator;

            Receive<string>(str =>
            {
                var upperCase = str.ToUpper();
                mediator.Tell(new Publish("content", upperCase));
            });
        }
    }

Client

static void Main(string[] args)
        {
            var config = ConfigurationFactory.ParseString(ConfigurationManager.AppSettings["ClientConf"]);
            ActorSystem Sys = ActorSystem.Create("ClusterClient", config);

            //Connection path to Cluster Node
            var initialContacts = new List<ActorPath>(){
                    ActorPath.Parse("akka.tcp://ClusterSystem@localhost:5001/system/receptionist"),
            }.ToImmutableHashSet();
            var settings = ClusterClientSettings.Create(Sys).WithInitialContacts(initialContacts);

            for(int i = 0; i < 5000; i++)
            {
                    IActorRef c = Sys.ActorOf(ClusterClient.Props(settings), "client" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
                    var asd = Sys.ActorOf(Props.Create<Subscriber>(), "clientSub" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
                    c.Tell(new ClusterClient.Send("/user/Sub5001", asd));
                    Thread.Sleep(1/10);
            }

            Console.ReadKey();
        }

Server config

akka {
extensions = [&quot;Akka.Cluster.Tools.Client.ClusterClientReceptionistExtensionProvider, Akka.Cluster.Tools&quot;]
actor.provider = cluster
remote {
    dot-netty.tcp {
        port = 5001
        public-hostname = localhost
        auto-down-unreachable-after = off
    }
}
cluster {
   seed-nodes = [&quot;akka.tcp://ClusterSystem@localhost:5001&quot;]
}
}

0 Answers0