fix: pre-WS health probe, callTimeout, granular phase logging
- HTTP GET /health probe before WebSocket: distinguishes DNS/TCP/TLS failure from WS handshake - callTimeout 15s covers entire connect+handshake (not just connectTimeout) - 15s Handler guard covers probe-to-hello full flow, shows timeout reason - Phase log: submitted / dns / tcp / tls / http response / ws open / auth / hello - Old generation callbacks isolated during probe phase too Version 0.2.4-m5-fix5
This commit is contained in:
@@ -2,6 +2,10 @@ package com.gorios.lanvoice.net
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.EventListener
|
||||
import okhttp3.Handshake
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
@@ -9,8 +13,11 @@ import okhttp3.Response
|
||||
import okhttp3.WebSocket
|
||||
import okhttp3.WebSocketListener
|
||||
import okio.ByteString
|
||||
import java.io.IOException
|
||||
import java.io.InterruptedIOException
|
||||
import java.net.ConnectException
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Proxy
|
||||
import java.net.SocketTimeoutException
|
||||
import java.net.UnknownHostException
|
||||
import java.security.cert.CertificateException
|
||||
@@ -41,6 +48,8 @@ class DialogueClient(
|
||||
.url(endpoint.toString().replaceFirst("https://", "wss://"))
|
||||
.header("Origin", "https://${endpoint.host.let { if (':' in it) "[$it]" else it }}:${endpoint.port}")
|
||||
.build()
|
||||
private val healthRequest = request.newBuilder()
|
||||
.url(endpoint.newBuilder().encodedPath("/health").build()).get().build()
|
||||
// OkHttp sends RFC 6455 ping frames, replies to ping with pong automatically, and fails a
|
||||
// connection whose pong is absent at the next interval. No timer survives disconnect.
|
||||
private val client = OkHttpClient.Builder()
|
||||
@@ -55,6 +64,8 @@ class DialogueClient(
|
||||
.build()
|
||||
private var generation = 0L
|
||||
private var socket: WebSocket? = null
|
||||
private var healthCall: Call? = null
|
||||
private var healthPhase = "health request"
|
||||
private var released = false
|
||||
private var ready = false
|
||||
private var wsOpened = false
|
||||
@@ -71,28 +82,82 @@ class DialogueClient(
|
||||
wsOpened = false
|
||||
authReceived = false
|
||||
helloSent = false
|
||||
healthCall?.cancel()
|
||||
healthCall = null
|
||||
socket?.cancel()
|
||||
socket = null
|
||||
val attempt = generation
|
||||
onStatus(ConnectionStatus.CONNECTING)
|
||||
onConnectionLog("ws connecting #$attempt")
|
||||
onConnectionLog("health checking #$attempt")
|
||||
// Arm before submitting the request; synchronous setup failures are terminal too.
|
||||
handshakeTimeout = Runnable {
|
||||
if (generation == attempt && !released && !ready) {
|
||||
val phase = when {
|
||||
healthCall != null -> healthPhase
|
||||
helloSent -> "等待 hello"
|
||||
wsOpened -> "等待 auth"
|
||||
else -> "等待 WS(TCP/TLS/HTTP)"
|
||||
}
|
||||
onConnectionLog("连接超时:$phase")
|
||||
onConnectionLog("连接超时,请检查网络 · $phase")
|
||||
finish(socket, ConnectionStatus.DISCONNECTED)
|
||||
}
|
||||
}.also { main.postDelayed(it, 15_000L) }
|
||||
startHealthProbe(attempt)
|
||||
}
|
||||
|
||||
private fun startHealthProbe(attempt: Long) {
|
||||
healthPhase = "health request"
|
||||
try {
|
||||
// Same trust, endpoint and dispatcher as WS. Probe events reveal DNS/TCP/TLS
|
||||
// progress; OkHttp deliberately disables EventListener on WebSocket requests.
|
||||
val probeClient = client.newBuilder()
|
||||
.connectTimeout(5, TimeUnit.SECONDS)
|
||||
.readTimeout(5, TimeUnit.SECONDS)
|
||||
.callTimeout(5, TimeUnit.SECONDS)
|
||||
.eventListener(object : EventListener() {
|
||||
private fun stage(call: Call, phase: String) = dispatchHealth(attempt, call) {
|
||||
healthPhase = phase
|
||||
onConnectionLog(phase)
|
||||
}
|
||||
|
||||
override fun dnsStart(call: Call, domainName: String) = stage(call, "health DNS")
|
||||
override fun connectStart(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy) =
|
||||
stage(call, "health TCP connecting")
|
||||
override fun secureConnectStart(call: Call) = stage(call, "health TLS handshake")
|
||||
override fun secureConnectEnd(call: Call, handshake: Handshake?) = stage(call, "health TLS ok")
|
||||
}).build()
|
||||
val current = probeClient.newCall(healthRequest)
|
||||
healthCall = current
|
||||
current.enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, error: IOException) = dispatchHealth(attempt, call) {
|
||||
onConnectionLog("health failed: ${failureSummary(error)}")
|
||||
finish(socket, ConnectionStatus.DISCONNECTED)
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
val code = response.use { it.code } // Never retain or log a response body.
|
||||
dispatchHealth(attempt, call) {
|
||||
healthCall = null
|
||||
// Even 404/403/503 establishes HTTPS reachability. /health may be
|
||||
// absent or protected; only the actual WS handshake decides busy.
|
||||
onConnectionLog("health HTTP $code · HTTPS 可达")
|
||||
openWebSocket(attempt)
|
||||
}
|
||||
}
|
||||
})
|
||||
} catch (error: Exception) {
|
||||
onConnectionLog("health setup failed: ${failureSummary(error)}")
|
||||
finish(socket, ConnectionStatus.DISCONNECTED)
|
||||
}
|
||||
}
|
||||
|
||||
private fun openWebSocket(attempt: Long) {
|
||||
onConnectionLog("ws connecting #$attempt")
|
||||
try {
|
||||
// Callbacks always post to main, including AUTH arriving before onOpen.
|
||||
// They run after this assignment and do not depend on wsOpened.
|
||||
socket = client.newWebSocket(request, listener(attempt))
|
||||
onConnectionLog("ws request queued #$attempt")
|
||||
onConnectionLog("ws submitted #$attempt")
|
||||
} catch (error: Exception) {
|
||||
onConnectionLog("ws setup failed: ${failureSummary(error)}")
|
||||
finish(socket, ConnectionStatus.DISCONNECTED)
|
||||
@@ -104,10 +169,14 @@ class DialogueClient(
|
||||
generation++ // Invalidate before cancellation can trigger onFailure.
|
||||
cancelHandshakeTimeout()
|
||||
ready = false
|
||||
val previousHealth = healthCall
|
||||
healthCall = null
|
||||
previousHealth?.cancel()
|
||||
val previous = socket
|
||||
socket = null
|
||||
previous?.cancel()
|
||||
if (previous != null) onConnectionLog("ws disconnected")
|
||||
else if (previousHealth != null) onConnectionLog("health cancelled")
|
||||
onStatus(ConnectionStatus.DISCONNECTED)
|
||||
}
|
||||
|
||||
@@ -243,9 +312,18 @@ class DialogueClient(
|
||||
}
|
||||
}
|
||||
|
||||
private fun dispatchHealth(attempt: Long, call: Call, action: () -> Unit) {
|
||||
main.post {
|
||||
if (generation == attempt && healthCall === call && !released) action()
|
||||
}
|
||||
}
|
||||
|
||||
private fun finish(webSocket: WebSocket?, status: ConnectionStatus) {
|
||||
// Invalidate terminal callbacks so a later failure cannot overwrite a 1013 busy status.
|
||||
cancelHandshakeTimeout()
|
||||
val previousHealth = healthCall
|
||||
healthCall = null
|
||||
previousHealth?.cancel()
|
||||
socket = null
|
||||
ready = false
|
||||
webSocket?.cancel()
|
||||
|
||||
Reference in New Issue
Block a user