fix(m165): forget 之后让自更新也换引擎(复审 P2-1)+ 卡口计数与端到端守卫

复审 P2-1(采「修法 1」,不是只改措辞):`shared::forget` 只让登记处不再持有
那一份,换不掉自更新 `Factory` 里常驻的 Arc —— 于是「重新检测地区」对更新路径
不生效,直到重启。现在设置动作会把 Factory 一起重建。

- presenter/update_state: `Runtime::rebuild_factory` + `CoreHost::refresh_update_factory`;
  只换 factory,不动 view/inflight(在飞的 Job 派发时已克隆走装配,重建不打断它)
- presenter/dispatch: `SetSetting` 成功且键是「重新检测地区」时调 refresh
- presenter/engine: `DETECT_REGION_ACTION` 常量(设置页行 id / set 分派 / dispatch
  判断共用);放这里而不是 settings.rs 是因为后者已到 400 行红线
- core/shared: 文档改准 —— forget 的范围是「登记处不再持有」,长命持有者必须自己重建;
  记账从 `engine_for` 移到 `DownloadEngine::new` 这个卡口(分两把锁,避免自锁),
  于是 `constructions` 连绕过登记处另造引擎的调用点也统得住

守卫(都做了变异实证):
- update_tests::detect_region_rebuilds_the_update_factory_engine:走真实命令路径
  `AppCommand::SetSetting` → 断言 Factory 换了引擎且指向登记处当前实例;变异(删掉
  dispatch 里那句 refresh)→ 红在 Arc::ptr_eq
- update_tests::rebuilding_the_factory_does_not_touch_a_running_job:重建不清
  inflight、不改快照,在飞 Job 仍能跑完
- addon/http::tests::three_host_request_paths_share_one_engine:连走三条真实宿主路径
  (Modrinth 元数据 GET / CF 带 header GET / Java 页渲染)后 constructions == 1;
  变异(让 get_via_engine 自己造引擎)→ 红,left:2 right:1

顺带修:settings.rs 在上一提交被推到 403 行、engine.rs 401 行,两者都超
guards.yml 的 file-size 红线(>400 直接失败)。现在 settings.rs 400 / engine.rs 399,
全仓 `*.rs` 复跑「>400」扫描无输出。

验证:core lib 608 过、5 个 download 集成 target 42 过、presenter lib 270 过、
clippy --all-targets 干净、fmt 干净。真站点仍未打(见报告「没验的部分」)。
This commit is contained in:
AskaEth committed 2026-11-08 09:30:05 +08:00
1 parent 46389d61fd
commit afe169751f
8 files changed
+299 -32

No files matched your search

