Commit f40adaea authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Detect when the connection is healthy again

Implements #271
parent f4d2822b
......@@ -26,7 +26,8 @@ class ConnectionWatchdog(private val trafficStats: TrafficStats,
val debounceCallbackByMs:Long? = null,
val badLatencyThresholdMs:Int = 750,
val badPacketLossThresholdPercent:Int = 30,
private val onBadServerConnection:() -> Unit) {
private val onBadServerConnection:() -> Unit,
private val onBadConnectionResolved:() -> Unit) {
val supervisor = SupervisorJob()
val scope = CoroutineScope(supervisor + Dispatchers.IO)
private var running = true
......@@ -34,6 +35,7 @@ class ConnectionWatchdog(private val trafficStats: TrafficStats,
private var packetLossAtLastCheck:Int? = null
private var packetCountAtLastCheck:Int? = null
private var lastCallbackCall:Long? = null
private var measurementsWithBadConnection:Int = 0
init {
scope.launch {
......@@ -50,12 +52,18 @@ class ConnectionWatchdog(private val trafficStats: TrafficStats,
val currentLatency = trafficStats.floatingAverageLatency.toInt()
val currentPacketLossPercent = (100*trafficStats.failedAnswers)/(trafficStats.packetsReceivedFromDevice*0.9)
if(currentLatency > badLatencyThresholdMs*1.3 ||
val hasBadConnection = if(currentLatency > badLatencyThresholdMs*1.3 ||
(latencyAtLastCheck?.let { it > badLatencyThresholdMs } == true && currentLatency > badLatencyThresholdMs)) {
true
} else currentPacketLossPercent > badPacketLossThresholdPercent*1.3 || (
packetLossAtLastCheck?.let { it > badPacketLossThresholdPercent } == true && currentPacketLossPercent > badPacketLossThresholdPercent)
if(hasBadConnection) {
measurementsWithBadConnection++
callCallback()
} else if(currentPacketLossPercent > badPacketLossThresholdPercent*1.3 || (
packetLossAtLastCheck?.let { it > badPacketLossThresholdPercent } == true && currentPacketLossPercent > badPacketLossThresholdPercent)) {
callCallback()
} else if(measurementsWithBadConnection != 0){
if(measurementsWithBadConnection == 1) onBadConnectionResolved()
measurementsWithBadConnection = maxOf(0, measurementsWithBadConnection - minOf(1,maxOf(8, measurementsWithBadConnection/12)))
}
latencyAtLastCheck = trafficStats.floatingAverageLatency.toInt()
......
......@@ -1297,9 +1297,11 @@ class DnsVpnService : VpnService(), Runnable {
}
currentTrafficStats = vpnProxy?.trafficStats
if(!watchdogDisabledForSession && getPreferences().enableConnectionWatchDog) connectionWatchDog = currentTrafficStats?.let {
ConnectionWatchdog(it, 30*1000, 10*60*10000) {
ConnectionWatchdog(it, 30*1000, 10*60*10000,onBadServerConnection = {
showBadConnectionNotification()
}
}, onBadConnectionResolved = {
hideBadConnectionNotification()
})
}
hideBadConnectionNotification()
LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(BROADCAST_VPN_ACTIVE))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment