-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handler.cs
80 lines (64 loc) · 1.54 KB
/
Handler.cs
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
using Microsoft.Extensions.Logging;
namespace Task8
{
interface IHandler
{
TimeSpan Timeout { get; }
Task PerformOperation(CancellationToken cancellationToken);
}
class Handler : IHandler
{
private readonly IConsumer _consumer;
private readonly IPublisher _publisher;
private readonly ILogger<Handler> _logger;
public TimeSpan Timeout { get; }
public Handler(TimeSpan timeout, IConsumer consumer, IPublisher publisher, ILogger<Handler> logger)
{
Timeout = timeout;
_consumer = consumer;
_publisher = publisher;
_logger = logger;
}
public async Task PerformOperation(CancellationToken cancellationToken)
{
do
{
var data = await _consumer.ReadData();
if (data == null)
return;
var payload = data.Payload;
foreach (var recipient in data.Recipients)
{
var tr = Task.Run(async () =>
{
bool finished = false;
while (!finished)
{
var res = await _publisher.SendData(recipient, payload);
if (res == SendResult.Rejected)
await Task.Delay(Timeout, cancellationToken);
else
finished = true;
}
});
}
} while (!cancellationToken.IsCancellationRequested);
}
}
record Payload(string Origin, byte[] Data);
record Address(string DataCenter, string NodeId);
record Event(IReadOnlyCollection<Address> Recipients, Payload Payload);
enum SendResult
{
Accepted,
Rejected
}
interface IConsumer
{
Task<Event> ReadData();
}
interface IPublisher
{
Task<SendResult> SendData(Address address, Payload payload);
}
}