feat(cache): shm payload 支持多源(v2),向后兼容 v1 三行格式
v1 payload 仍是三行;v2 在第 4 行打版本标记,源列表每两行一个 (url,key)。 仓内读法 split_payload 取前三行 ⇒ v1/v2 下 base/key/mode 全干净。 旧二进制带的是它自己那份 splitn(3):base/key 仍正确,但 mode 会被后续行污染 (已知残留;生产上无混版本读同一份 shm,唯一暴露面是显式回退二进制)。
This commit is contained in:
1 parent
838f497290
commit
85ec38aa2d
1 file changed
+203
-14
+203
-14
@@ -12,34 +12,102 @@ use ipass_core::{http, state, util};
|
||||
|
||||
use crate::config;
|
||||
use crate::handlers; // `$scheme` 变量索引由 handlers 统一预取(见 prefetch_indexes)
|
||||
use crate::models::{CacheUpstream, CacheUpstreamStore};
|
||||
use crate::models::{CacheSource, CacheUpstream, CacheUpstreamStore};
|
||||
use crate::store;
|
||||
|
||||
// ===========================================================================
|
||||
// payload 编解码(shm 值)
|
||||
// ===========================================================================
|
||||
|
||||
/// `base \n 上游Key \n mode`。三行塞一个键,热路径一次查表拿全。
|
||||
fn payload_of(item: &CacheUpstream) -> String {
|
||||
format!(
|
||||
/// payload 版本标记。**第 4 行**:旧 payload 只有 3 行(`base\nkey\nmode`),
|
||||
/// 有第 4 行且等于本值即为 v2(后面每两行一个源:`url\nkey`)。
|
||||
pub const PAYLOAD_V2: &str = "v2";
|
||||
|
||||
/// 编码上游配置成 shm payload。
|
||||
///
|
||||
/// * **v1**(无 `sources`):`base\nkey\nmode`(三行,与旧版逐字节相同)
|
||||
/// * **v2**(有 `sources`):`base\nkey\nmode\nv2\n<url1>\n<key1>\n<url2>\n<key2>…`
|
||||
///
|
||||
/// ⚠️ 源列表里**不放 id**:id 只用于管理台展示与人工识别,
|
||||
/// 运行时认的是**下标**(冷却键用下标 ⇒ 配置改了也只是冷却条目失效,无副作用)。
|
||||
pub fn payload_of(item: &CacheUpstream) -> String {
|
||||
let mut s = format!(
|
||||
"{}{}{}{}{}",
|
||||
item.base,
|
||||
config::PAYLOAD_SEP,
|
||||
item.key,
|
||||
config::PAYLOAD_SEP,
|
||||
item.mode
|
||||
)
|
||||
);
|
||||
if item.sources.is_empty() {
|
||||
return s;
|
||||
}
|
||||
s.push(config::PAYLOAD_SEP);
|
||||
s.push_str(PAYLOAD_V2);
|
||||
for src in &item.sources {
|
||||
s.push(config::PAYLOAD_SEP);
|
||||
s.push_str(&src.url);
|
||||
s.push(config::PAYLOAD_SEP);
|
||||
s.push_str(&src.key);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// 拆 payload:`base \n key \n mode`(缺字段时给空串,不 panic)。
|
||||
///
|
||||
/// ⚠️ **只取前三行**:v2 payload 的第 3 行之后还有内容,
|
||||
/// 若用 `splitn(3)` 会把 `mode` 读成 `"proxy\nv2\nhttps://b"`。
|
||||
///
|
||||
/// 本函数保证的是「**仓内**所有调用点在 v1 与 v2 下都拿到**干净的前三行**」。
|
||||
///
|
||||
/// ⚠️ **旧二进制**读 v2 payload 时:base / key 仍然正确,
|
||||
/// **但 mode 会被后续行污染**(它带的是自己那份 `splitn(3)`,不调用本函数)。
|
||||
/// 这是**已知残留**——spec 只要求「别把 base 认成别的东西」,那条是满足的。
|
||||
/// 生产上不存在混版本读同一份 shm(nginx 的 shm 由每个实例自己的 master 建,
|
||||
/// master 与 worker 跑同一个二进制);ipass-all 与 ipass-cache 是两个独立
|
||||
/// nginx 实例(各自 shm、各自 PVC 子目录)⇒ 互不影响。
|
||||
/// 唯一暴露面是**显式回退二进制**对着已写入 `sources` 的表。
|
||||
pub fn split_payload(payload: &str) -> (&str, &str, &str) {
|
||||
let mut it = payload.splitn(3, config::PAYLOAD_SEP);
|
||||
let base = it.next().unwrap_or("");
|
||||
let key = it.next().unwrap_or("");
|
||||
let mode = it.next().unwrap_or("");
|
||||
let mut lines = payload.split(config::PAYLOAD_SEP);
|
||||
let base = lines.next().unwrap_or("");
|
||||
let key = lines.next().unwrap_or("");
|
||||
let mode = lines.next().unwrap_or("");
|
||||
(base, key, mode)
|
||||
}
|
||||
|
||||
/// 解析后的上游配置(v1 / v2 通吃)。
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Decoded {
|
||||
pub base: String,
|
||||
pub key: String,
|
||||
pub mode: String,
|
||||
pub sources: Vec<CacheSource>,
|
||||
}
|
||||
|
||||
/// 解析 payload(v1 / v2 通吃)。
|
||||
pub fn decode(payload: &str) -> Decoded {
|
||||
let lines: Vec<&str> = payload.split(config::PAYLOAD_SEP).collect();
|
||||
let mut sources = Vec::new();
|
||||
if lines.len() >= 4 && lines[3] == PAYLOAD_V2 {
|
||||
let mut i = 4;
|
||||
while i + 1 < lines.len() {
|
||||
sources.push(CacheSource {
|
||||
id: String::new(),
|
||||
url: lines[i].to_owned(),
|
||||
key: lines[i + 1].to_owned(),
|
||||
note: String::new(),
|
||||
});
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
Decoded {
|
||||
base: lines.first().copied().unwrap_or("").to_owned(),
|
||||
key: lines.get(1).copied().unwrap_or("").to_owned(),
|
||||
mode: lines.get(2).copied().unwrap_or("").to_owned(),
|
||||
sources,
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// 共享内存同步
|
||||
// ===========================================================================
|
||||
@@ -102,14 +170,36 @@ pub fn reload_shm() -> usize {
|
||||
// 查询
|
||||
// ===========================================================================
|
||||
|
||||
/// 热路径查表:`route → (base, 上游Key, mode)`
|
||||
pub fn lookup(route: &str) -> Option<(String, String, String)> {
|
||||
/// 热路径查表:`route → 完整上游配置(base / key / mode / sources)`
|
||||
pub fn lookup_upstream(route: &str) -> Option<Decoded> {
|
||||
let payload = state::get(config::KEY_CX_PREFIX, route.as_bytes())?.0;
|
||||
let (base, key, mode) = split_payload(&payload);
|
||||
if base.is_empty() {
|
||||
let up = decode(&payload);
|
||||
if up.base.is_empty() && up.sources.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some((base.to_owned(), key.to_owned(), mode.to_owned()))
|
||||
Some(up)
|
||||
}
|
||||
|
||||
/// 热路径查表:`route → (base, 上游Key, mode)`(签名不变,语义不变)
|
||||
pub fn lookup(route: &str) -> Option<(String, String, String)> {
|
||||
let up = lookup_upstream(route)?;
|
||||
Some((up.base, up.key, up.mode))
|
||||
}
|
||||
|
||||
/// 该路由的**有效源列表**。
|
||||
///
|
||||
/// 多源模式 ⇒ `sources` 原样;单源模式(无 `sources`)⇒ 用 `base` 造一条,
|
||||
/// 这样下游的选源逻辑**只有一条路径**(不必到处判「有没有池」)。
|
||||
pub fn effective_sources(up: &Decoded) -> Vec<CacheSource> {
|
||||
if !up.sources.is_empty() {
|
||||
return up.sources.clone();
|
||||
}
|
||||
vec![CacheSource {
|
||||
id: String::new(),
|
||||
url: up.base.clone(),
|
||||
key: up.key.clone(),
|
||||
note: String::new(),
|
||||
}]
|
||||
}
|
||||
|
||||
/// 从请求推出 `(route, base, 上游Key, mode)`——access handler 与变量求值共用。
|
||||
@@ -377,3 +467,102 @@ pub fn view_all(entry: &str) -> Vec<Value> {
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::models::CacheSource;
|
||||
|
||||
#[test]
|
||||
fn decode_v1_payload_yields_no_sources() {
|
||||
let up = decode("https://goproxy.cn\n\nproxy");
|
||||
assert_eq!(up.base, "https://goproxy.cn");
|
||||
assert_eq!(up.key, "");
|
||||
assert_eq!(up.mode, "proxy");
|
||||
assert!(up.sources.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_v2_payload_reads_source_pairs() {
|
||||
let payload = "https://a\nKEY\nproxy\nv2\nhttps://b\nK2\nhttps://c\n";
|
||||
let up = decode(payload);
|
||||
assert_eq!(up.base, "https://a");
|
||||
assert_eq!(up.key, "KEY");
|
||||
assert_eq!(up.mode, "proxy");
|
||||
assert_eq!(up.sources.len(), 2);
|
||||
assert_eq!(up.sources[0].url, "https://b");
|
||||
assert_eq!(up.sources[0].key, "K2");
|
||||
assert_eq!(up.sources[1].url, "https://c");
|
||||
assert_eq!(up.sources[1].key, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repo_split_reads_v2_payload_clean_first_three_lines() {
|
||||
// 仓内读法(本函数)遇到 v2 payload:取前三行 ⇒ base/key/mode 全干净
|
||||
let (b, k, m) = split_payload("https://a\nKEY\nproxy\nv2\nhttps://b\n");
|
||||
assert_eq!(b, "https://a");
|
||||
assert_eq!(k, "KEY");
|
||||
assert_eq!(m, "proxy");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_binary_splitn3_keeps_base_key_but_pollutes_mode() {
|
||||
// 旧二进制用的是它自己那份 splitn(3):base/key 干净,mode 被后续行污染。
|
||||
// 这是已知且可接受的(spec 只要求 base 不被认错),但必须被钉住,不能被误传成"全干净"。
|
||||
let payload = "https://a\nKEY\nproxy\nv2\nhttps://b\n";
|
||||
let mut it = payload.splitn(3, config::PAYLOAD_SEP);
|
||||
let b = it.next().unwrap_or("");
|
||||
let k = it.next().unwrap_or("");
|
||||
let m = it.next().unwrap_or("");
|
||||
assert_eq!(b, "https://a");
|
||||
assert_eq!(k, "KEY");
|
||||
// mode 被后续行污染(**已知残留**,见 split_payload 的 doc 注释)
|
||||
assert!(m.contains("v2"), "旧读法下 mode 应被污染,实际 = {m:?}");
|
||||
assert_ne!(m, "proxy");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn payload_of_v2_roundtrip() {
|
||||
let mut item = CacheUpstream::default();
|
||||
item.base = "https://a".to_owned();
|
||||
item.key = "KEY".to_owned();
|
||||
item.mode = "proxy".to_owned();
|
||||
item.sources = vec![CacheSource {
|
||||
id: "b".to_owned(),
|
||||
url: "https://b".to_owned(),
|
||||
key: "K2".to_owned(),
|
||||
note: String::new(),
|
||||
}];
|
||||
let up = decode(&payload_of(&item));
|
||||
assert_eq!(up.sources.len(), 1);
|
||||
assert_eq!(up.sources[0].url, "https://b");
|
||||
assert_eq!(up.sources[0].key, "K2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn payload_of_v1_is_byte_identical_legacy_format() {
|
||||
// v1 兼容的核心承诺:**逐字节**与旧版三行格式相同(不是只行数相同)
|
||||
let mut item = CacheUpstream::default();
|
||||
item.base = "https://a".to_owned();
|
||||
item.key = "KEY".to_owned();
|
||||
item.mode = "proxy".to_owned();
|
||||
assert!(item.sources.is_empty(), "本用例针对 v1(无 sources)");
|
||||
let expected = format!(
|
||||
"https://a{}KEY{}proxy",
|
||||
config::PAYLOAD_SEP,
|
||||
config::PAYLOAD_SEP
|
||||
);
|
||||
let p = payload_of(&item);
|
||||
assert_eq!(p, expected);
|
||||
assert_eq!(p.lines().count(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effective_sources_falls_back_to_base() {
|
||||
let up = decode("https://goproxy.cn\nKEY\nproxy");
|
||||
let srcs = effective_sources(&up);
|
||||
assert_eq!(srcs.len(), 1);
|
||||
assert_eq!(srcs[0].url, "https://goproxy.cn");
|
||||
assert_eq!(srcs[0].key, "KEY");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user