-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathProgram.cs
More file actions
128 lines (102 loc) · 3.3 KB
/
Program.cs
File metadata and controls
128 lines (102 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Proto;
using Proto.Cluster;
using Proto.Cluster.Partition;
using Proto.Cluster.PubSub;
using Proto.Cluster.Testing;
using Proto.Remote;
using Proto.Remote.GrpcNet;
using Proto.Remote;
using Proto.Timers;
namespace ClusterPubSub;
internal static class Program
{
private static async Task Main()
{
Log.SetLoggerFactory(LoggerFactory.Create(l =>
l.AddConsole().SetMinimumLevel(LogLevel.Information)
)
);
var system = GetSystem();
var cluster = system.Cluster();
await cluster.StartMemberAsync();
for (var i = 0; i < 3; i++)
{
await cluster.GetUserActor("user-" + i).Connect(CancellationToken.None);
}
Console.ReadKey();
await cluster.ShutdownAsync();
}
private static ActorSystem GetSystem() =>
new ActorSystem()
.WithRemote(GetRemoteConfig())
.WithCluster(GetClusterConfig());
private static RemoteConfig GetRemoteConfig() =>
RemoteConfig
.BindToLocalhost()
.WithProtoMessages(ProtosReflection.Descriptor);
private static ClusterConfig GetClusterConfig()
{
var clusterConfig =
ClusterConfig
.Setup("MyCluster", new TestProvider(new TestProviderOptions(), new InMemAgent()),
new PartitionIdentityLookup())
.WithClusterKind(UserActorActor.Kind,
Props.FromProducer(() => new UserActorActor((c, _) => new User(c))));
return clusterConfig;
}
}
public class User : UserActorBase
{
private const string ChatTopic = "chat";
private static readonly string[] _messages =
{
"Good day sir!",
"Lovely weather, innit?",
"How do you do?",
"Pardon me!"
};
private CancellationTokenSource _schedule;
public User(IContext context) : base(context)
{
}
public override Task Connect()
{
_schedule = Context.Scheduler()
.SendRepeatedly(
TimeSpan.FromSeconds(Random.Shared.Next(2, 5)),
Context.Self,
new Tick());
return Context.Cluster().Subscribe(ChatTopic, Context.ClusterIdentity()!);
}
public override Task OnStopping()
{
_schedule.Cancel();
return Context.Cluster().Unsubscribe(ChatTopic, Context.ClusterIdentity()!);
}
public override Task OnReceive()
{
switch (Context.Message)
{
case Tick:
var message = _messages[Random.Shared.Next(0, _messages.Length)];
Console.WriteLine($"{Context.ClusterIdentity()!.Identity} publishes '{message}'");
_ = Context.Cluster()
.Publisher()
.Publish(ChatTopic, new ChatMessage
{
Sender = Context.ClusterIdentity()!.Identity,
Message = message
});
break;
case ChatMessage msg:
Console.WriteLine($"{Context.ClusterIdentity()!.Identity} received '{msg.Message}' from {msg.Sender}");
break;
}
return Task.CompletedTask;
}
}
internal record Tick;