feat(cache): 选源纯函数 + 源冷却状态

select() 轮转起点=hash%n,冷却源降权到队尾不丢弃,链长恒为 3;
  冷却项存 shm ch:<route>:<idx>,靠 expires_at 自动过期。
This commit is contained in:
admin committed 2026-10-30 15:35:49 +08:00
1 parent 85ec38aa2d
commit f412ec3dcc
3 files changed
+157 -1

No files matched your search

+127 -1
View File
@@ -24,9 +24,10 @@ use ngx::ffi::{
};
use ngx::ngx_string;
use ipass_core::http;
use ipass_core::{http, state, util};
use crate::config;
use crate::handlers;
use crate::upstreams;
// ===========================================================================
@@ -138,6 +139,96 @@ pub fn host_of(base: &str) -> &str {
rest.split('/').next().unwrap_or(rest)
}
// ===========================================================================
// 选源(纯函数,可单测)
// ===========================================================================
/// FNV-1a 64 位散列(选源用;只需稳定散列,不抗碰撞)。
pub fn fnv1a(data: &[u8]) -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in data {
h ^= *b as u64;
h = h.wrapping_mul(0x0000_0100_0000_01b3);
}
h
}
/// 选源:返回三个顺位对应的源下标。
///
/// 规则:
/// 1. 起点 = `hash % n`,按顺位轮转;
/// 2. **冷却中的源排到队尾**(不丢弃)——冷却是降权不是禁用:
/// 池里只剩冷却源时仍要能出请求(宁可重试也不 500);
/// 3. 源数不足 3 时重复最后一个 ⇒ **链长恒为 3**,
/// conf 里写死的三跳链永远拿得到值。
pub fn select(n: usize, hash: u64, cooled: &[bool]) -> [usize; config::MAX_SLOTS] {
if n == 0 {
return [0; config::MAX_SLOTS];
}
let start = (hash % n as u64) as usize;
let mut order: Vec<usize> = Vec::with_capacity(n);
for k in 0..n {
let i = (start + k) % n;
if !cooled.get(i).copied().unwrap_or(false) {
order.push(i);
}
}
for k in 0..n {
let i = (start + k) % n;
if cooled.get(i).copied().unwrap_or(false) {
order.push(i);
}
}
let last = order.len() - 1;
let mut slots = [order[0]; config::MAX_SLOTS];
for (s, slot) in slots.iter_mut().enumerate() {
*slot = order[s.min(last)];
}
slots
}
/// 该路由各源是否处于冷却中。
///
/// 冷却项用 `expires_at` 自动过期 ⇒ 这里只需查「还活着没有」。
pub fn cooled_flags(route: &str, n: usize) -> Vec<bool> {
let now = util::now();
(0..n)
.map(|i| {
let name = format!("{route}:{i}");
state::get_live(config::KEY_CH_PREFIX, name.as_bytes(), now).is_some()
})
.collect()
}
/// 把某个源打入冷却(`config::COOLDOWN_SECS` 秒后自动恢复)。
pub fn cool_down(route: &str, idx: usize) {
let name = format!("{route}:{idx}");
let until = util::now() + config::COOLDOWN_SECS;
if !state::put(config::KEY_CH_PREFIX, name.as_bytes(), b"1", until) {
util::log_err(&format!(
"[cache] 源冷却写入失败 route={route} idx={idx}(共享内存可能已满)"
));
}
}
/// 本次请求的散列种子:优先用 `$request_id`(每请求唯一 ⇒ 冷请求天然分散),
/// 拿不到时退化为 URI+args 的散列。
pub fn hash_of_request(r: *mut ngx_http_request_t) -> u64 {
let rid = handlers::read_request_id(r);
if !rid.is_empty() {
return fnv1a(rid.as_bytes());
}
// ⚠️ 简报原稿写的是 `String` 缓冲 + `extend_from_slice(&args)`,但
// `http::args_raw` 给的是 `Vec<u8>`、`String::extend_from_slice` 只吃
// `&str` ⇒ 原稿这段过不了编译。这里改成字节缓冲,语义等价(拼
// `uri?args` 后整体散列)。
let mut buf: Vec<u8> = http::uri(r).into_bytes();
let args = http::args_raw(r);
buf.push(b'?');
buf.extend_from_slice(&args);
fnv1a(&buf)
}
/// 由 `(route, tail, base)` 拼出上游完整 URL。
///
/// 再补回 [`config::upstream_path_prefix`](只有 Docker 非空)。
@@ -295,3 +386,38 @@ pub const VARIABLE_DOCS: &[(&str, &str)] = &[
"缓存键(proxy_cache_key)= <route>:<uri>[?args],含 query、不含任何下游凭据(全站共享)",
),
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fnv1a_is_stable_and_spread() {
assert_eq!(fnv1a(b"abc"), fnv1a(b"abc"));
assert_ne!(fnv1a(b"abc"), fnv1a(b"abd"));
}
#[test]
fn select_starts_at_hash_position() {
// 4 个源全部健康:起点 = hash % 4 = 2
assert_eq!(select(4, 2, &[false; 4]), [2, 3, 0]);
}
#[test]
fn select_moves_cooled_sources_to_the_tail() {
// 源 1 在冷却:健康源 0/2/3 排前,冷却源 1 排后(不丢弃)
assert_eq!(select(4, 0, &[false, true, false, false]), [0, 2, 3]);
}
#[test]
fn select_always_returns_three_slots() {
assert_eq!(select(1, 0, &[false]), [0, 0, 0]);
assert_eq!(select(2, 1, &[false, false]), [1, 0, 0]);
assert_eq!(select(2, 1, &[true, true]), [1, 0, 0]);
}
#[test]
fn select_with_zero_sources_is_safe() {
assert_eq!(select(0, 0, &[]), [0, 0, 0]);
}
}
+11
View File
@@ -25,6 +25,17 @@ pub const KEY_CX_PREFIX: &[u8] = b"cx:";
/// 与 `cx:` 分属两片键空间:前者是**配置**,后者是**读数**。
pub const KEY_CACHE_PREFIX: &str = "cache:";
/// 源冷却的键前缀:`ch:<route>:<源下标>` → 值 `"1"`,靠 `expires_at` 自动过期。
///
/// 与 `cx:`(配置)、`cache:`(读数)分属三片键空间:这里是**运行时状态**。
pub const KEY_CH_PREFIX: &[u8] = b"ch:";
/// 失败源的冷却时长(秒)。短到能自愈,长到能挡住一次抽风窗口。
pub const COOLDOWN_SECS: i64 = 60;
/// 换源链长度:首选 + 备选 1 + 备选 2(conf 里固定三跳 ⇒ 这里固定 3)。
pub const MAX_SLOTS: usize = 3;
/// shm payload 内部的行分隔符(base / 上游 Key / mode)。
pub const PAYLOAD_SEP: char = '\n';
+19
View File
@@ -40,6 +40,8 @@ use crate::upstreams;
static CACHE_STATUS_IDX: AtomicIsize = AtomicIsize::new(-1);
/// `$scheme` 的变量索引(管理 API 拼「下游地址」时要回显 http/https)
static SCHEME_IDX: AtomicIsize = AtomicIsize::new(-1);
/// `$request_id` 的变量索引(选源散列用)。**可选**:拿不到就退化为 URI 散列。
static REQUEST_ID_IDX: AtomicIsize = AtomicIsize::new(-1);
/// postconfiguration:预取两个变量索引。失败返回 false,gateway 会 EMERG 退出。
///
@@ -63,6 +65,17 @@ pub fn prefetch_indexes(cf: *mut ngx_conf_t) -> bool {
}
CACHE_STATUS_IDX.store(i1 as isize, Ordering::Relaxed);
SCHEME_IDX.store(i2 as isize, Ordering::Relaxed);
// `$request_id` 是**可选**的:拿不到就退化为 URI 散列(见 cache::hash_of_request),
// 因此这里解析失败**不致命**——与上面两个不同,它们缺了会让核心功能不可用。
let mut rid = ngx_str_t {
len: "request_id".len() as usize,
data: b"request_id\0".as_ptr() as *mut u8,
};
// SAFETY: cf 由 nginx 传入且有效;ngx_str_t 指向栈上合法内存
let i3 = unsafe { ngx_http_get_variable_index(cf, &mut rid) };
REQUEST_ID_IDX.store(i3 as isize, Ordering::Relaxed);
true
}
@@ -72,6 +85,12 @@ pub fn read_scheme(r: *mut ngx_http_request_t) -> String {
unsafe { read_indexed(r, SCHEME_IDX.load(Ordering::Relaxed)) }
}
/// 读当前请求的 `$request_id`(空串 = 拿不到,调用方自行退化)
pub fn read_request_id(r: *mut ngx_http_request_t) -> String {
// SAFETY: r 由 nginx 传入且有效;索引在 postconfig 已解析
unsafe { read_indexed(r, REQUEST_ID_IDX.load(Ordering::Relaxed)) }
}
/// 安全读取一个已索引变量的字符串值;拿不到返回空串。
unsafe fn read_indexed(r: *mut ngx_http_request_t, idx: isize) -> String {
if idx < 0 {