From 709a0cd5e00df62c6f55a107a081fb48ff433967 Mon Sep 17 00:00:00 2001 From: samosvalishe Date: Fri, 3 Apr 2026 02:19:04 +0300 Subject: [PATCH] fix: watchdog debounce --- .../java/com/freeturn/app/ProxyService.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/freeturn/app/ProxyService.kt b/app/src/main/java/com/freeturn/app/ProxyService.kt index 4da18e7..1f92661 100644 --- a/app/src/main/java/com/freeturn/app/ProxyService.kt +++ b/app/src/main/java/com/freeturn/app/ProxyService.kt @@ -259,6 +259,8 @@ class ProxyService : Service() { // ── Network Handover ────────────────────────────────────────────────────── + private var networkDebounceJob: kotlinx.coroutines.Job? = null + private fun registerNetworkCallback() { networkInitialized = false val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager @@ -268,12 +270,17 @@ class ProxyService : Service() { networkInitialized = true return } - if (!userStopped.get() && process.get() != null) { - addLog("=== СМЕНА СЕТИ — ПЕРЕЗАПУСК ===") - updateNotification("VK TURN Proxy", "Смена сети, переподключение...") - restartCount = 0 - // P2-5: destroyForcibly() - process.get()?.destroyForcibly() + + // Дебаунс: отменяем предыдущий ждущий перезапуск, если он был + networkDebounceJob?.cancel() + networkDebounceJob = serviceScope.launch { + kotlinx.coroutines.delay(2000) + if (!userStopped.get() && process.get() != null) { + addLog("=== СМЕНА СЕТИ — ПЕРЕЗАПУСК ===") + updateNotification("VK TURN Proxy", "Смена сети, переподключение...") + restartCount = 0 + process.get()?.destroyForcibly() + } } } }