deps_core/cache/pool.rs
1//! Transport pool and registry-policy handling for `HttpCache`.
2
3use super::CacheTier;
4use super::HttpCache;
5use super::transport::Transport;
6use super::transport::pinned_digest;
7use crate::net_policy::{TrustedPrefix, WorkspaceRegistryAccess};
8#[cfg(test)]
9use std::sync::atomic::Ordering;
10
11impl HttpCache {
12 /// Returns the transport scoped to `trusted_origin`, building and pooling one on first use.
13 ///
14 /// The `get` fast path (a shared read lock) serves the common case — a `trusted_origin`
15 /// already pooled — without ever taking `trusted_clients`' write-capable `entry` lock;
16 /// `entry().or_insert_with()` only runs on a miss, so two callers racing on the same new
17 /// origin still only ever build and store one [`Transport`] for it, not one each.
18 pub(super) fn transport_for_origin(&self, trusted_origin: &TrustedPrefix) -> Transport {
19 let key = (trusted_origin.clone(), CacheTier::Baseline);
20 if let Some(existing) = self.trusted_clients.get(&key) {
21 return existing.clone();
22 }
23
24 self.trusted_clients
25 .entry(key)
26 .or_insert_with(|| Transport::origin_pinned(trusted_origin))
27 .clone()
28 }
29
30 /// Like [`Self::transport_for_origin`], but for an origin-pinned, connect-address-guarded
31 /// [`CacheTier::Pinned`] transport (issue #561/#562) — building and pooling one on first
32 /// use, keyed by `(trusted_origin, CacheTier::Pinned { .. })` so an authenticated and
33 /// unauthenticated transport for the same origin share one pool entry; the credential state
34 /// lives in the request's [`RequestAuth`](super::RequestAuth), not the transport.
35 pub(super) fn transport_for_pinned(&self, trusted_origin: &TrustedPrefix) -> Transport {
36 let snapshot = self.policy.snapshot();
37 let digest = pinned_digest(trusted_origin, snapshot.level);
38 let key = (trusted_origin.clone(), CacheTier::Pinned { digest });
39 if let Some(existing) = self.trusted_clients.get(&key) {
40 return existing.clone();
41 }
42
43 self.trusted_clients
44 .entry(key)
45 .or_insert_with(|| {
46 Transport::origin_pinned_guarded(trusted_origin, snapshot, self.policy.egress())
47 })
48 .clone()
49 }
50
51 /// Updates the policy governing [`Self::get_cached_workspace`], rebuilding the workspace
52 /// transport field (and so its cache-key namespace and guard together) when
53 /// `value` actually differs from the current setting — a no-op call does not rebuild, so a
54 /// caller that re-applies an unchanged configuration does not pay for a fresh `Client` and
55 /// its connection pool.
56 ///
57 /// Effective for every [`Self::get_cached_workspace`] call after this returns. Note this
58 /// only gates *future* fetches: an `All -> PublicOnly`/`Off` tightening does not purge
59 /// already-registered `deps-cargo` alternate-registry clients resolved under the looser
60 /// policy (pre-existing, documented on [`crate::net_policy::RegistryAccessPolicy::set`]).
61 ///
62 /// Unlike that pre-existing gap, every `CacheTier::Pinned` cache entry (issue #561/#562)
63 /// **is** purged on every actual policy transition, along with every pinned-tier pooled
64 /// `Transport` — substantially narrowing the `All -> PublicOnly -> All` round-trip hole for
65 /// credential-carrying entries specifically (NFR-004): re-namespacing alone (as the
66 /// pre-existing workspace-tier digit prefix does) would leave an old-era authenticated
67 /// body reachable once the policy round-trips back to a value whose digest happens to
68 /// collide again. Not an absolute close: a fetch already in flight when the transition
69 /// happens can still land its response and re-insert an old-era key after the purge —
70 /// harmless (readable only under the era it was legitimately fetched in), just not
71 /// prevented by this purge alone.
72 pub fn set_registry_policy(&self, value: WorkspaceRegistryAccess) {
73 if self.policy.get() == value {
74 return;
75 }
76 self.policy.set(value);
77 let rebuilt = Transport::workspace(&self.policy);
78 #[expect(
79 clippy::expect_used,
80 reason = "poisoned only if another thread panicked while holding the lock — see \
81 the matching justification on get_cached_workspace_with_headers above"
82 )]
83 {
84 *self
85 .workspace
86 .write()
87 .expect("workspace transport lock poisoned") = rebuilt;
88 }
89 #[cfg(test)]
90 self.workspace_rebuilds.fetch_add(1, Ordering::Relaxed);
91
92 self.evict_entries_where(|key| key.starts_with(Self::PINNED_KEY_PREFIX));
93 self.trusted_clients
94 .retain(|(_, tier), _| !matches!(tier, CacheTier::Pinned { .. }));
95 }
96}