Skip to content

Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.

Network names: start a lookup only once it is stored

jesse-agent-821421a4c7a00ec
diff --git a/app/src/main/java/app/tellyscope/data/NetworkNames.kt b/app/src/main/java/app/tellyscope/data/NetworkNames.kt
index fb19828..a226238 100644
--- a/app/src/main/java/app/tellyscope/data/NetworkNames.kt
+++ b/app/src/main/java/app/tellyscope/data/NetworkNames.kt
@@ -3,6 +3,7 @@
package app.tellyscope.data
import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
@@ -11,6 +12,7 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withTimeoutOrNull
import java.net.InetAddress
import java.util.concurrent.ConcurrentHashMap
+import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
@@ -27,8 +29,9 @@ class NetworkNames(
private val keep: Duration = 10.minutes,
private val clock: () -> Long = System::currentTimeMillis,
private val resolve: (String) -> String? = { ip -> InetAddress.getByName(ip).canonicalHostName },
+ context: CoroutineContext = Dispatchers.IO,
) {
- private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
+ private val scope = CoroutineScope(SupervisorJob() + context)
private val known = ConcurrentHashMap<String, Pair<Long, String?>>()
private val asking = ConcurrentHashMap<String, Deferred<String?>>()
@@ -37,12 +40,13 @@ class NetworkNames(
val now = clock()
val asks = addresses.distinct()
.filter { a -> known[a]?.let { now - it.first > keep.inWholeMilliseconds } ?: true }
- .map { a -> asking.computeIfAbsent(a) { ask(a) } }
+ // Started only once it's in [asking], so a quick answer can't remove itself before it's there.
+ .map { a -> asking.computeIfAbsent(a) { ask(a) }.also { it.start() } }
if (asks.isNotEmpty()) withTimeoutOrNull(wait) { asks.awaitAll() }
return addresses.mapNotNull { a -> known[a]?.second?.let { a to it } }.toMap()
}
- private fun ask(address: String) = scope.async {
+ private fun ask(address: String) = scope.async(start = CoroutineStart.LAZY) {
val name = runCatching { clean(resolve(address), address) }.getOrNull()
known[address] = clock() to name
asking.remove(address)
diff --git a/app/src/test/java/app/tellyscope/NetworkNamesTest.kt b/app/src/test/java/app/tellyscope/NetworkNamesTest.kt
index 56241b0..78065b2 100644
--- a/app/src/test/java/app/tellyscope/NetworkNamesTest.kt
+++ b/app/src/test/java/app/tellyscope/NetworkNamesTest.kt
@@ -3,6 +3,7 @@
package app.tellyscope
import app.tellyscope.data.NetworkNames
+import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
@@ -51,6 +52,17 @@ class NetworkNamesTest {
assertEquals(2, asked.get())
}
+ @Test fun aQuickAnswerStillRefreshesWhenOld() = runBlocking {
+ // Unconfined answers before the lookup is even stored, the race a fast resolver can win on IO.
+ val asked = AtomicInteger()
+ var now = 0L
+ val names = NetworkNames(keep = 10.minutes, clock = { now }, context = Dispatchers.Unconfined,
+ resolve = { "box-${asked.incrementAndGet()}" })
+ assertEquals(mapOf("192.0.2.47" to "box-1"), names.of(listOf("192.0.2.47")))
+ now += 11 * 60_000
+ assertEquals(mapOf("192.0.2.47" to "box-2"), names.of(listOf("192.0.2.47")))
+ }
+
@Test fun aFailingLookupIsNoName() = runBlocking {
val names = NetworkNames(resolve = { throw java.net.UnknownHostException(it) })
assertEquals(emptyMap<String, String>(), names.of(listOf("192.0.2.47")))