|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +// Copyright (C) 2026 Marc Hoffmann (b14ckyy) |
| 3 | + |
| 4 | +//! Backend map-tile loader. |
| 5 | +//! |
| 6 | +//! Fetches map tiles through the Rust HTTP stack (reqwest → HTTP/2 + connection |
| 7 | +//! pooling) instead of the OS WebView's HTTP client, for both the 2D (Leaflet) |
| 8 | +//! and 3D (Cesium) tile pipelines. The WebView caps connections per host and, on |
| 9 | +//! WebKitGTK/Linux, serialises requests to a single-host provider (e.g. ESRI), |
| 10 | +//! which makes tiles crawl in; reqwest multiplexes over HTTP/2 without that cap, |
| 11 | +//! so tile loading is fast and consistent across all platforms. A concurrency |
| 12 | +//! gate keeps us a polite client toward community providers (see below). |
| 13 | +
|
| 14 | +use std::sync::OnceLock; |
| 15 | +use std::time::Duration; |
| 16 | +use tokio::sync::Semaphore; |
| 17 | + |
| 18 | +/// Cap on concurrent tile fetches. Bypassing the WebView removes the browser's |
| 19 | +/// natural per-host connection limit, so a single pan could otherwise fire 100+ |
| 20 | +/// requests at once — fine for CDN providers (ESRI/Carto) but a policy/ban risk |
| 21 | +/// for community servers (OSM, OpenTopoMap). 12 in flight keeps loading fast |
| 22 | +/// (parallel HTTP/2 over pooled connections) while staying a polite client. |
| 23 | +/// Easily tuned; a future refinement could vary it per provider. |
| 24 | +const MAX_CONCURRENT_TILE_FETCHES: usize = 12; |
| 25 | + |
| 26 | +/// Shared HTTP client — pooled connections + HTTP/2, reused across all tile fetches. |
| 27 | +fn client() -> &'static reqwest::Client { |
| 28 | + static CLIENT: OnceLock<reqwest::Client> = OnceLock::new(); |
| 29 | + CLIENT.get_or_init(|| { |
| 30 | + reqwest::Client::builder() |
| 31 | + .user_agent("KiteGroundControl/1.0 (+https://github.com/b14ckyy/Kite-GC)") |
| 32 | + .timeout(Duration::from_secs(15)) |
| 33 | + .build() |
| 34 | + .expect("failed to build tile HTTP client") |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +/// Global gate bounding how many tile fetches run at once (see the const above). |
| 39 | +fn fetch_gate() -> &'static Semaphore { |
| 40 | + static GATE: OnceLock<Semaphore> = OnceLock::new(); |
| 41 | + GATE.get_or_init(|| Semaphore::new(MAX_CONCURRENT_TILE_FETCHES)) |
| 42 | +} |
| 43 | + |
| 44 | +/// Fetch a single map tile and return its raw bytes to the frontend. |
| 45 | +/// |
| 46 | +/// Returned as a Tauri `Response` so the bytes travel over IPC as a real binary |
| 47 | +/// payload (an `ArrayBuffer` on the JS side), not a JSON number array. |
| 48 | +#[tauri::command] |
| 49 | +pub async fn fetch_tile(url: String) -> Result<tauri::ipc::Response, String> { |
| 50 | + // Hold a permit for the whole request so no more than MAX_CONCURRENT_TILE_FETCHES |
| 51 | + // are ever in flight; excess fetches queue here and proceed as permits free up. |
| 52 | + let _permit = fetch_gate() |
| 53 | + .acquire() |
| 54 | + .await |
| 55 | + .map_err(|e| format!("tile gate closed: {e}"))?; |
| 56 | + let resp = client() |
| 57 | + .get(&url) |
| 58 | + .send() |
| 59 | + .await |
| 60 | + .map_err(|e| format!("tile request failed: {e}"))?; |
| 61 | + if !resp.status().is_success() { |
| 62 | + return Err(format!("tile HTTP {}", resp.status())); |
| 63 | + } |
| 64 | + let bytes = resp |
| 65 | + .bytes() |
| 66 | + .await |
| 67 | + .map_err(|e| format!("tile body read failed: {e}"))?; |
| 68 | + Ok(tauri::ipc::Response::new(bytes.to_vec())) |
| 69 | +} |
0 commit comments