-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncWorker.cpp
More file actions
100 lines (86 loc) · 1.99 KB
/
Copy pathSyncWorker.cpp
File metadata and controls
100 lines (86 loc) · 1.99 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
/**
*
* @file SyncWorker.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/sync/engine/SyncWorker.hpp>
namespace vix::sync::engine
{
SyncWorker::SyncWorker(
Config cfg,
std::shared_ptr<vix::sync::outbox::Outbox> outbox,
std::shared_ptr<vix::net::NetworkProbe> probe,
std::shared_ptr<ISyncTransport> transport)
: cfg_(cfg),
outbox_(std::move(outbox)),
probe_(std::move(probe)),
transport_(std::move(transport))
{
}
bool SyncWorker::should_send_(std::int64_t now_ms)
{
const bool online = probe_ ? probe_->refresh(now_ms) : true;
return online;
}
std::size_t SyncWorker::process_ready_(std::int64_t now_ms)
{
auto ops = outbox_->peek_ready(now_ms, cfg_.batch_limit);
if (ops.empty())
return 0;
std::size_t processed = 0;
for (const auto &op : ops)
{
if (!outbox_->claim(op.id, now_ms))
{
continue;
}
SendResult r;
if (transport_)
{
r = transport_->send(op);
}
else
{
r.ok = false;
r.retryable = true;
r.error = "No transport configured";
}
if (r.ok)
{
outbox_->complete(op.id, now_ms);
}
else
{
outbox_->fail(
op.id,
r.error.empty() ? "send failed" : r.error,
now_ms,
/*retryable=*/r.retryable);
}
++processed;
}
return processed;
}
std::size_t SyncWorker::tick(std::int64_t now_ms)
{
if (!outbox_)
return 0;
if (auto store = outbox_->store())
{
store->requeue_inflight_older_than(
now_ms,
cfg_.inflight_timeout_ms);
}
if (!should_send_(now_ms))
return 0;
return process_ready_(now_ms);
}
} // namespace vix::sync::engine