feat(cache): 选源纯函数 + 源冷却状态
select() 轮转起点=hash%n,冷却源降权到队尾不丢弃,链长恒为 3; 冷却项存 shm ch:<route>:<idx>,靠 expires_at 自动过期。
This commit is contained in:
1 parent
85ec38aa2d
commit
f412ec3dcc
3 files changed
+157
-1
No files matched your search
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user