Modern Apps git

← Modern-Apps.git

maps: read live traffic squares from their R2 copy first (#static-r2)

Commit
9433d6e9cad51322f280937734ebbbd84dca8670
Author
Vayun Mathur <[email protected]>
When
2026-11-13 22:39 UTC (22 days ago)
Parents
e22c4cc
What changed: fetchTrafficPayload first reads
https://static.maproj.org/traffic/v1/<lat>_<lon>.bin (the whole 1°
square named by its south-west corner, the only bbox the native
traffic_square ever requests) and uses it when its Last-Modified is at
most 5 minutes old; otherwise it calls api.maproj.org/maps/traffic as
before. The payload format is identical (it is the server's own
response body), so processTrafficPayload is unchanged.

Why: /maps/traffic costs the server a HERE flow fetch plus a
multi-second graph match per square (mean 1,828 ms, max 5.9 s on the
dashboard) and a 1-5 MB download per user. The server
(location_share_server feat/static-r2, src/handlers/maps/traffic.rs)
now copies every fresh match to R2, so users viewing the same area
within the window share one match and the downloads come from
Cloudflare instead of the 8 vCPU origin. A stale or missing copy makes
the app ask the server, which re-matches (or SWR-refreshes) and
rewrites it. 5 minutes is the oldest result the server itself already
serves from its stale-while-revalidate cache, so traffic is never older
than before.

User-visible effect: traffic overlays and traffic-aware routes appear
faster in areas someone else viewed recently.

Verified: new TrafficStaticUrlTest pins the key format to the server's
r2_square_key (positive, negative and zero corners; half-degree and
two-degree boxes get no static copy). Run via :maps:testDevUnitTest in
the container build.

Diff

maps/src/main/java/com/vayunmathur/maps/util/OfflineRouterTrafficFetch.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouterTrafficFetch.kt b/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouterTrafficFetch.ktindex fa9ed8b55a..7bd12a34fe 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouterTrafficFetch.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouterTrafficFetch.kt@@ -2,6 +2,7 @@ package com.vayunmathur.maps.util  import com.vayunmathur.library.log.Log import com.vayunmathur.library.network.NetworkClient+import com.vayunmathur.library.network.StaticMirror import java.nio.ByteBuffer import java.nio.ByteOrder @@ -23,6 +24,22 @@ internal object OfflineRouterTrafficFetch {     /** Mask for one unsigned 32-bit count read out of the payload. */     private const val U32_MASK = 0xFFFF_FFFFL +    /**+     * Oldest R2 copy of a square still used. HERE flow moves every 1-2 min; the+     * server itself serves a square's match for up to 5 min (stale-while-revalidate).+     */+    private const val TRAFFIC_STATIC_MAX_AGE_MS = 5L * 60 * 1000++    /**+     * The R2 copy of a whole 1° square (`traffic/v1/<lat>_<lon>.bin`, named by its+     * south-west corner, as the server writes it), or null for any other bbox.+     */+    internal fun staticSquareUrl(minLat: Double, minLon: Double, maxLat: Double, maxLon: Double): String? {+        val whole = minLat == Math.floor(minLat) && minLon == Math.floor(minLon)+        if (!whole || maxLat != minLat + 1.0 || maxLon != minLon + 1.0) return null+        return "${StaticMirror.BASE}/traffic/v1/${minLat.toInt()}_${minLon.toInt()}.bin"+    }+     fun fetchTrafficData(         minLat: Double,         minLon: Double,@@ -75,10 +92,19 @@ internal object OfflineRouterTrafficFetch {         maxLon: Double,         packedSquare: Int,     ): TrafficPayload? {-        val (status, bytes) = NetworkClient.performRequestBytes(-            url = "https://api.maproj.org/maps/traffic" +-                "?min_lat=$minLat&min_lon=$minLon&max_lat=$maxLat&max_lon=$maxLon",-        )+        // The server copies every fresh match of a whole 1° square to R2; read that+        // first and ask the server (which re-matches and re-copies) only when the+        // copy is missing or older than TRAFFIC_STATIC_MAX_AGE_MS.+        val static = staticSquareUrl(minLat, minLon, maxLat, maxLon)+            ?.let { StaticMirror.getStatic(it, TRAFFIC_STATIC_MAX_AGE_MS) }+        val (status, bytes) = if (static != null) {+            static.status to static.bytes+        } else {+            NetworkClient.performRequestBytes(+                url = "https://api.maproj.org/maps/traffic" ++                    "?min_lat=$minLat&min_lon=$minLon&max_lat=$maxLat&max_lon=$maxLon",+            )+        }         Log.dev(             "TRAFFIC_DATA",             "fetchTrafficData NETWORK DONE: status=$status, size=${bytes.size}",
maps/src/test/java/com/vayunmathur/maps/util/TrafficStaticUrlTest.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/util/TrafficStaticUrlTest.kt b/maps/src/test/java/com/vayunmathur/maps/util/TrafficStaticUrlTest.ktnew file mode 100644index 0000000000..64aeb10cec--- /dev/null+++ b/maps/src/test/java/com/vayunmathur/maps/util/TrafficStaticUrlTest.kt@@ -0,0 +1,31 @@+package com.vayunmathur.maps.util++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/** The R2 key the app reads must be the one the server writes (traffic.rs `r2_square_key`). */+class TrafficStaticUrlTest {++    @Test+    fun whole_degree_squares_map_to_their_south_west_corner() {+        assertEquals(+            "https://static.maproj.org/traffic/v1/37_-123.bin",+            OfflineRouterTrafficFetch.staticSquareUrl(37.0, -123.0, 38.0, -122.0),+        )+        assertEquals(+            "https://static.maproj.org/traffic/v1/-34_151.bin",+            OfflineRouterTrafficFetch.staticSquareUrl(-34.0, 151.0, -33.0, 152.0),+        )+        assertEquals(+            "https://static.maproj.org/traffic/v1/0_-1.bin",+            OfflineRouterTrafficFetch.staticSquareUrl(0.0, -1.0, 1.0, 0.0),+        )+    }++    @Test+    fun other_bboxes_have_no_static_copy() {+        assertNull(OfflineRouterTrafficFetch.staticSquareUrl(37.5, -123.0, 38.5, -122.0))+        assertNull(OfflineRouterTrafficFetch.staticSquareUrl(37.0, -123.0, 39.0, -122.0))+    }+}