mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
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:
@@ -36,6 +36,9 @@ private const val CHANNEL_ID = "vpn_status"
|
|||||||
private const val NOTIFICATION_ID = 1
|
private const val NOTIFICATION_ID = 1
|
||||||
private const val MAX_BAD_PACKETS = 100
|
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
|
// 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
|
// rather than on the main thread, where the service stops them
|
||||||
private val closer = Executors.newSingleThreadExecutor { r -> Thread(r, "doh-close").apply { isDaemon = true } }
|
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)
|
QueryLog.add(question, QueryStatus.CACHED, 0)
|
||||||
return
|
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 {
|
try {
|
||||||
val start = SystemClock.elapsedRealtime()
|
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
|
var status = QueryStatus.OK
|
||||||
val answer = try {
|
val answer = try {
|
||||||
doh.query(query.payload).also { cache.put(query.payload, it) }
|
doh.query(query.payload).also { cache.put(query.payload, it) }
|
||||||
|
|||||||
Reference in New Issue
Block a user