-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (36 loc) · 1.3 KB
/
Copy pathProgram.cs
File metadata and controls
54 lines (36 loc) · 1.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
using SquidStd.Actors;
await using var counter = new CounterActor();
#region step-2
// Fire-and-forget messages: TellAsync enqueues without awaiting a reply.
await counter.TellAsync(new Increment(5));
await counter.TellAsync(new Increment(3));
#endregion
#region step-3
// Request/response: AskAsync enqueues a request and awaits its typed reply.
var total = await counter.AskAsync<GetTotal, int>(new());
Console.WriteLine($"Total: {total}");
#endregion
#region step-1
// The message contract: a marker interface, a fire-and-forget message, and an ask request.
internal interface ICounterMessage;
internal sealed record Increment(int By) : ICounterMessage;
internal sealed record GetTotal : ActorRequest<int>, ICounterMessage;
// A single-consumer actor: state is mutated without locks inside ReceiveAsync.
internal sealed class CounterActor : Actor<ICounterMessage>
{
private int _total;
protected override ValueTask ReceiveAsync(ICounterMessage message, CancellationToken cancellationToken)
{
switch (message)
{
case Increment increment:
_total += increment.By;
break;
case GetTotal request:
request.Reply(_total);
break;
}
return ValueTask.CompletedTask;
}
}
#endregion