private static async Task Main()
        {
            Log.SetLoggerFactory(LoggerFactory.Create(c => c
                                                      .SetMinimumLevel(LogLevel.Information)
                                                      .AddConsole()
                                                      )
                                 );

#if NETCORE
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif

            Console.WriteLine("Enter 0 to use GrpcCore provider");
            Console.WriteLine("Enter 1 to use GrpcNet provider");
            if (!int.TryParse(Console.ReadLine(), out var provider))
            {
                provider = 0;
            }

            Console.WriteLine("Enter Advertised Host (Enter = localhost)");
            var advertisedHost = Console.ReadLine().Trim();
            if (string.IsNullOrEmpty(advertisedHost))
            {
                advertisedHost = "127.0.0.1";
            }

            var actorSystemConfig = new ActorSystemConfig()
                                    .WithDeadLetterThrottleCount(10)
                                    .WithDeadLetterThrottleInterval(TimeSpan.FromSeconds(2));
            var     system  = new ActorSystem(actorSystemConfig);
            var     context = new RootContext(system);
            IRemote remote;

            if (provider == 0)
            {
                var remoteConfig = GrpcCoreRemoteConfig
                                   .BindTo(advertisedHost, 12000)
                                   .WithProtoMessages(ProtosReflection.Descriptor)
                                   .WithRemoteKind("echo", Props.FromProducer(() => new EchoActor()));
                remote = new GrpcCoreRemote(system, remoteConfig);
            }
            else
            {
                var remoteConfig = GrpcNetRemoteConfig
                                   .BindTo(advertisedHost, 12000)
                                   .WithChannelOptions(new GrpcChannelOptions
                {
                    CompressionProviders = new[]
                    {
                        new GzipCompressionProvider(CompressionLevel.Fastest)
                    }
                }
                                                       )
                                   .WithProtoMessages(ProtosReflection.Descriptor)
                                   .WithRemoteKind("echo", Props.FromProducer(() => new EchoActor()));
                remote = new GrpcNetRemote(system, remoteConfig);
            }

            await remote.StartAsync();

            context.SpawnNamed(Props.FromProducer(() => new EchoActor()), "remote");
            Console.ReadLine();
            await remote.ShutdownAsync();
        }
示例#2
0
    private static async Task Main()
    {
        Log.SetLoggerFactory(LoggerFactory.Create(c => c
                                                  .SetMinimumLevel(LogLevel.Information)
                                                  .AddConsole()
                                                  )
                             );

        var logger = Log.CreateLogger <Program>();

#if NETCORE
        AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif

        Console.WriteLine("Enter 0 to use GrpcCore provider");
        Console.WriteLine("Enter 1 to use GrpcNet provider");
        if (!int.TryParse(Console.ReadLine(), out var provider))
        {
            provider = 0;
        }

        Console.WriteLine("Enter client advertised host (Enter = localhost)");
        var advertisedHost = Console.ReadLine().Trim();
        if (string.IsNullOrEmpty(advertisedHost))
        {
            advertisedHost = "127.0.0.1";
        }

        Console.WriteLine("Enter remote advertised host (Enter = localhost)");
        var remoteAddress = Console.ReadLine().Trim();

        if (string.IsNullOrEmpty(remoteAddress))
        {
            remoteAddress = "127.0.0.1";
        }

        var actorSystemConfig = new ActorSystemConfig()
                                .WithDeadLetterThrottleCount(10)
                                .WithDeadLetterThrottleInterval(TimeSpan.FromSeconds(2));
        var system  = new ActorSystem(actorSystemConfig);
        var context = new RootContext(system);

        IRemote remote;

        if (provider == 0)
        {
            var remoteConfig = GrpcCoreRemoteConfig
                               .BindTo(advertisedHost)
                               .WithProtoMessages(ProtosReflection.Descriptor);
            remote = new GrpcCoreRemote(system, remoteConfig);
        }
        else
        {
            var remoteConfig = GrpcNetRemoteConfig
                               .BindTo(advertisedHost)
                               .WithChannelOptions(new GrpcChannelOptions
            {
                CompressionProviders = new[]
                {
                    new GzipCompressionProvider(CompressionLevel.Fastest)
                }
            }
                                                   )
                               .WithProtoMessages(ProtosReflection.Descriptor);
            remote = new GrpcNetRemote(system, remoteConfig);
        }

        await remote.StartAsync();

        var messageCount            = 1000000;
        var cancellationTokenSource = new CancellationTokenSource();
        _ = SafeTask.Run(async() => {
            while (!cancellationTokenSource.IsCancellationRequested)
            {
                var semaphore = new SemaphoreSlim(0);
                var props     = Props.FromProducer(() => new LocalActor(0, messageCount, semaphore));

                var pid = context.Spawn(props);

                try
                {
                    var actorPidResponse =
                        await remote.SpawnAsync($"{remoteAddress}:12000", "echo", TimeSpan.FromSeconds(1));

                    if (actorPidResponse.StatusCode == (int)ResponseStatusCode.OK)
                    {
                        var remotePid = actorPidResponse.Pid;
                        await context.RequestAsync <Start>(remotePid, new StartRemote {
                            Sender = pid
                        },
                                                           TimeSpan.FromSeconds(1)
                                                           );
                        var stopWatch = new Stopwatch();
                        stopWatch.Start();
                        Console.WriteLine("Starting to send");
                        var msg = new Ping();

                        for (var i = 0; i < messageCount; i++)
                        {
                            context.Send(remotePid, msg);
                        }

                        var linkedTokenSource =
                            CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token,
                                                                            new CancellationTokenSource(2000).Token
                                                                            );
                        await semaphore.WaitAsync(linkedTokenSource.Token);
                        stopWatch.Stop();
                        var elapsed = stopWatch.Elapsed;
                        Console.WriteLine("Elapsed {0}", elapsed);

                        var t = messageCount * 2.0 / elapsed.TotalMilliseconds * 1000;
                        Console.Clear();
                        Console.WriteLine("Throughput {0} msg / sec", t);
                        await context.StopAsync(remotePid);
                    }
                }
                catch (OperationCanceledException)
                {
                    await Task.Delay(1000);
                }
                catch (Exception e)
                {
                    logger?.LogError(e, "Error");
                    await Task.Delay(5000);
                }

                await context.PoisonAsync(pid);
            }
        }, cancellationTokenSource.Token
                         );

        Console.ReadLine();
        cancellationTokenSource.Cancel();
        await Task.Delay(1000);

        Console.WriteLine("Press enter to quit");
        Console.ReadLine();
        await remote.ShutdownAsync();
    }