Skip to content

Commit a8ef70e

Browse files
committed
refactor(sync): remove historical net connectivity dependency
1 parent a0c78e4 commit a8ef70e

8 files changed

Lines changed: 129 additions & 118 deletions

‎CMakeLists.txt‎

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
# - vix_sync : The actual library target (STATIC or INTERFACE)
1919
# - vix::sync : Namespaced alias for consumers
2020
#
21-
# Public Dependencies:
22-
# - vix::net
23-
#
2421
# Options:
2522
# - VIX_ENABLE_SANITIZERS : Inherit sanitizers from the parent project
2623
#
@@ -41,31 +38,6 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
4138
# Sources discovery
4239
file(GLOB_RECURSE SYNC_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
4340

44-
# Net dependency
45-
option(VIX_SYNC_FETCH_NET "Auto-fetch vix::net if missing" ON)
46-
47-
if (NOT TARGET vix::net)
48-
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/../net/CMakeLists.txt")
49-
message(STATUS "[sync] Adding net from umbrella: ../net")
50-
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../net" "net")
51-
elseif (VIX_SYNC_FETCH_NET)
52-
include(FetchContent)
53-
message(STATUS "[sync] Fetching vix::net via FetchContent")
54-
FetchContent_Declare(vix_net
55-
GIT_REPOSITORY https://github.com/vixcpp/net.git
56-
GIT_TAG dev
57-
)
58-
FetchContent_MakeAvailable(vix_net)
59-
else()
60-
message(FATAL_ERROR "vix::net not found. Enable VIX_SYNC_FETCH_NET=ON or provide the target before sync.")
61-
endif()
62-
endif()
63-
64-
set(VIX_NET_TARGET vix::net)
65-
if (NOT TARGET ${VIX_NET_TARGET} AND TARGET vix::net)
66-
set(VIX_NET_TARGET vix::net)
67-
endif()
68-
6941
# JSON dependency (needed by FileOutboxStore.cpp via <vix/json/...>)
7042
option(VIX_SYNC_FETCH_JSON "Auto-fetch vix::json if missing" ON)
7143

@@ -132,7 +104,6 @@ endif()
132104

133105
target_link_libraries(vix_sync
134106
PUBLIC
135-
${VIX_NET_TARGET}
136107
${VIX_JSON_TARGET}
137108
)
138109

@@ -172,7 +143,6 @@ else()
172143
)
173144

174145
target_link_libraries(vix_sync INTERFACE
175-
${VIX_NET_TARGET}
176146
${VIX_JSON_TARGET}
177147
)
178148

@@ -201,7 +171,6 @@ if (SYNC_SOURCES)
201171
else()
202172
message(STATUS "Mode: HEADER-ONLY / no sources")
203173
endif()
204-
message(STATUS "Net target: ${VIX_NET_TARGET}")
205174
message(STATUS "Include dir: ${CMAKE_CURRENT_SOURCE_DIR}/include (for <vix/...>)")
206175
message(STATUS "Sanitizers (inherited): ${VIX_ENABLE_SANITIZERS}")
207176
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

‎include/vix/sync/engine/SyncEngine.hpp‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
#include <atomic>
2020
#include <cstdint>
21+
#include <functional>
2122
#include <memory>
2223
#include <thread>
2324
#include <vector>
2425

25-
#include <vix/net/NetworkProbe.hpp>
2626
#include <vix/sync/engine/SyncWorker.hpp>
2727
#include <vix/sync/outbox/Outbox.hpp>
2828

@@ -34,7 +34,7 @@ namespace vix::sync::engine
3434
* SyncEngine is responsible for running the main synchronization control loop:
3535
* - Spawns and owns a set of SyncWorker instances
3636
* - Periodically ticks workers to pull operations from the Outbox
37-
* - Uses NetworkProbe to adapt behavior when offline/online
37+
* - Applies configured send permission before delivery attempts
3838
* - Delegates actual I/O to an ISyncTransport implementation
3939
*
4040
* The engine can be driven manually via tick() (single-threaded integration),
@@ -65,10 +65,22 @@ namespace vix::sync::engine
6565
std::int64_t idle_sleep_ms{250};
6666

6767
/**
68-
* @brief Sleep duration when network is considered offline.
68+
* @brief Sleep duration when delivery permission is denied.
6969
*/
7070
std::int64_t offline_sleep_ms{500};
7171

72+
/**
73+
* @brief Optional policy deciding whether remote delivery may proceed.
74+
*
75+
* When unset, Sync permits delivery. Exceptions propagate from tick().
76+
*/
77+
std::function<bool()> send_permission{};
78+
79+
/**
80+
* @brief Minimum delay between shared permission evaluations.
81+
*/
82+
std::int64_t send_permission_min_interval_ms{1000};
83+
7284
/**
7385
* @brief Maximum number of operations to pull per batch.
7486
*/
@@ -88,13 +100,11 @@ namespace vix::sync::engine
88100
*
89101
* @param cfg Engine configuration.
90102
* @param outbox Shared Outbox containing pending operations to sync.
91-
* @param probe Network probe used to detect offline/online state.
92103
* @param transport Transport used to send/receive sync payloads.
93104
*/
94105
SyncEngine(
95106
Config cfg,
96107
std::shared_ptr<vix::sync::outbox::Outbox> outbox,
97-
std::shared_ptr<vix::net::NetworkProbe> probe,
98108
std::shared_ptr<ISyncTransport> transport);
99109

100110
/**
@@ -160,9 +170,9 @@ namespace vix::sync::engine
160170
std::shared_ptr<vix::sync::outbox::Outbox> outbox_;
161171

162172
/**
163-
* @brief Network probe used to detect connectivity changes.
173+
* @brief Shared, rate-limited delivery permission used by every worker.
164174
*/
165-
std::shared_ptr<vix::net::NetworkProbe> probe_;
175+
std::function<bool(std::int64_t)> send_permission_;
166176

167177
/**
168178
* @brief Transport used by workers to communicate with remote peers/edge.

‎include/vix/sync/engine/SyncWorker.hpp‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <memory>
2222
#include <string>
2323

24-
#include <vix/net/NetworkProbe.hpp>
2524
#include <vix/sync/Operation.hpp>
2625
#include <vix/sync/outbox/Outbox.hpp>
2726

@@ -84,7 +83,7 @@ namespace vix::sync::engine
8483
* @brief Single-worker unit that processes ready operations from the Outbox.
8584
*
8685
* SyncWorker:
87-
* - Consults NetworkProbe to decide if sending should proceed
86+
* - Consults configured send permission before delivery attempts
8887
* - Pulls a batch of ready operations from the Outbox
8988
* - Sends operations through ISyncTransport
9089
* - Applies retry/backoff decisions by updating Outbox state (implementation-defined)
@@ -117,7 +116,7 @@ namespace vix::sync::engine
117116
std::int64_t idle_sleep_ms{250};
118117

119118
/**
120-
* @brief Sleep duration when network is considered offline.
119+
* @brief Sleep duration when delivery permission is denied.
121120
*
122121
* This is mainly used by higher-level orchestrators (SyncEngine).
123122
*/
@@ -137,13 +136,13 @@ namespace vix::sync::engine
137136
*
138137
* @param cfg Worker configuration.
139138
* @param outbox Shared outbox used to fetch and update operations.
140-
* @param probe Network probe used to detect offline/online state.
139+
* @param send_permission Optional policy deciding whether sending proceeds.
141140
* @param transport Transport used to deliver operations.
142141
*/
143142
SyncWorker(
144143
Config cfg,
145144
std::shared_ptr<vix::sync::outbox::Outbox> outbox,
146-
std::shared_ptr<vix::net::NetworkProbe> probe,
145+
std::function<bool(std::int64_t)> send_permission,
147146
std::shared_ptr<ISyncTransport> transport);
148147

