private static async Task Main() { var system = new ActorSystem() .WithRemote(GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(ProtosReflection.Descriptor)) .WithCluster(ClusterConfig .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig()), new PartitionIdentityLookup())); await system .Cluster() .StartClientAsync(); Console.WriteLine("Started"); await Task.Delay(2000); var helloGrain = system.Cluster().GetHelloGrain("MyGrain"); var res = await helloGrain.SayHello(new HelloRequest(), WithTimeout(5000)); Console.WriteLine(res.Message); res = await helloGrain.SayHello(new HelloRequest(), WithTimeout(5000)); Console.WriteLine(res.Message); Console.CancelKeyPress += async(e, y) => { Console.WriteLine("Shutting Down..."); await system.Cluster().ShutdownAsync(); }; await Task.Delay(-1); }
static async Task Main(string[] args) { var system = new ActorSystem(); var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost(12000) .WithProtoMessages(ProtosReflection.Descriptor); var consulProvider = new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")); var clusterConfig = ClusterConfig.Setup("MyCluster", consulProvider, new PartitionIdentityLookup()); system .WithRemote(remoteConfig) .WithCluster(clusterConfig); var grains = new Grains(system.Cluster()); grains.HelloGrainFactory(() => new HelloGrain()); await system.Cluster().StartMemberAsync(); Console.CancelKeyPress += async(e, y) => { Console.WriteLine("Shutting Down..."); await system.Cluster().ShutdownAsync(); }; await Task.Delay(-1); }
public Fixture() { var clientConfig = ConfigureClientRemoteConfig(GrpcCoreRemoteConfig.BindToLocalhost(5000)); Remote = GetGrpcCoreRemote(clientConfig); var serverConfig = ConfigureServerRemoteConfig(GrpcCoreRemoteConfig.BindToLocalhost(5001)); ServerRemote = GetGrpcCoreRemote(serverConfig); }
static async Task Main(string[] args) { var log = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Debug)); Log.SetLoggerFactory(log); Console.WriteLine("Starting Node2"); var system = new ActorSystem(); var serialization = new Serialization(); var context = new RootContext(system); serialization.RegisterFileDescriptor(ProtosReflection.Descriptor); var props = Props.FromFunc( ctx => { switch (ctx.Message) { case HelloRequest _: ctx.Respond(new HelloResponse { Message = "Hello from node 2" }); break; } return(Task.CompletedTask); } ); var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost(12001) .WithProtoMessages(ProtosReflection.Descriptor); var clusterConfig = ClusterConfig.Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")), new PartitionIdentityLookup() ).WithClusterKind("HelloKind", props); system .WithRemote(remoteConfig) .WithCluster(clusterConfig); // CONSUL await system .Cluster() .StartMemberAsync(); await Task.Delay(-1); Console.WriteLine("Shutting Down..."); await system .Cluster() .ShutdownAsync(); }
public void CanRegisterKind() { var props = new Props(); var kind = Guid.NewGuid().ToString(); var remote = new GrpcCoreRemote(new ActorSystem(), GrpcCoreRemoteConfig.BindToLocalhost() .WithRemoteKinds((kind, props)) ); Assert.Equal(props, remote.Config.GetRemoteKind(kind)); }
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); }
static async Task Main(string[] args) { var system = new ActorSystem(); var context = new RootContext(system); //Registering "knownTypes" is not required, but improves performance as those messages //do not need to pass any typename manifest var remoteConfig = GrpcCoreRemoteConfig.BindToLocalhost(12001); system.WithRemote(remoteConfig); var wire = new WireSerializer(new[] { typeof(Ping), typeof(Pong), typeof(StartRemote), typeof(Start) }); system.Serialization().RegisterSerializer(wire, true); await system.Remote().StartAsync(); var messageCount = 1000000; var wg = new AutoResetEvent(false); var props = Props.FromProducer(() => new LocalActor(0, messageCount, wg)); var pid = context.Spawn(props); var remotePid = new PID("127.0.0.1:12000", "remote"); context.RequestAsync <Start>(remotePid, new StartRemote { Sender = pid }).Wait(); var start = DateTime.Now; Console.WriteLine("Starting to send"); var msg = new Ping(); for (var i = 0; i < messageCount; i++) { context.Send(remotePid, msg); } wg.WaitOne(); var elapsed = DateTime.Now - start; Console.WriteLine("Elapsed {0}", elapsed); var t = messageCount * 2.0 / elapsed.TotalMilliseconds * 1000; Console.WriteLine("Throughput {0} msg / sec", t); Console.ReadLine(); }
static async Task Main(string[] args) { var log = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Debug)); Log.SetLoggerFactory(log); Console.WriteLine("Starting Node1"); var system = new ActorSystem(); var context = new RootContext(system); var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost(12000) .WithProtoMessages(ProtosReflection.Descriptor); var clusterConfig = ClusterConfig.Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")), new PartitionIdentityLookup() ); system .WithRemote(remoteConfig) .WithCluster(clusterConfig); // CONSUL await system .Cluster() .StartMemberAsync(); var i = 10000; while (i-- > 0) { var res = await system .Cluster() .RequestAsync <HelloResponse>("TheName", "HelloKind", new HelloRequest(), CancellationToken.None); Console.WriteLine(res.Message); await Task.Delay(500); } await Task.Delay(-1); Console.WriteLine("Shutting Down..."); await system.Cluster().ShutdownAsync(); }
public void CanRegisterMultipleKinds() { var props = new Props(); var kind1 = Guid.NewGuid().ToString(); var kind2 = Guid.NewGuid().ToString(); var remote = new GrpcCoreRemote(new ActorSystem(), GrpcCoreRemoteConfig .BindToLocalhost() .WithRemoteKinds( (kind1, props), (kind2, props) ) ); var kinds = remote.Config.GetRemoteKinds(); Assert.Contains(kind1, kinds); Assert.Contains(kind2, kinds); }
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() { var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(ProtosReflection.Descriptor); var consulProvider = new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")); var clusterConfig = ClusterConfig .Setup("MyCluster", consulProvider, new PartitionIdentityLookup()); var system = new ActorSystem() .WithRemote(remoteConfig) .WithCluster(clusterConfig); await system .Cluster() .StartMemberAsync(); //subscribe to the eventstream via type, just like you do locally system.EventStream.SubscribeToTopic <SomeMessage>("MyTopic.*", x => Console.WriteLine($"Got message for {x.Name}")); //publish messages onto the eventstream on Subtopic1 on MyTopic root //but do this using cluster broadcasting. this will publish the event //to the event stream on all the members in the cluster //this is best effort only, if some member is unavailable, there is no guarantee associated here system.Cluster().BroadcastEvent(new SomeMessage("ProtoActor", "MyTopic.Subtopic1")); //this message is published on a topic that is not subscribed to, and nothing will happen system.Cluster().BroadcastEvent(new SomeMessage("Asynkron", "AnotherTopic")); //send a message to the same root topic, but another child topic system.Cluster().BroadcastEvent(new SomeMessage("Do we get this?", "MyTopic.Subtopic1")); //this example is local only. //see ClusterEventStream for cluster broadcast onto the eventstream Console.ReadLine(); }
public GrpcCoreChannelProvider(GrpcCoreRemoteConfig remoteConfig) => _remoteConfig = remoteConfig;
public static void Run() { var l = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Information)); Log.SetLoggerFactory(l); var config = ActorSystemConfig.Setup().WithMetricsProviders(new PrometheusConfigurator()); var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(MessagesReflection.Descriptor); var clusterConfig = ClusterConfig .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://127.0.0.1:8500/")), new PartitionIdentityLookup() ); var system = new ActorSystem(config) .WithRemote(remoteConfig) .WithCluster(clusterConfig); system .Cluster() .StartMemberAsync(); var props = Props.FromProducer(() => new MyActor()); var config2 = ActorSystemConfig.Setup().WithMetricsProviders(new PrometheusConfigurator()); var remoteConfig2 = GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(MessagesReflection.Descriptor); var clusterConfig2 = ClusterConfig .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://127.0.0.1:8500/")), new PartitionIdentityLookup() ) .WithClusterKind("somekind", props); var system2 = new ActorSystem(config2) .WithRemote(remoteConfig2) .WithCluster(clusterConfig2); system2 .Cluster() .StartMemberAsync(); _ = SafeTask.Run(async() => { var r = new Random(); while (true) { await Task.Delay(r.Next(1, 2000)); await system.Cluster().RequestAsync <SomeResponse>($"someactor{r.Next(1, 100)}", "somekind", new SomeRequest(), CancellationTokens.WithTimeout(5000) ); } } ); }
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(); }
public static ActorSystem WithRemote(this ActorSystem system, GrpcCoreRemoteConfig remoteConfig) { _ = new GrpcCoreRemote(system, remoteConfig); return(system); }
public static GrpcCoreRemoteConfig WithChannelOptions(this GrpcCoreRemoteConfig remoteConfig, IEnumerable <ChannelOption> options) => remoteConfig with
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);
public void UnknownKindThrowsException() { var remote = new GrpcCoreRemote(new ActorSystem(), GrpcCoreRemoteConfig.BindToLocalhost()); Assert.Throws <ArgumentException>(() => { remote.Config.GetRemoteKind("not registered"); }); }
private static GrpcCoreRemoteConfig GetRemoteConfig() => GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(ClusterPubSub.ProtosReflection.Descriptor);