Skip to content

Commit ca72d74

Browse files
naonakclaude
andcommitted
refactor(restart): always race for early recovery during cooldown
Remove the cooldownMs > pingIntervalMs guard. The withTimeoutOrNull block already handles both cases correctly — it expires after cooldownMs when no recovery is detected, and exits early if pings succeed. This enables early recovery detection even when cooldown <= pingInterval, at zero extra cost. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 69e109e commit ca72d74

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

app/src/main/java/com/zaneschepke/wireguardautotunnel/core/tunnel/handler/HandshakeRestartHandler.kt

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,36 +278,26 @@ class HandshakeRestartHandler(
278278
),
279279
)
280280

281-
// If cooldown is longer than ping interval, race against periodic ping recovery
282-
val pingIntervalMs = settings.tunnelPingIntervalSeconds.toMillis()
283-
if (cooldownMs > pingIntervalMs) {
284-
val recoveredDuringCooldown =
285-
withTimeoutOrNull(cooldownMs) {
286-
activeTunnels
287-
.mapNotNull { it[tunnelId]?.pingStates }
288-
.distinctUntilChanged()
289-
.first { pingStates ->
290-
pingStates.values.isNotEmpty() &&
291-
pingStates.values.all { it.isReachable }
292-
}
293-
true
294-
} != null
281+
// Race against periodic ping recovery during cooldown
282+
val recoveredDuringCooldown =
283+
withTimeoutOrNull(cooldownMs) {
284+
activeTunnels
285+
.mapNotNull { it[tunnelId]?.pingStates }
286+
.distinctUntilChanged()
287+
.first { pingStates ->
288+
pingStates.values.isNotEmpty() &&
289+
pingStates.values.all { it.isReachable }
290+
}
291+
true
292+
} != null
295293

296-
if (recoveredDuringCooldown) {
297-
Timber.d(
298-
"HandshakeRestartHandler: tunnel $tunnelId recovered during cooldown"
299-
)
300-
emitMessage(config.name, BackendMessage.ConnectionRestored)
301-
attempt = 0
302-
updateProgress(
303-
tunnelId,
304-
TunnelRestartProgress(totalRestarts = totalRestarts),
305-
)
306-
pingTarget = awaitPingFailures(tunnelId, settings)
307-
continue
308-
}
309-
} else {
310-
delay(cooldownMs)
294+
if (recoveredDuringCooldown) {
295+
Timber.d("HandshakeRestartHandler: tunnel $tunnelId recovered during cooldown")
296+
emitMessage(config.name, BackendMessage.ConnectionRestored)
297+
attempt = 0
298+
updateProgress(tunnelId, TunnelRestartProgress(totalRestarts = totalRestarts))
299+
pingTarget = awaitPingFailures(tunnelId, settings)
300+
continue
311301
}
312302
}
313303
// If not recovered: loop continues → next restart attempt

0 commit comments

Comments
 (0)