149148
/**
@@ -161,7 +160,7 @@ namespace vix::sync::engine
161160
/**
162161
* @brief Decide whether the worker should attempt sending right now.
163162
*
164-
* Typically checks connectivity (NetworkProbe) and possibly Outbox state.
163+
* Applies the configured delivery permission policy.
165164
*
166165
* @param now_ms Current monotonic time in milliseconds.
167166
* @return true if sending should proceed, false otherwise.
@@ -191,9 +190,9 @@ namespace vix::sync::engine
191190
std::shared_ptr<vix::sync::outbox::Outbox> outbox_;
192191

193192
/**
194-
* @brief Network probe used to detect connectivity.
193+
* @brief Optional policy deciding whether sending proceeds at a given time.
195194
*/
196-
std::shared_ptr<vix::net::NetworkProbe> probe_;
195+
std::function<bool(std::int64_t)> send_permission_;
197196

198197
/**
199198
* @brief Transport used to send operations.

‎src/engine/SyncEngine.cpp‎

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,55 @@
1414
#include <vix/sync/engine/SyncEngine.hpp>
1515

1616
#include <chrono>
17+
#include <functional>
18+
#include <memory>
19+
#include <utility>
1720

1821
namespace vix::sync::engine
1922
{
2023

24+
namespace
25+
{
26+
class SendGate
27+
{
28+
public:
29+
SendGate(
30+
std::function<bool()> permission,
31+
std::int64_t minimum_interval_ms)
32+
: permission_(std::move(permission)),
33+
minimum_interval_ms_(minimum_interval_ms >= 0
34+
? minimum_interval_ms
35+
: 1000)
36+
{
37+
}
38+
39+
[[nodiscard]] bool allows_send(std::int64_t now_ms)
40+
{
41+
if (!permission_)
42+
{
43+
return true;
44+
}
45+
46+
if (!initialized_ || now_ms - last_evaluation_ms_ < 0 ||
47+
now_ms - last_evaluation_ms_ >= minimum_interval_ms_)
48+
{
49+
last_allowed_ = permission_();
50+
last_evaluation_ms_ = now_ms;
51+
initialized_ = true;
52+
}
53+
54+
return last_allowed_;
55+
}
56+
57+
private:
58+
std::function<bool()> permission_;
59+
std::int64_t minimum_interval_ms_{1000};
60+
bool last_allowed_{true};
61+
std::int64_t last_evaluation_ms_{0};
62+
bool initialized_{false};
63+
};
64+
} // namespace
65+
2166
static std::int64_t now_ms()
2267
{
2368
using namespace std::chrono;
@@ -27,13 +72,19 @@ namespace vix::sync::engine
2772
SyncEngine::SyncEngine(
2873
Config cfg,
2974
std::shared_ptr<vix::sync::outbox::Outbox> outbox,
30-
std::shared_ptr<vix::net::NetworkProbe> probe,
3175
std::shared_ptr<ISyncTransport> transport)
32-
: cfg_(cfg),
76+
: cfg_(std::move(cfg)),
3377
outbox_(std::move(outbox)),
34-
probe_(std::move(probe)),
3578
transport_(std::move(transport))
3679
{
80+
auto send_gate = std::make_shared<SendGate>(
81+
cfg_.send_permission,
82+
cfg_.send_permission_min_interval_ms);
83+
send_permission_ = [send_gate](std::int64_t now_ms)
84+
{
85+
return send_gate->allows_send(now_ms);
86+
};
87+
3788
workers_.reserve(cfg_.worker_count);
3889
for (std::size_t i = 0; i < cfg_.worker_count; ++i)
3990
{
@@ -43,7 +94,11 @@ namespace vix::sync::engine
4394
wc.offline_sleep_ms = cfg_.offline_sleep_ms;
4495
wc.inflight_timeout_ms = cfg_.inflight_timeout_ms;
4596

46-
workers_.push_back(std::make_unique<SyncWorker>(wc, outbox_, probe_, transport_));
97+
workers_.push_back(std::make_unique<SyncWorker>(
98+
wc,
99+
outbox_,
100+
send_permission_,
101+
transport_));
47102
}
48103
}
49104

‎src/engine/SyncWorker.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@ namespace vix::sync::engine
1919
SyncWorker::SyncWorker(
2020
Config cfg,
2121
std::shared_ptr<vix::sync::outbox::Outbox> outbox,
22-
std::shared_ptr<vix::net::NetworkProbe> probe,
22+
std::function<bool(std::int64_t)> send_permission,
2323
std::shared_ptr<ISyncTransport> transport)
2424
: cfg_(cfg),
2525
outbox_(std::move(outbox)),
26-
probe_(std::move(probe)),
26+
send_permission_(std::move(send_permission)),
2727
transport_(std::move(transport))
2828
{
2929
}
3030

3131
bool SyncWorker::should_send_(std::int64_t now_ms)
3232
{
33-
const bool online = probe_ ? probe_->refresh(now_ms) : true;
34-
return online;
33+
return send_permission_ ? send_permission_(now_ms) : true;
3534
}
3635

3736
std::size_t SyncWorker::process_ready_(std::int64_t now_ms)

‎tests/sync_engine_inflight_timeout_test.cpp‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <iostream>
1818
#include <memory>
1919

20-
#include <vix/net/NetworkProbe.hpp>
2120
#include <vix/sync/outbox/Outbox.hpp>
2221
#include <vix/sync/outbox/FileOutboxStore.hpp>
2322
#include <vix/sync/engine/SyncEngine.hpp>
@@ -58,13 +57,7 @@ int main()
5857
},
5958
store);
6059

61-
// 2) Probe: always online
62-
auto probe = std::make_shared<vix::net::NetworkProbe>(
63-
vix::net::NetworkProbe::Config{},
64-
[]
65-
{ return true; });
66-
67-
// 3) Transport: success
60+
// 2) Transport: success
6861
auto transport = std::make_shared<FakeHttpTransport>();
6962
transport->setDefault({.ok = true});
7063

@@ -75,8 +68,9 @@ int main()
7568
ecfg.idle_sleep_ms = 0;
7669
ecfg.offline_sleep_ms = 0;
7770
ecfg.inflight_timeout_ms = 50; // 50ms for test
71+
ecfg.send_permission = [] { return true; };
7872

79-
SyncEngine engine(ecfg, outbox, probe, transport);
73+
SyncEngine engine(ecfg, outbox, transport);
8074

8175
// 5) Enqueue op
8276
Operation op;

‎tests/sync_engine_outbox_permanent_fail_test.cpp‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <iostream>
1818
#include <memory>
1919

20-
#include <vix/net/NetworkProbe.hpp>
2120
#include <vix/sync/outbox/Outbox.hpp>
2221
#include <vix/sync/outbox/FileOutboxStore.hpp>
2322
#include <vix/sync/engine/SyncEngine.hpp>
@@ -58,13 +57,7 @@ int main()
5857
},
5958
store);
6059

61-
// 2) Network probe: always online
62-
auto probe = std::make_shared<vix::net::NetworkProbe>(
63-
vix::net::NetworkProbe::Config{},
64-
[]
65-
{ return true; });
66-
67-
// 3) Fake transport: permanent failure on this target
60+
// 2) Fake transport: permanent failure on this target
6861
auto transport = std::make_shared<FakeHttpTransport>();
6962
transport->setRuleForTarget(
7063
"/api/messages",
@@ -79,8 +72,9 @@ int main()
7972
ecfg.batch_limit = 10;
8073
ecfg.idle_sleep_ms = 0;
8174
ecfg.offline_sleep_ms = 0;
75+
ecfg.send_permission = [] { return true; };
8276

83-
SyncEngine engine(ecfg, outbox, probe, transport);
77+
SyncEngine engine(ecfg, outbox, transport);
8478

8579
// 5) Enqueue operation
8680
Operation op;

0 commit comments

Comments
 (0)