11 |
11 |
public sealed class ApiClient : IApiClient {
|
12 |
12 |
|
13 |
13 |
public IHttpClient _client;
|
14 |
|
public IPersistentQueue _cache;
|
15 |
|
|
|
14 |
|
|
15 |
public bool ClientRunning;
|
|
16 |
|
16 |
17 |
private readonly uint _retryPeriod;
|
17 |
18 |
private readonly uint _maxEntries;
|
18 |
19 |
private readonly uint _maxRetries;
|
19 |
|
|
20 |
|
public ApiClient(string url, uint port, string path, uint retryPeriod, uint maxEntries, uint maxRetries, string cacheFilename) {
|
|
20 |
private readonly IPersistentQueue _cache;
|
|
21 |
|
|
22 |
public ApiClient(string url, uint port, string path, uint retryPeriod, uint maxEntries, uint maxRetries, IPersistentQueue cache) {
|
21 |
23 |
var uri = $"{url}:{port}{path}";
|
22 |
24 |
_retryPeriod = retryPeriod;
|
23 |
25 |
_maxEntries = maxEntries;
|
24 |
26 |
_maxRetries = maxRetries;
|
25 |
27 |
|
26 |
28 |
_client = new HttpClient(uri);
|
27 |
|
_cache = new PersistentQueue(cacheFilename);
|
|
29 |
_cache = cache;
|
28 |
30 |
}
|
29 |
31 |
|
30 |
32 |
public async Task SendPayloadAsync(Payload payload) {
|
... | ... | |
59 |
61 |
private async Task ResendPayloadsAsync() {
|
60 |
62 |
var numberOfPayloadsToResend = Math.Min(_maxRetries, _cache.EstimatedCountOfItemsInQueue);
|
61 |
63 |
var payloads = new List<Payload>();
|
62 |
|
|
63 |
|
using (var session = _cache.OpenSession()) {
|
|
64 |
if (numberOfPayloadsToResend > 0) {
|
|
65 |
using var session = _cache.OpenSession();
|
64 |
66 |
for (var i = 0; i < numberOfPayloadsToResend; i++) {
|
65 |
67 |
var rawBytes = session.Dequeue();
|
66 |
68 |
var payload = JsonSerializer.Deserialize<Payload>(rawBytes);
|
... | ... | |
96 |
98 |
|
97 |
99 |
public async void Run() {
|
98 |
100 |
Program.DefaultLogger.Info("Api Client thread has started");
|
99 |
|
while (true) {
|
|
101 |
ClientRunning = true;
|
|
102 |
while (ClientRunning) {
|
100 |
103 |
await ResendPayloadsAsync();
|
101 |
104 |
Thread.Sleep((int) _retryPeriod);
|
102 |
105 |
}
|
103 |
|
// ReSharper disable once FunctionNeverReturns
|
104 |
106 |
}
|
105 |
107 |
}
|
106 |
108 |
}
|
re #9443 ApiClient refactoring for better testing