|
| 1 | +use std::ffi::{c_char, c_void}; |
| 2 | +use std::ptr::{addr_of, addr_of_mut}; |
| 3 | +use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; |
| 4 | +use std::sync::Arc; |
| 5 | +use std::time::Instant; |
| 6 | + |
| 7 | +use ngx::async_::{sleep, spawn, Task}; |
| 8 | +use ngx::core; |
| 9 | +use ngx::ffi::{ |
| 10 | + ngx_array_push, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_t, ngx_http_handler_pt, |
| 11 | + ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, |
| 12 | + ngx_post_event, ngx_posted_events, ngx_posted_next_events, ngx_str_t, ngx_uint_t, |
| 13 | + NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_MODULE, NGX_LOG_EMERG, |
| 14 | +}; |
| 15 | +use ngx::http::{self, HttpModule, MergeConfigError}; |
| 16 | +use ngx::http::{HttpModuleLocationConf, HttpModuleMainConf, NgxHttpCoreModule}; |
| 17 | +use ngx::{http_request_handler, ngx_conf_log_error, ngx_log_debug_http, ngx_string}; |
| 18 | + |
| 19 | +struct Module; |
| 20 | + |
| 21 | +impl http::HttpModule for Module { |
| 22 | + fn module() -> &'static ngx_module_t { |
| 23 | + unsafe { &*::core::ptr::addr_of!(ngx_http_async_module) } |
| 24 | + } |
| 25 | + |
| 26 | + unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { |
| 27 | + // SAFETY: this function is called with non-NULL cf always |
| 28 | + let cf = &mut *cf; |
| 29 | + let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf"); |
| 30 | + |
| 31 | + let h = ngx_array_push( |
| 32 | + &mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers, |
| 33 | + ) as *mut ngx_http_handler_pt; |
| 34 | + if h.is_null() { |
| 35 | + return core::Status::NGX_ERROR.into(); |
| 36 | + } |
| 37 | + // set an Access phase handler |
| 38 | + *h = Some(async_access_handler); |
| 39 | + core::Status::NGX_OK.into() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Default)] |
| 44 | +struct ModuleConfig { |
| 45 | + enable: bool, |
| 46 | +} |
| 47 | + |
| 48 | +unsafe impl HttpModuleLocationConf for Module { |
| 49 | + type LocationConf = ModuleConfig; |
| 50 | +} |
| 51 | + |
| 52 | +static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [ |
| 53 | + ngx_command_t { |
| 54 | + name: ngx_string!("async"), |
| 55 | + type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t, |
| 56 | + set: Some(ngx_http_async_commands_set_enable), |
| 57 | + conf: NGX_HTTP_LOC_CONF_OFFSET, |
| 58 | + offset: 0, |
| 59 | + post: std::ptr::null_mut(), |
| 60 | + }, |
| 61 | + ngx_command_t::empty(), |
| 62 | +]; |
| 63 | + |
| 64 | +static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t { |
| 65 | + preconfiguration: Some(Module::preconfiguration), |
| 66 | + postconfiguration: Some(Module::postconfiguration), |
| 67 | + create_main_conf: None, |
| 68 | + init_main_conf: None, |
| 69 | + create_srv_conf: None, |
| 70 | + merge_srv_conf: None, |
| 71 | + create_loc_conf: Some(Module::create_loc_conf), |
| 72 | + merge_loc_conf: Some(Module::merge_loc_conf), |
| 73 | +}; |
| 74 | + |
| 75 | +// Generate the `ngx_modules` table with exported modules. |
| 76 | +// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem. |
| 77 | +#[cfg(feature = "export-modules")] |
| 78 | +ngx::ngx_modules!(ngx_http_async_module); |
| 79 | + |
| 80 | +#[used] |
| 81 | +#[allow(non_upper_case_globals)] |
| 82 | +#[cfg_attr(not(feature = "export-modules"), no_mangle)] |
| 83 | +pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t { |
| 84 | + ctx: std::ptr::addr_of!(NGX_HTTP_ASYNC_MODULE_CTX) as _, |
| 85 | + commands: unsafe { &NGX_HTTP_ASYNC_COMMANDS[0] as *const _ as *mut _ }, |
| 86 | + type_: NGX_HTTP_MODULE as _, |
| 87 | + ..ngx_module_t::default() |
| 88 | +}; |
| 89 | + |
| 90 | +impl http::Merge for ModuleConfig { |
| 91 | + fn merge(&mut self, prev: &ModuleConfig) -> Result<(), MergeConfigError> { |
| 92 | + if prev.enable { |
| 93 | + self.enable = true; |
| 94 | + }; |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) { |
| 100 | + let ctx = ngx::ngx_container_of!(event, RequestCTX, event); |
| 101 | + let c: *mut ngx_connection_t = (*event).data.cast(); |
| 102 | + |
| 103 | + if (*ctx).done.load(Ordering::Relaxed) { |
| 104 | + // Triggering async_access_handler again |
| 105 | + ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events)); |
| 106 | + } else { |
| 107 | + // this doesn't have have good performance but works as a simple thread-safe example and |
| 108 | + // doesn't causes segfault. The best method that provides both thread-safety and |
| 109 | + // performance requires an nginx patch. |
| 110 | + ngx_post_event(event, addr_of_mut!(ngx_posted_next_events)); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +struct RequestCTX { |
| 115 | + done: Arc<AtomicBool>, |
| 116 | + event: ngx_event_t, |
| 117 | + task: Option<Task<()>>, |
| 118 | +} |
| 119 | + |
| 120 | +impl Default for RequestCTX { |
| 121 | + fn default() -> Self { |
| 122 | + Self { |
| 123 | + done: AtomicBool::new(false).into(), |
| 124 | + event: unsafe { std::mem::zeroed() }, |
| 125 | + task: Default::default(), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl Drop for RequestCTX { |
| 131 | + fn drop(&mut self) { |
| 132 | + if let Some(handle) = self.task.take() { |
| 133 | + drop(handle); |
| 134 | + } |
| 135 | + |
| 136 | + if self.event.posted() != 0 { |
| 137 | + unsafe { ngx::ffi::ngx_delete_posted_event(&mut self.event) }; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +http_request_handler!(async_access_handler, |request: &mut http::Request| { |
| 143 | + let co = Module::location_conf(request).expect("module config is none"); |
| 144 | + |
| 145 | + ngx_log_debug_http!(request, "async module enabled: {}", co.enable); |
| 146 | + |
| 147 | + if !co.enable { |
| 148 | + return core::Status::NGX_DECLINED; |
| 149 | + } |
| 150 | + |
| 151 | + if let Some(ctx) = |
| 152 | + unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) } |
| 153 | + { |
| 154 | + if !ctx.done.load(Ordering::Relaxed) { |
| 155 | + return core::Status::NGX_AGAIN; |
| 156 | + } |
| 157 | + |
| 158 | + return core::Status::NGX_OK; |
| 159 | + } |
| 160 | + |
| 161 | + let ctx = request.pool().allocate(RequestCTX::default()); |
| 162 | + if ctx.is_null() { |
| 163 | + return core::Status::NGX_ERROR; |
| 164 | + } |
| 165 | + request.set_module_ctx(ctx.cast(), unsafe { &*addr_of!(ngx_http_async_module) }); |
| 166 | + |
| 167 | + let ctx = unsafe { &mut *ctx }; |
| 168 | + ctx.event.handler = Some(check_async_work_done); |
| 169 | + ctx.event.data = request.connection().cast(); |
| 170 | + ctx.event.log = unsafe { (*request.connection()).log }; |
| 171 | + unsafe { ngx_post_event(&mut ctx.event, addr_of_mut!(ngx_posted_next_events)) }; |
| 172 | + |
| 173 | + // Request is no longer needed and can be converted to something movable to the async block |
| 174 | + let req = AtomicPtr::new(request.into()); |
| 175 | + let done_flag = ctx.done.clone(); |
| 176 | + |
| 177 | + ctx.task = Some(spawn(async move { |
| 178 | + let start = Instant::now(); |
| 179 | + sleep(std::time::Duration::from_secs(2)).await; |
| 180 | + let req = unsafe { http::Request::from_ngx_http_request(req.load(Ordering::Relaxed)) }; |
| 181 | + // not really thread safe, we should apply all these operation in nginx thread |
| 182 | + // but this is just an example. proper way would be storing these headers in the request ctx |
| 183 | + // and apply them when we get back to the nginx thread. |
| 184 | + req.add_header_out( |
| 185 | + "X-Async-Time", |
| 186 | + start.elapsed().as_millis().to_string().as_str(), |
| 187 | + ); |
| 188 | + |
| 189 | + done_flag.store(true, Ordering::Release); |
| 190 | + // there is a small issue here. If traffic is low we may get stuck behind a 300ms timer |
| 191 | + // in the nginx event loop. To workaround it we can notify the event loop using |
| 192 | + // pthread_kill( nginx_thread, SIGIO ) to wake up the event loop. (or patch nginx |
| 193 | + // and use the same trick as the thread pool) |
| 194 | + })); |
| 195 | + |
| 196 | + core::Status::NGX_AGAIN |
| 197 | +}); |
| 198 | + |
| 199 | +extern "C" fn ngx_http_async_commands_set_enable( |
| 200 | + cf: *mut ngx_conf_t, |
| 201 | + _cmd: *mut ngx_command_t, |
| 202 | + conf: *mut c_void, |
| 203 | +) -> *mut c_char { |
| 204 | + unsafe { |
| 205 | + let conf = &mut *(conf as *mut ModuleConfig); |
| 206 | + let args: &[ngx_str_t] = (*(*cf).args).as_slice(); |
| 207 | + let val = match args[1].to_str() { |
| 208 | + Ok(s) => s, |
| 209 | + Err(_) => { |
| 210 | + ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`async` argument is not utf-8 encoded"); |
| 211 | + return ngx::core::NGX_CONF_ERROR; |
| 212 | + } |
| 213 | + }; |
| 214 | + |
| 215 | + // set default value optionally |
| 216 | + conf.enable = false; |
| 217 | + |
| 218 | + if val.eq_ignore_ascii_case("on") { |
| 219 | + conf.enable = true; |
| 220 | + } else if val.eq_ignore_ascii_case("off") { |
| 221 | + conf.enable = false; |
| 222 | + } |
| 223 | + }; |
| 224 | + |
| 225 | + ngx::core::NGX_CONF_OK |
| 226 | +} |
0 commit comments