From 01e38607414455b44de193d480edfe25245561fc Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Fri, 20 Nov 2026 13:27:59 +0100 Subject: [PATCH] refactor(fuse): block fetch single flight from proton-media-rs BlockFlight is now proton_media_rs::SingleFlight joined with the typed block result. Same semantics: one fetch per block, the key retires before the result is published, and a waiter whose leader is cancelled fetches the block itself. --- Cargo.lock | 14 ++++++ Cargo.toml | 1 + crates/pdfs-fuse/Cargo.toml | 1 + crates/pdfs-fuse/src/reads.rs | 84 ++++------------------------------- docs/CHANGELOG.md | 4 ++ 5 files changed, 28 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41af14a..ba5cbb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3669,6 +3669,7 @@ dependencies = [ "proton-drive-rs", "proton-gallery-app-rs", "proton-gallery-rs", + "proton-media-rs", "rustix", "serde", "serde_json", @@ -4043,6 +4044,19 @@ dependencies = [ "zip", ] +[[package]] +name = "proton-media-rs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f03aa6c403e4f2f7a8f9a444dff1e268d8bc9bcaf1d0d953cc3a1312d798f1ef" +dependencies = [ + "lru", + "sha2 0.11.1", + "thiserror 2.0.21", + "tokio", + "tracing", +] + [[package]] name = "proton-sdk" version = "0.12.1" diff --git a/Cargo.toml b/Cargo.toml index bf0cfef..a38f017 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,7 @@ keyring-core = "1" # restart (B285) and replaces items instead of editing them (B291). proton-auth-rs = "0.1" proton-base-rs = "0.1" +proton-media-rs = "0.1.1" dbus-secret-service-keyring-store = { version = "1", features = ["crypto-rust"] } # The same Secret Service client the store wraps, used directly to replace an # item instead of editing it, which oo7 before 0.7.0 kept only in memory (B291). diff --git a/crates/pdfs-fuse/Cargo.toml b/crates/pdfs-fuse/Cargo.toml index cefc5a6..d6741ea 100644 --- a/crates/pdfs-fuse/Cargo.toml +++ b/crates/pdfs-fuse/Cargo.toml @@ -16,6 +16,7 @@ sha1.workspace = true proton-drive-rs.workspace = true proton-gallery-app-rs.workspace = true proton-gallery-rs.workspace = true +proton-media-rs.workspace = true hyper = { version = "1", features = ["http1", "server"] } hyper-util = { version = "0.1", features = ["tokio"] } http-body-util = "0.1" diff --git a/crates/pdfs-fuse/src/reads.rs b/crates/pdfs-fuse/src/reads.rs index a785177..824171b 100644 --- a/crates/pdfs-fuse/src/reads.rs +++ b/crates/pdfs-fuse/src/reads.rs @@ -212,15 +212,12 @@ impl BlockRing { } /// The result of fetching one block, shared between everyone who wanted it. -type BlockResult = Result>, Errno>; +pub(super) type BlockResult = Result>, Errno>; /// Identifies a block *of a revision*: node, validity tag, and the plaintext /// range the block covers — see [`RingKey`] for why it is the range and not the /// index. -type BlockKey = (NodeUid, i64, u64, BlockSpan); - -/// A claim on a block key: who owns the fetch, and where its result is published. -type Claim = (u64, tokio::sync::watch::Receiver>); +pub(super) type BlockKey = (NodeUid, i64, u64, BlockSpan); /// Block fetches in flight, so a block is downloaded and decrypted once however /// many readers want it at that moment (audit T1). @@ -231,34 +228,7 @@ type Claim = (u64, tokio::sync::watch::Receiver>); /// for. Without this each of them pays the full round-trip and decrypt and all /// but one result is discarded — roughly double the bandwidth and CPU in steady /// state. Same shape as [`ReaderSlot::Pending`] one level up. -#[derive(Default)] -pub(super) struct BlockFlight { - inflight: Mutex>, -} - -/// Distinguishes attempts at the same key, so a leader only retires its own. -static NEXT_FLIGHT_ID: AtomicU64 = AtomicU64::new(0); - -/// The leader's claim on a key. Retires it on drop — including when the task is -/// cancelled mid-fetch, which is what keeps a dropped `JoinSet` from leaving a -/// key nobody will ever publish. -struct Flight { - flight: Arc, - key: BlockKey, - id: u64, -} - -impl Drop for Flight { - fn drop(&mut self) { - let mut inflight = self.flight.inflight.lock(); - if inflight - .get(&self.key) - .is_some_and(|(id, _)| *id == self.id) - { - inflight.remove(&self.key); - } - } -} +pub(super) type BlockFlight = proton_media_rs::SingleFlight; /// Where each file's reader is expected to read next, so sequential reads can be /// told from random ones and only the former prefetched (audit T2). @@ -581,49 +551,11 @@ impl Core { cache_blocks: bool, ) -> BlockResult { let key: BlockKey = (uid.clone(), mtime, fsize, span); - let claim = { - let mut inflight = self.block_flight.inflight.lock(); - match inflight.get(&key) { - Some((_, rx)) => Err(rx.clone()), - None => { - let id = NEXT_FLIGHT_ID.fetch_add(1, Ordering::Relaxed); - let (tx, rx) = tokio::sync::watch::channel(None); - inflight.insert(key.clone(), (id, rx)); - Ok(( - tx, - Flight { - flight: self.block_flight.clone(), - key: key.clone(), - id, - }, - )) - } - } - }; - let (tx, flight) = match claim { - Ok(lead) => lead, - Err(mut rx) => { - loop { - if let Some(result) = rx.borrow_and_update().clone() { - return result; - } - if rx.changed().await.is_err() { - break; - } - } - return self - .read_block(reader, uid, mtime, fsize, span, cache_blocks) - .await; - } - }; - let result = self - .read_block(reader, uid, mtime, fsize, span, cache_blocks) - .await; - // Retire the claim before publishing, so a racer that wakes and re-enters - // finds no stale entry to join. - drop(flight); - let _ = tx.send(Some(result.clone())); - result + self.block_flight + .join(key, || { + self.read_block(reader, uid, mtime, fsize, span, cache_blocks) + }) + .await } /// Download and decrypt one block, then put it where the next reader will diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index c9f5905..a3e9bf6 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -13,6 +13,10 @@ scratch (user data in `staging/` and `recovery/` is never touched by this). ### Changed +- Block fetches shared by concurrent readers now use `SingleFlight` from + `proton-media-rs`, the same code Proton Stream uses. Same behaviour: one + download per block, every reader gets its result, and a reader whose fetch was + cancelled downloads the block itself. - The shared Gallery engine now comes from the `proton-gallery-rs` and `proton-gallery-app-rs` crates on crates.io instead of a pinned Git revision of Proton Gallery. Builds, vendoring and the Nix package no longer fetch from Git.