- vanilla nginx + Rust 动态模块(取代 OpenResty+Lua),共享内存为运行期真源 - 三域名分流 18098 ssl:mdm 管理台 / lm LLM 代理 / *.dm 企业转发,共用 *.mayishangshu.cn 通配符证书(证书不入库) - LLM 统一出口:同 Key 鉴权、上游 Key 替换、白名单、限流配额、SSE - 回归夹具 tests/(网关 55 + LLM 34 = 89 断言,夹具 cp 覆盖重置) - 文档:readme(16 章)+ CLAUDE.md + conf/tls/README(acme.sh 证书管理、hairpin 环回链路)
238 lines
7.9 KiB
Rust
238 lines
7.9 KiB
Rust
extern crate alloc;
|
|
|
|
use alloc::sync::Arc;
|
|
use core::ffi::{c_char, c_void};
|
|
use core::mem;
|
|
use core::ptr;
|
|
use core::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
|
|
use core::time::Duration;
|
|
use std::sync::OnceLock;
|
|
use std::time::Instant;
|
|
|
|
use ngx::core::Status;
|
|
use ngx::ffi::{
|
|
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_LOG_EMERG,
|
|
ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_module_t, ngx_int_t,
|
|
ngx_module_t, ngx_post_event, ngx_posted_events, ngx_posted_next_events, ngx_str_t, ngx_uint_t,
|
|
};
|
|
use ngx::http::{self, HttpModule, HttpModuleLocationConf, HttpRequestHandler, MergeConfigError};
|
|
use ngx::{ngx_conf_log_error, ngx_log_debug_http, ngx_string};
|
|
use tokio::runtime::Runtime;
|
|
|
|
struct Module;
|
|
|
|
impl http::HttpModule for Module {
|
|
fn module() -> &'static ngx_module_t {
|
|
unsafe { &*::core::ptr::addr_of!(ngx_http_async_module) }
|
|
}
|
|
|
|
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
|
|
// SAFETY: this function is called with non-NULL cf always
|
|
let cf = unsafe { &mut *cf };
|
|
http::add_phase_handler::<AsyncAccessHandler>(cf)
|
|
.map_or(Status::NGX_ERROR, |_| Status::NGX_OK)
|
|
.into()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
struct ModuleConfig {
|
|
enable: bool,
|
|
}
|
|
|
|
unsafe impl HttpModuleLocationConf for Module {
|
|
type LocationConf = ModuleConfig;
|
|
}
|
|
|
|
static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
|
|
ngx_command_t {
|
|
name: ngx_string!("async"),
|
|
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
|
|
set: Some(ngx_http_async_commands_set_enable),
|
|
conf: NGX_HTTP_LOC_CONF_OFFSET,
|
|
offset: 0,
|
|
post: ptr::null_mut(),
|
|
},
|
|
ngx_command_t::empty(),
|
|
];
|
|
|
|
static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
|
|
preconfiguration: Some(Module::preconfiguration),
|
|
postconfiguration: Some(Module::postconfiguration),
|
|
create_main_conf: None,
|
|
init_main_conf: None,
|
|
create_srv_conf: None,
|
|
merge_srv_conf: None,
|
|
create_loc_conf: Some(Module::create_loc_conf),
|
|
merge_loc_conf: Some(Module::merge_loc_conf),
|
|
};
|
|
|
|
// Generate the `ngx_modules` table with exported modules.
|
|
// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem.
|
|
#[cfg(feature = "export-modules")]
|
|
ngx::ngx_modules!(ngx_http_async_module);
|
|
|
|
#[used]
|
|
#[allow(non_upper_case_globals)]
|
|
#[cfg_attr(not(feature = "export-modules"), unsafe(no_mangle))]
|
|
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
|
|
ctx: &raw const NGX_HTTP_ASYNC_MODULE_CTX as _,
|
|
commands: unsafe { &raw mut NGX_HTTP_ASYNC_COMMANDS[0] },
|
|
type_: NGX_HTTP_MODULE as _,
|
|
..ngx_module_t::default()
|
|
};
|
|
|
|
impl http::Merge for ModuleConfig {
|
|
fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> {
|
|
if prev.enable {
|
|
self.enable = true;
|
|
};
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
|
|
let ctx = ngx::ngx_container_of!(event, RequestCTX, event);
|
|
let c: *mut ngx_connection_t = unsafe { (*event).data.cast() };
|
|
|
|
if unsafe { (*ctx).done.load(Ordering::Relaxed) } {
|
|
// Triggering async_access_handler again
|
|
unsafe { ngx_post_event((*c).write, &raw mut ngx_posted_events) };
|
|
} else {
|
|
// this doesn't have have good performance but works as a simple thread-safe example and
|
|
// doesn't causes segfault. The best method that provides both thread-safety and
|
|
// performance requires an nginx patch.
|
|
unsafe { ngx_post_event(event, &raw mut ngx_posted_next_events) };
|
|
}
|
|
}
|
|
|
|
struct RequestCTX {
|
|
done: Arc<AtomicBool>,
|
|
event: ngx_event_t,
|
|
task: Option<tokio::task::JoinHandle<()>>,
|
|
}
|
|
|
|
impl Default for RequestCTX {
|
|
fn default() -> Self {
|
|
Self {
|
|
done: AtomicBool::new(false).into(),
|
|
event: unsafe { mem::zeroed() },
|
|
task: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for RequestCTX {
|
|
fn drop(&mut self) {
|
|
if let Some(handle) = self.task.take() {
|
|
handle.abort();
|
|
}
|
|
|
|
if self.event.posted() != 0 {
|
|
unsafe { ngx::ffi::ngx_delete_posted_event(&raw mut self.event) };
|
|
}
|
|
}
|
|
}
|
|
|
|
struct AsyncAccessHandler;
|
|
|
|
impl HttpRequestHandler for AsyncAccessHandler {
|
|
const PHASE: ngx::http::HttpPhase = ngx::http::HttpPhase::Access;
|
|
type Output = Status;
|
|
|
|
fn handler(request: &mut http::Request) -> Self::Output {
|
|
let co = Module::location_conf(request).expect("module config is none");
|
|
|
|
ngx_log_debug_http!(request, "async module enabled: {}", co.enable);
|
|
|
|
if !co.enable {
|
|
return Status::NGX_DECLINED;
|
|
}
|
|
|
|
if let Some(ctx) = request.get_module_ctx::<RequestCTX>(Module::module()) {
|
|
if !ctx.done.load(Ordering::Relaxed) {
|
|
return Status::NGX_AGAIN;
|
|
}
|
|
|
|
return Status::NGX_OK;
|
|
}
|
|
|
|
let ctx = request.pool().allocate(RequestCTX::default());
|
|
if ctx.is_null() {
|
|
return Status::NGX_ERROR;
|
|
}
|
|
request.set_module_ctx(ctx.cast(), Module::module());
|
|
|
|
let ctx = unsafe { &mut *ctx };
|
|
ctx.event.handler = Some(check_async_work_done);
|
|
ctx.event.data = request.connection().cast();
|
|
ctx.event.log = unsafe { (*request.connection()).log };
|
|
unsafe { ngx_post_event(&raw mut ctx.event, &raw mut ngx_posted_next_events) };
|
|
|
|
// Request is no longer needed and can be converted to something movable to the async block
|
|
let req = AtomicPtr::new(request.into());
|
|
let done_flag = ctx.done.clone();
|
|
|
|
let rt = ngx_http_async_runtime();
|
|
ctx.task = Some(rt.spawn(async move {
|
|
let start = Instant::now();
|
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
|
let req = unsafe { http::Request::from_ngx_http_request(req.load(Ordering::Relaxed)) };
|
|
// not really thread safe, we should apply all these operation in nginx thread
|
|
// but this is just an example. proper way would be storing these headers in the request
|
|
// ctx and apply them when we get back to the nginx thread.
|
|
req.add_header_out("X-Async-Time", start.elapsed().as_millis().to_string().as_str());
|
|
|
|
done_flag.store(true, Ordering::Release);
|
|
// there is a small issue here. If traffic is low we may get stuck behind a 300ms timer
|
|
// in the nginx event loop. To workaround it we can notify the event loop using
|
|
// pthread_kill( nginx_thread, SIGIO ) to wake up the event loop. (or patch nginx
|
|
// and use the same trick as the thread pool)
|
|
}));
|
|
|
|
Status::NGX_AGAIN
|
|
}
|
|
}
|
|
|
|
extern "C" fn ngx_http_async_commands_set_enable(
|
|
cf: *mut ngx_conf_t,
|
|
_cmd: *mut ngx_command_t,
|
|
conf: *mut c_void,
|
|
) -> *mut c_char {
|
|
unsafe {
|
|
let conf = &mut *(conf as *mut ModuleConfig);
|
|
let args: &[ngx_str_t] = (*(*cf).args).as_slice();
|
|
let val = match args[1].to_str() {
|
|
Ok(s) => s,
|
|
Err(_) => {
|
|
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`async` argument is not utf-8 encoded");
|
|
return ngx::core::NGX_CONF_ERROR;
|
|
}
|
|
};
|
|
|
|
// set default value optionally
|
|
conf.enable = false;
|
|
|
|
if val.eq_ignore_ascii_case("on") {
|
|
conf.enable = true;
|
|
} else if val.eq_ignore_ascii_case("off") {
|
|
conf.enable = false;
|
|
}
|
|
};
|
|
|
|
ngx::core::NGX_CONF_OK
|
|
}
|
|
|
|
fn ngx_http_async_runtime() -> &'static Runtime {
|
|
// Should not be called from the master process
|
|
assert_ne!(unsafe { ngx::ffi::ngx_process }, ngx::ffi::NGX_PROCESS_MASTER as _);
|
|
|
|
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
|
|
RUNTIME.get_or_init(|| {
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
.enable_all()
|
|
.build()
|
|
.expect("tokio runtime init")
|
|
})
|
|
}
|