private static GrpcCoreRemoteConfig GetRemoteConfig()
        {
            var portStr        = Environment.GetEnvironmentVariable("PROTOPORT") ?? $"{RemoteConfigBase.AnyFreePort}";
            var port           = int.Parse(portStr);
            var host           = Environment.GetEnvironmentVariable("PROTOHOST") ?? RemoteConfigBase.Localhost;
            var advertisedHost = Environment.GetEnvironmentVariable("PROTOHOSTPUBLIC");

            var remoteConfig = GrpcCoreRemoteConfig
                               .BindTo(host, port)
                               .WithAdvertisedHost(advertisedHost)
                               .WithProtoMessages(MessagesReflection.Descriptor)
                               .WithEndpointWriterMaxRetries(2);

            return(remoteConfig);
        }
Exemple #2
0
        public static (ClusterConfig, GrpcCoreRemoteConfig) CreateClusterConfig(IClusterSettings clusterSettings, IClusterProvider clusterProvider, IIdentityLookup identityLookup, IDescriptorProvider descriptorProvider, ILogger _logger)
        {
            //var portStr = Environment.GetEnvironmentVariable("PROTOPORT") ?? $"{RemoteConfigBase.AnyFreePort}";

            var clusterName = clusterSettings.ClusterName;

            var hostip         = Environment.GetEnvironmentVariable("PROTOHOST");
            var host           = clusterSettings.ClusterHost;
            var advertisedHost = clusterSettings.ClusterHost;
            var port           = clusterSettings.ClusterPort;

            if ("protohost".Equals(host))
            {
                host           = hostip;
                advertisedHost = hostip;
                _logger.LogDebug($"Using PROTOHOST");
            }
            _logger.LogDebug($"BindTo to {host} port {port}");
            _logger.LogDebug($"WithAdvertisedHost to {advertisedHost}");

            FileDescriptor[] descriptors = descriptorProvider.GetDescriptors();


            // TOOD: This doesn't seem to work. Why?
            List <ChannelOption> options = new List <ChannelOption>()
            {
                new ChannelOption(ChannelOptions.MaxSendMessageLength, (100 * 1024 * 1024)),
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength, (100 * 1024 * 1024))
            };


            GrpcCoreRemoteConfig remoteConfig = GrpcCoreRemoteConfig.BindTo(host, port)
                                                .WithEndpointWriterMaxRetries(0)
                                                .WithRemoteDiagnostics(true)
                                                .WithAdvertisedHost(advertisedHost)
                                                .WithProtoMessages(descriptors)
                                                .WithChannelOptions(options);

            var clusterConfig = ClusterConfig.Setup(clusterName, clusterProvider, identityLookup);

            return(clusterConfig, remoteConfig);
        }
        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();
        }
        //TODO: Uncomment to enable metrics
        //  .WithMetricsProviders(new StatsdConfigurator(new[] { new Label("service", "my-system-name") }));

        private static GrpcCoreRemoteConfig GetRemoteConfig() => GrpcCoreRemoteConfig
        .BindTo("127.0.0.1")
        //   .WithAdvertisedHost("the hostname or ip of this pod")
        .WithProtoMessages(MyMessagesReflection.Descriptor);
Exemple #5
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();
    }