Skip to content

Commit f7daeb3

Browse files
committed
feat(examples): add example using ngx::async_
1 parent 4043c1f commit f7daeb3

File tree

6 files changed

+324
-0
lines changed

6 files changed

+324
-0
lines changed

.github/workflows/nginx.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ env:
4949
5050
NGX_TEST_FILES: examples/t
5151
NGX_TEST_GLOBALS_DYNAMIC: >-
52+
load_module ${{ github.workspace }}/nginx/objs/ngx_http_async_module.so;
5253
load_module ${{ github.workspace }}/nginx/objs/ngx_http_tokio_module.so;
5354
load_module ${{ github.workspace }}/nginx/objs/ngx_http_awssigv4_module.so;
5455
load_module ${{ github.workspace }}/nginx/objs/ngx_http_curl_module.so;

examples/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ idna_adapter = "=1.1.0"
2525
libc = "0.2.140"
2626
tokio = { version = "1.33.0", features = ["full"] }
2727

28+
29+
[[example]]
30+
name = "async"
31+
path = "async.rs"
32+
crate-type = ["cdylib"]
33+
required-features = [ "async" ]
34+
2835
[[example]]
2936
name = "curl"
3037
path = "curl.rs"
@@ -65,3 +72,4 @@ default = ["export-modules", "ngx/vendored"]
6572
# See https://github.com/rust-lang/rust/issues/20267
6673
export-modules = []
6774
linux = []
75+
async = [ "ngx/async" ]

examples/async.conf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
daemon off;
2+
master_process off;
3+
# worker_processes 1;
4+
5+
load_module modules/libasync.so;
6+
error_log error.log debug;
7+
8+
events { }
9+
10+
http {
11+
server {
12+
listen *:8000;
13+
server_name localhost;
14+
location / {
15+
root html;
16+
index index.html index.htm;
17+
async on;
18+
}
19+
error_page 500 502 503 504 /50x.html;
20+
location = /50x.html {
21+
root html;
22+
}
23+
}
24+
}

examples/async.rs

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
}

examples/config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ if [ $HTTP = YES ]; then
1515
ngx_rust_target_type=EXAMPLE
1616
ngx_rust_target_features=
1717

18+
if :; then
19+
ngx_module_name=ngx_http_async_module
20+
ngx_module_libs="-lm"
21+
ngx_rust_target_name=async
22+
ngx_rust_target_features=async
23+
24+
ngx_rust_module
25+
fi
26+
1827
if :; then
1928
ngx_module_name=ngx_http_tokio_module
2029
ngx_module_libs="-lm"

examples/t/async.t

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Nginx, Inc
4+
5+
# Tests for ngx-rust example modules.
6+
7+
###############################################################################
8+
9+
use warnings;
10+
use strict;
11+
12+
use Test::More;
13+
14+
BEGIN { use FindBin; chdir($FindBin::Bin); }
15+
16+
use lib 'lib';
17+
use Test::Nginx;
18+
19+
###############################################################################
20+
21+
select STDERR; $| = 1;
22+
select STDOUT; $| = 1;
23+
24+
my $t = Test::Nginx->new()->has(qw/http/)->plan(1)
25+
->write_file_expand('nginx.conf', <<'EOF');
26+
27+
%%TEST_GLOBALS%%
28+
29+
daemon off;
30+
31+
events {
32+
}
33+
34+
http {
35+
%%TEST_GLOBALS_HTTP%%
36+
37+
server {
38+
listen 127.0.0.1:8080;
39+
server_name localhost;
40+
41+
location / {
42+
async on;
43+
}
44+
}
45+
}
46+
47+
EOF
48+
49+
$t->write_file('index.html', '');
50+
$t->run();
51+
52+
###############################################################################
53+
54+
like(http_get('/index.html'), qr/X-Async-Time:/, 'async handler');
55+
56+
###############################################################################

0 commit comments

Comments
 (0)