+2
View File
@@ -74,6 +74,8 @@ impl DownloadEngine {
let http = http::build_client(&cfg).unwrap_or_else(|_| reqwest::Client::new());
let sem = Arc::new(Semaphore::new(cfg.max_concurrency.max(1)));
let seed = now_ms() ^ (u64::from(cfg.max_concurrency as u32) << 32);
// 纯记账(M165):卡在构造这个点上,才统计得到绕过登记处的调用点(守卫靠 `constructions`)。
super::shared::record_build(cache.root());
Self {
http,
cfg,
+48 -26
View File
@@ -26,6 +26,12 @@
//! * **活多久**:跟随进程。登记处**不做淘汰**——淘汰会让后续请求建出第二个引擎,同一个根上
//! 同时存在两条连接池,等于把要修的问题换个地方复现。要让地区判定等「一次性派生值」重算,
//! 用 [`forget`] 显式失效(设置页「重新检测地区」就这么做)。
//! * **`forget` 到什么范围**:只让登记处不再持有那一份。**已经拿到 `Arc` 的人继续用旧的**:
//! 短命请求自然结束(它们的窗口只有一次下载),但**长命持有者必须自己重建**——presenter 的
//! 自更新子系统(`host::update::Factory`)从 `CoreHost::build` 起就把引擎存在字段里,
//! 只 forget 的话它会一直用旧实例(含旧地区档案)直到进程结束。所以「重新检测地区」那条
//! 设置动作除了 [`forget`] 还调 `CoreHost::refresh_update_factory()` 把新引擎接过去
//! (M165 复审 P2-1:这是本模块文档原先说漏的一半)。
//! * **并发多少**:`EngineConfig::default().max_concurrency`(64)现在是**进程级**上限,
//! 不再是「每请求 64」。单请求独占时与从前一致;多请求并发时它们分摊这 64 张许可。
//! * **镜像切换 / 失效**:一律走引擎原有的候选规划 + 健康度冷却,本模块不参与判定;唯一可见的
@@ -39,7 +45,7 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
use std::sync::{Arc, Mutex, OnceLock};
use super::cache::{now_ms, ContentCache};
use super::config::EngineConfig;
@@ -49,22 +55,19 @@ use super::mirrors::MirrorTable;
use super::region::{decide_from, load_cached, store_cached};
use super::region_system::detect_signals;
/// 登记处:缓存根 → 引擎,外加「这个根建过几次引擎」(守卫/诊断用,**不**随淘汰清零)。
#[derive(Debug, Default)]
struct Registry {
engines: HashMap<PathBuf, Arc<DownloadEngine>>,
builds: HashMap<PathBuf, usize>,
/// 登记处:缓存根 → 引擎。
fn registry() -> &'static Mutex<HashMap<PathBuf, Arc<DownloadEngine>>> {
static ENGINES: OnceLock<Mutex<HashMap<PathBuf, Arc<DownloadEngine>>>> = OnceLock::new();
ENGINES.get_or_init(|| Mutex::new(HashMap::new()))
}
fn registry() -> &'static Mutex<Registry> {
static REGISTRY: OnceLock<Mutex<Registry>> = OnceLock::new();
REGISTRY.get_or_init(|| Mutex::new(Registry::default()))
}
fn lock() -> MutexGuard<'static, Registry> {
registry()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
/// 「这个根建过几次引擎」的账本(守卫/诊断用,**不**随 [`forget`] 清零)。
///
/// 与 [`registry`] **分两把锁**:记账发生在 `DownloadEngine::new` 里(见 [`record_build`]),
/// 而 [`engine_for`] 是**持着**登记处那把锁去调构造的——合成一把锁就是自己等自己。
fn ledger() -> &'static Mutex<HashMap<PathBuf, usize>> {
static BUILDS: OnceLock<Mutex<HashMap<PathBuf, usize>>> = OnceLock::new();
BUILDS.get_or_init(|| Mutex::new(HashMap::new()))
}
/// 缓存根的键:去掉尾部分隔符(`D:/x/cache` 与 `D:/x/cache/` 视作同一根)。
@@ -85,35 +88,54 @@ fn key_of(cache_root: &Path) -> PathBuf {
#[must_use]
pub fn engine_for(cache_root: impl AsRef<Path>) -> Arc<DownloadEngine> {
let key = key_of(cache_root.as_ref());
let mut reg = lock();
if let Some(engine) = reg.engines.get(&key) {
let mut engines = registry()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if let Some(engine) = engines.get(&key) {
return Arc::clone(engine);
}
let engine = Arc::new(build(&key));
reg.engines.insert(key.clone(), Arc::clone(&engine));
*reg.builds.entry(key).or_default() += 1;
engines.insert(key, Arc::clone(&engine));
engine
}
/// 让该数据根的共享引擎失效(下次 [`engine_for`] 重新构造)。
///
/// 在飞请求持有的 `Arc` 不受影响(它们继续用完手里那个引擎);之后的新请求拿到新引擎,
/// 于是「重新检测地区」这类一次性派生值能真的重算。返回是否真淘汰了一份。
/// **只让登记处不再持有那一份**:已经拿到 `Arc` 的人继续用旧的。短命请求自然结束;长命持有者
/// (presenter 的自更新 `Factory`)必须自己重建,否则它会一直用旧实例——本仓在「重新检测地区」
/// 的设置动作里调 `CoreHost::refresh_update_factory()` 补上这一步(见模块文档「`forget` 到什么范围」)。
/// 返回是否真淘汰了一份。
pub fn forget(cache_root: impl AsRef<Path>) -> bool {
lock()
.engines
registry()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.remove(&key_of(cache_root.as_ref()))
.is_some()
}
/// 该数据根在本进程里**实际构造过几次引擎**。
/// 记一笔「这个缓存根上又造了一个引擎」。
///
/// **唯一调用点是 [`DownloadEngine::new`]**(不是 [`engine_for`]):记账卡在构造这个卡口上,
/// 于是 [`constructions`] 统计的是**进程里真实发生过几次构造**,与「谁造的」无关。
/// 若只在 `engine_for` 里记账,任何绕过登记处、自己 `DownloadEngine::new` 的调用点都统计不到
/// (M165 复审备注 2 的诉求)。
pub(crate) fn record_build(cache_root: &Path) {
*ledger()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.entry(key_of(cache_root))
.or_default() += 1;
}
/// 该数据根在本进程里**实际构造过几次引擎**(不论谁构造的)。
///
/// 这是给守卫与诊断用的:跨请求复用成立时,同一个根无论被请求多少次,这个数都停在 `1`
/// (`forget` 之后每重建一次 +1)。只断言「结果一样」测不出复用——引擎建几次都能下同一个文件。
#[must_use]
pub fn constructions(cache_root: impl AsRef<Path>) -> usize {
lock()
.builds
ledger()
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.get(&key_of(cache_root.as_ref()))
.copied()
.unwrap_or(0)