-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedOutbox.cs
More file actions
37 lines (31 loc) · 1.01 KB
/
Copy pathDistributedOutbox.cs
File metadata and controls
37 lines (31 loc) · 1.01 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
using CSharpMastery.FraudEngine.Models;
using System.Collections.Concurrent;
namespace CSharpMastery.FraudEngine.Distributed;
public readonly record struct OutboxMessage(
Guid MessageId,
string Topic,
string PayloadJson,
DateTime CreatedAtUtc
);
public sealed class DistributedOutbox
{
private readonly ConcurrentQueue<OutboxMessage> _pendingMessages = new();
public void SaveToOutbox(Transaction<decimal> tx, string topic)
{
var message = new OutboxMessage(
Guid.NewGuid(),
topic,
$"{{\"id\":\"{tx.TransactionId}\",\"account\":{tx.AccountId},\"amount\":{tx.Amount}}}",
DateTime.UtcNow
);
_pendingMessages.Enqueue(message);
}
public async ValueTask DispatchPendingMessagesAsync(Func<OutboxMessage, ValueTask> brokerPublisher)
{
while (_pendingMessages.TryDequeue(out var message))
{
// Reliable delivery to network broker
await brokerPublisher(message);
}
}
}