android: standby duty-cycle в фоне — defer ping/reconnect/probe через standby_wait

standby.h/c: duty-cycle ACTIVE/SLEEP, standby_wait() будит в burst сразу,
в SLEEP — до следующего burst; notify_network_activity для раннего пробуждения.
Интеграция (по флагу UTUN_HAVE_STANDBY, десктоп без изменений):
- conn_mgr: bg_ping/candidate_ping в SLEEP defer через standby_wait
- route_connectivity: probe_node откладывается (PROBE_STATUS_DEFERRED)
- topo_group_connect: пауза между попытками — активный режим 1с, фон standby_wait
- jni_bridge/ConfigProvider/NetworkActivityMonitor: set_active + сетевая активность
- chat_setting: standby_active_sec/sleep_sec/min_sleep_sec
+ юнит-тест test_standby (31 проверок)
This commit is contained in:
evgeny committed 2026-08-14 12:28:56 +03:00
1 parent 1c3ec5b9f3
commit dec16ff51e
26 files changed
+921 -14

No files matched your search

+7
View File
@@ -24,6 +24,9 @@
#include "../transport_layer/etcp_connect.h"
#include "route_ping.h"
#include "../chat/chat_core.h"
#ifdef UTUN_HAVE_STANDBY
#include "standby.h"
#endif
/* ═══════ forward-декларации ═══════ */
@@ -326,6 +329,10 @@ void conn_mgr_destroy(struct CONN_MGR* mgr) {
DEBUG_INFO(DEBUG_CATEGORY_SYS, "[CM_DESTROY] 1 bg_ping done");
if (mgr->candidate_ping_timer) { uasync_cancel_timeout(mgr->instance->ua, mgr->candidate_ping_timer); mgr->candidate_ping_timer = NULL; }
DEBUG_INFO(DEBUG_CATEGORY_SYS, "[CM_DESTROY] 2 cand_ping done");
#ifdef UTUN_HAVE_STANDBY
if (mgr->bg_ping_wait) { standby_wait_cancel(mgr->bg_ping_wait); mgr->bg_ping_wait = NULL; }
if (mgr->candidate_ping_wait) { standby_wait_cancel(mgr->candidate_ping_wait); mgr->candidate_ping_wait = NULL; }
#endif
{ size_t ec = queue_entry_count(mgr->entries);
DEBUG_INFO(DEBUG_CATEGORY_SYS, "[CM_DESTROY] 3 entries count=%zu", ec);
struct ll_entry* e = mgr->entries->head;
+17
View File
@@ -13,6 +13,9 @@
#include "topo_group.h"
#include "route_connectivity.h"
#include "route_ping.h"
#ifdef UTUN_HAVE_STANDBY
#include "standby.h"
#endif
/* ═══════ probe time утилиты ═══════ */
@@ -104,6 +107,13 @@ static void cm_bg_ping_to_node(struct CONN_MGR* mgr, struct TOPO_NODE* ni) {
* и INDIRECT-фаза не может выбрать посредника. */
void cm_bg_ping_timer_cb(void* arg) {
struct CONN_MGR* mgr = (struct CONN_MGR*)arg;
#ifdef UTUN_HAVE_STANDBY
if (standby_get_sleep_tb() > 0) {
mgr->bg_ping_wait = standby_wait(mgr, cm_bg_ping_timer_cb);
return;
}
mgr->bg_ping_wait = NULL;
#endif
struct TOPO_GROUP* group = mgr->group;
if (!group || !group->nodes) goto rearm;
size_t total = queue_entry_count(group->nodes);
@@ -150,6 +160,13 @@ static int cm_has_active_conn(struct TOPO_GROUP* g, uint64_t nid) {
* Вызывается раз в ~2с, самоперезапускается. */
void cm_candidate_ping_timer_cb(void* arg) {
struct CONN_MGR* mgr = (struct CONN_MGR*)arg; uint64_t now = get_time_tb();
#ifdef UTUN_HAVE_STANDBY
if (standby_get_sleep_tb() > 0) {
mgr->candidate_ping_wait = standby_wait(mgr, cm_candidate_ping_timer_cb);
return;
}
mgr->candidate_ping_wait = NULL;
#endif
for (uint8_t i = 0; i < mgr->best_candidate_count; i++) {
uint64_t nid = mgr->best_candidates[i].node_id;
struct TOPO_GROUP_NODE* nq = topo_node_find_by_id(mgr->group, nid);
+1
View File
@@ -164,6 +164,7 @@ struct CONN_MGR {
struct UTUN_INSTANCE* instance; struct TOPO_GROUP* group;
struct ll_queue* entries;
void *bg_ping_timer, *candidate_ping_timer;
void *bg_ping_wait, *candidate_ping_wait; /* standby_wait handle (Android) */
size_t bg_ping_cursor; uint64_t bg_ping_cycle_start_tb;
struct CONN_MGR_CANDIDATE best_candidates[CONN_MGR_MAX_CANDIDATES]; uint8_t best_candidate_count;
uint32_t next_request_id, direct_timeout_ms; uint8_t initialized;
+56
View File
@@ -17,6 +17,9 @@
#include "topo_group.h"
#include "route_connectivity.h"
#include "topo_node_sqlite.h"
#ifdef UTUN_HAVE_STANDBY
#include "standby.h"
#endif
#define CONN_MAX_SOCKET_CANDIDATES 8
@@ -293,11 +296,46 @@ static void conn_probe_finish(struct conn_probe_ctx* ctx, int ok) {
// ---- public API ----
#ifdef UTUN_HAVE_STANDBY
/* Контекст пробы, отложенной до следующего активного интервала (standby_wait). */
struct route_probe_defer {
struct UTUN_INSTANCE* instance;
struct TOPO_GROUP* group;
struct TOPO_GROUP_NODE* nq;
void* wait_handle; /* handle standby_wait для отмены */
};
static void route_connectivity_probe_deferred_cb(void* arg) {
struct route_probe_defer* d = (struct route_probe_defer*)arg;
struct TOPO_GROUP_NODE* nq = d->nq;
nq->connectivity.probe_deferred_wait = NULL;
nq->connectivity.probe_status = PROBE_STATUS_NONE;
route_connectivity_probe_node(d->instance, d->group, nq);
u_free(d);
}
#endif
void route_connectivity_probe_node(struct UTUN_INSTANCE* instance, struct TOPO_GROUP* group, struct TOPO_GROUP_NODE* nq) {
if (!instance || !nq) return;
struct TOPO_NODE* ni = topo_node_registry_find(instance->topo_groups, nq->node_id);
if (!ni) return;
if (nq->node_id == instance->node_id) return; // не пингуем себя
#ifdef UTUN_HAVE_STANDBY
if (nq->connectivity.probe_status == PROBE_STATUS_DEFERRED) {
DEBUG_INFO(DEBUG_CATEGORY_BGP, "probe already deferred for node %016llx", (unsigned long long)nq->node_id);
return;
}
if (standby_get_sleep_tb() > 0) {
nq->connectivity.probe_status = PROBE_STATUS_DEFERRED;
struct route_probe_defer* d = u_calloc(1, sizeof(*d));
if (!d) { nq->connectivity.probe_status = PROBE_STATUS_NONE; return; }
d->instance = instance; d->group = group; d->nq = nq;
void* h = standby_wait(d, route_connectivity_probe_deferred_cb);
if (h) { d->wait_handle = h; nq->connectivity.probe_deferred_wait = d; }
/* else: cb вызван синхронно (OOM), d уже освобождён внутри */
return;
}
#endif
if (nq->connectivity.probe_status == PROBE_STATUS_IN_PROGRESS) {
DEBUG_INFO(DEBUG_CATEGORY_BGP, "probe already in progress for node %016llx", (unsigned long long)nq->node_id);
return;
@@ -386,6 +424,15 @@ void route_connectivity_probe_node(struct UTUN_INSTANCE* instance, struct TOPO_G
void route_connectivity_cancel_node(struct UTUN_INSTANCE* instance, struct TOPO_GROUP_NODE* nq) {
if (!instance || !nq) return;
#ifdef UTUN_HAVE_STANDBY
{ struct route_probe_defer* d = (struct route_probe_defer*)nq->connectivity.probe_deferred_wait;
if (d) {
if (d->wait_handle) standby_wait_cancel(d->wait_handle);
u_free(d);
nq->connectivity.probe_deferred_wait = NULL;
}
}
#endif
nq->connectivity.probe_status = PROBE_STATUS_NONE;
nq->connectivity.pending_count = 0;
struct conn_probe_ctx* ctx = (struct conn_probe_ctx*)nq->connectivity.probe_list;
@@ -401,6 +448,15 @@ void route_connectivity_cancel_all(struct UTUN_INSTANCE* instance) {
struct ll_entry* e = group->nodes->head;
while (e) {
struct TOPO_GROUP_NODE* nq = (struct TOPO_GROUP_NODE*)e;
#ifdef UTUN_HAVE_STANDBY
{ struct route_probe_defer* d = (struct route_probe_defer*)nq->connectivity.probe_deferred_wait;
if (d) {
if (d->wait_handle) standby_wait_cancel(d->wait_handle);
u_free(d);
nq->connectivity.probe_deferred_wait = NULL;
}
}
#endif
nq->connectivity.probe_status = PROBE_STATUS_NONE;
nq->connectivity.pending_count = 0;
e = e->next;
+1
View File
@@ -262,6 +262,7 @@ static void topo_group_destroy(struct TOPO_GROUP* group) {
struct ll_entry* e; int nc = 0;
while ((e = queue_data_get(group->nodes)) != NULL) {
struct TOPO_GROUP_NODE* nq = (struct TOPO_GROUP_NODE*)e;
route_connectivity_cancel_node(group->instance, nq);
if (nq->paths) { queue_free(nq->paths); nq->paths = NULL; }
topo_nodeq_free_group_fields(group->instance->topo_groups, nq);
queue_entry_free(e); nc++;
+26 -10
View File
@@ -12,7 +12,8 @@
*
* После исчерпания Phase 3 → пауза → cycle_restart → Phase 1 (бесконечно).
*
* Пауза: 1с когда client_activity == ACTIVE, 30с когда STANDBY (Android фон).
* Пауза: активный режим — 1с; фоновый (Android standby) — standby_wait
* (burst → сразу, sleep → до следующего burst).
* При DOWN (active_conn_count → 0) — немедленный cycle_restart.
* При повторном topo_group_connect_init — destroy + fresh init.
*
@@ -32,6 +33,9 @@
#include "../lib/ll_queue.h"
#include "etcp.h"
#include "../chat/chat_event.h"
#ifdef UTUN_HAVE_STANDBY
#include "standby.h"
#endif
/* ─── внутренние константы ─── */
#define TGC_ID "topo_group_connect"
@@ -42,13 +46,13 @@
#define TGC_PHASE_DONE 3
#define TGC_PHASE_PAUSE 4
#define TGC_MAX_HANDLES 128
#define TGC_PAUSE_ACTIVE_TB 10000 /* 1s пауза когда client_activity == ACTIVE */
#define TGC_PAUSE_IDLE_TB 300000 /* 30s пауза когда STANDBY (Android фон) */
#define TGC_PAUSE_ACTIVE_TB 10000 /* 1s пауза в активном режиме */
struct TOPO_GROUP_CONNECT {
struct TOPO_GROUP* group;
void* phase_timer;
void* pause_timer;
void* pause_timer; /* uasync timeout handle (активный режим) */
void* pause_wait; /* standby_wait handle (фоновый режим, Android) */
uint8_t phase;
int pending;
int connected_count;
@@ -149,6 +153,9 @@ void topo_group_connect_destroy(struct TOPO_GROUP* group) {
group->connect = NULL;
if (gc->phase_timer) { uasync_cancel_timeout(group->instance->ua, gc->phase_timer); gc->phase_timer = NULL; }
if (gc->pause_timer) { uasync_cancel_timeout(group->instance->ua, gc->pause_timer); gc->pause_timer = NULL; }
#ifdef UTUN_HAVE_STANDBY
if (gc->pause_wait) { standby_wait_cancel(gc->pause_wait); gc->pause_wait = NULL; }
#endif
for (int i = 0; i < gc->handle_count; i++) conn_mgr_close(gc->handles[i]);
if (gc->cur_handle) { conn_mgr_close(gc->cur_handle); gc->cur_handle = NULL; }
u_free(gc->candidate_ids);
@@ -327,12 +334,19 @@ static void tgc_callback(struct CONN_MGR_HANDLE* h, uint64_t node_id, uint64_t g
*/
static void tgc_start_pause(struct TOPO_GROUP_CONNECT* gc, void (*cb)(void*), const char* label) {
gc->phase = TGC_PHASE_PAUSE;
int active = (gc->group->instance->client_activity == CLIENT_ACTIVITY_ACTIVE);
int delay_tb = active ? TGC_PAUSE_ACTIVE_TB : TGC_PAUSE_IDLE_TB;
gc->pause_timer = uasync_set_timeout(gc->group->instance->ua, delay_tb, gc, cb, label);
tgc_notify_connecting(gc, NULL, 0);
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: pause %dms (activity=%s) for ch=%s",
TGC_ID, delay_tb / 10, active ? "ACTIVE" : "STANDBY", gc->group->channel_id);
#ifdef UTUN_HAVE_STANDBY
if (standby_is_enabled()) {
/* фоновый режим: burst → standby_wait будит сразу, sleep → до следующего burst */
gc->pause_wait = standby_wait(gc, cb);
gc->pause_timer = NULL;
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: pause (standby) for ch=%s", TGC_ID, gc->group->channel_id);
return;
}
#endif
gc->pause_timer = uasync_set_timeout(gc->group->instance->ua, TGC_PAUSE_ACTIVE_TB, gc, cb, label);
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: pause %dms (active) for ch=%s",
TGC_ID, TGC_PAUSE_ACTIVE_TB / 10, gc->group->channel_id);
}
/* ═══════════════════════════════════════════════════════════════════════
@@ -393,6 +407,7 @@ static void tgc_phase1_timeout(void* arg) {
* Когда все перебраны — переходит к Phase 3.
*/
static void tgc_phase2_try_next(struct TOPO_GROUP_CONNECT* gc) {
gc->pause_timer = NULL; gc->pause_wait = NULL;
if (gc->phase == TGC_PHASE_PAUSE) gc->phase = TGC_PHASE_TWO;
if (gc->phase != TGC_PHASE_TWO) return;
while (1) {
@@ -439,6 +454,7 @@ static void tgc_phase2_try_next(struct TOPO_GROUP_CONNECT* gc) {
* пауза и полный перезапуск цикла с Phase 1 (бесконечно, пока не подключимся).
*/
static void tgc_phase3_try_next(struct TOPO_GROUP_CONNECT* gc) {
gc->pause_timer = NULL; gc->pause_wait = NULL;
if (gc->phase == TGC_PHASE_PAUSE) gc->phase = TGC_PHASE_THREE;
if (gc->phase != TGC_PHASE_THREE) return;
if (gc->candidate_count == 0) {
@@ -472,7 +488,7 @@ static void tgc_phase3_try_next(struct TOPO_GROUP_CONNECT* gc) {
* и после исчерпания Phase 3.
*/
static void tgc_cycle_restart(struct TOPO_GROUP_CONNECT* gc) {
gc->pause_timer = NULL;
gc->pause_timer = NULL; gc->pause_wait = NULL;
if (gc->phase_timer) { uasync_cancel_timeout(gc->group->instance->ua, gc->phase_timer); gc->phase_timer = NULL; }
for (int i = 0; i < gc->handle_count; i++) conn_mgr_close(gc->handles[i]);
if (gc->cur_handle) { conn_mgr_close(gc->cur_handle); gc->cur_handle = NULL; }
+2 -1
View File
@@ -11,7 +11,8 @@
* После каждого TIMEOUT — пауза перед следующей попыткой.
* После исчерпания Phase 3 → пауза → cycle_restart → Phase 1 (бесконечно).
*
* Пауза: 1с когда client_activity == ACTIVE, 30с когда STANDBY (Android фон).
* Пауза: активный режим — 1с; фоновый (Android standby) — standby_wait
* (burst → сразу, sleep → до следующего burst).
* При DOWN (active_conn_count → 0) — немедленный cycle_restart из любой фазы.
* При повторном вызове topo_group_connect_init — destroy + fresh init.
*
+2
View File
@@ -52,6 +52,7 @@ struct UTUN_INSTANCE;
#define PROBE_STATUS_NONE 0
#define PROBE_STATUS_IN_PROGRESS 1
#define PROBE_STATUS_DONE 2
#define PROBE_STATUS_DEFERRED 3 /* отложена до следующего burst (standby_wait) */
#define PROBE_RESULT_UNKNOWN 0
#define PROBE_RESULT_REACHABLE 1
@@ -99,6 +100,7 @@ struct TOPO_CONNECTIVITY {
uint64_t ping_req_time;
uint64_t last_ping_time;
void* probe_list;
void* probe_deferred_wait; /* контекст отложенной пробы (standby_wait, Android) */
};
#define PING_TTL_TB (86400ULL * 10000ULL)