Fail lookups that waited too long in the worker queue

With a slow server all 16 workers can be busy for up to 8 s each, and
queries kept piling up long after the asking app had retried or given
up, delaying every newer one.
This commit is contained in:
2026-10-25 12:25:11 +02:00
parent 8ac908e684
commit 2cb3439efd
@@ -36,6 +36,9 @@ private const val CHANNEL_ID = "vpn_status"
private const val NOTIFICATION_ID = 1
private const val MAX_BAD_PACKETS = 100
// Android's resolver retries after 5 s, so an older lookup no longer has anyone waiting for it
private const val QUEUE_TIMEOUT_MS = 5000L
// Closing TLS connections writes to the network, so sessions release their DoH client here
// rather than on the main thread, where the service stops them
private val closer = Executors.newSingleThreadExecutor { r -> Thread(r, "doh-close").apply { isDaemon = true } }
@@ -283,12 +286,21 @@ private class Session(
QueryLog.add(question, QueryStatus.CACHED, 0)
return
}
pool.execute { handle(query, question) }
val queued = SystemClock.elapsedRealtime()
pool.execute { handle(query, question, queued) }
}
private fun handle(query: UdpPacket, question: DnsMessage.Question?) {
private fun handle(query: UdpPacket, question: DnsMessage.Question?, queued: Long) {
try {
val start = SystemClock.elapsedRealtime()
// On a slow network lookups pile up behind the workers. Answer the stale ones with
// SERVFAIL at once instead of spending a worker on them, so the queue drains.
if (start - queued > QUEUE_TIMEOUT_MS) {
if (!alive.get()) return
QueryLog.add(question, QueryStatus.FAILED, (start - queued).toInt())
DnsMessage.servfail(query.payload)?.let { write(query, it) }
return
}
var status = QueryStatus.OK
val answer = try {
doh.query(query.payload).also { cache.put(query.payload, it) }