Skip to content

Commit a2ecf0c

Browse files
committed
rename async example to tokio
To reduce some confusion. Since this example was introduced, we introduced a smaller nginx-native async executor directly to ngx
1 parent cbf33dd commit a2ecf0c

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

.github/workflows/nginx.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +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;
52+
load_module ${{ github.workspace }}/nginx/objs/ngx_http_tokio_module.so;
5353
load_module ${{ github.workspace }}/nginx/objs/ngx_http_awssigv4_module.so;
5454
load_module ${{ github.workspace }}/nginx/objs/ngx_http_curl_module.so;
5555
load_module ${{ github.workspace }}/nginx/objs/ngx_http_shared_dict_module.so;

examples/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ path = "upstream.rs"
4747
crate-type = ["cdylib"]
4848

4949
[[example]]
50-
name = "async"
51-
path = "async.rs"
50+
name = "tokio"
51+
path = "tokio.rs"
5252
crate-type = ["cdylib"]
5353

5454
[[example]]

examples/config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ if [ $HTTP = YES ]; then
1616
ngx_rust_target_features=
1717

1818
if :; then
19-
ngx_module_name=ngx_http_async_module
19+
ngx_module_name=ngx_http_tokio_module
2020
ngx_module_libs="-lm"
21-
ngx_rust_target_name=async
21+
ngx_rust_target_name=tokio
2222

2323
ngx_rust_module
2424
fi

examples/t/async.t renamed to examples/t/tokio.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ http {
3939
server_name localhost;
4040
4141
location / {
42-
async on;
42+
tokio on;
4343
}
4444
}
4545
}
@@ -51,6 +51,6 @@ $t->run();
5151

5252
###############################################################################
5353

54-
like(http_get('/index.html'), qr/X-Async-Time:/, 'async handler');
54+
like(http_get('/index.html'), qr/X-Tokio-Time:/, 'tokio handler');
5555

5656
###############################################################################

examples/async.conf renamed to examples/tokio.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ daemon off;
22
master_process off;
33
# worker_processes 1;
44

5-
load_module modules/libasync.so;
5+
load_module modules/libtokio.so;
66
error_log error.log debug;
77

88
events { }
@@ -14,7 +14,7 @@ http {
1414
location / {
1515
root html;
1616
index index.html index.htm;
17-
async on;
17+
tokio on;
1818
}
1919
error_page 500 502 503 504 /50x.html;
2020
location = /50x.html {

examples/async.rs renamed to examples/tokio.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Module;
2020

2121
impl http::HttpModule for Module {
2222
fn module() -> &'static ngx_module_t {
23-
unsafe { &*::core::ptr::addr_of!(ngx_http_async_module) }
23+
unsafe { &*::core::ptr::addr_of!(ngx_http_tokio_module) }
2424
}
2525

2626
unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
@@ -35,7 +35,7 @@ impl http::HttpModule for Module {
3535
return core::Status::NGX_ERROR.into();
3636
}
3737
// set an Access phase handler
38-
*h = Some(async_access_handler);
38+
*h = Some(tokio_access_handler);
3939
core::Status::NGX_OK.into()
4040
}
4141
}
@@ -49,19 +49,19 @@ unsafe impl HttpModuleLocationConf for Module {
4949
type LocationConf = ModuleConfig;
5050
}
5151

52-
static mut NGX_HTTP_ASYNC_COMMANDS: [ngx_command_t; 2] = [
52+
static mut NGX_HTTP_TOKIO_COMMANDS: [ngx_command_t; 2] = [
5353
ngx_command_t {
54-
name: ngx_string!("async"),
54+
name: ngx_string!("tokio"),
5555
type_: (NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1) as ngx_uint_t,
56-
set: Some(ngx_http_async_commands_set_enable),
56+
set: Some(ngx_http_tokio_commands_set_enable),
5757
conf: NGX_HTTP_LOC_CONF_OFFSET,
5858
offset: 0,
5959
post: std::ptr::null_mut(),
6060
},
6161
ngx_command_t::empty(),
6262
];
6363

64-
static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
64+
static NGX_HTTP_TOKIO_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
6565
preconfiguration: Some(Module::preconfiguration),
6666
postconfiguration: Some(Module::postconfiguration),
6767
create_main_conf: None,
@@ -75,14 +75,14 @@ static NGX_HTTP_ASYNC_MODULE_CTX: ngx_http_module_t = ngx_http_module_t {
7575
// Generate the `ngx_modules` table with exported modules.
7676
// This feature is required to build a 'cdylib' dynamic module outside of the NGINX buildsystem.
7777
#[cfg(feature = "export-modules")]
78-
ngx::ngx_modules!(ngx_http_async_module);
78+
ngx::ngx_modules!(ngx_http_tokio_module);
7979

8080
#[used]
8181
#[allow(non_upper_case_globals)]
8282
#[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 _ },
83+
pub static mut ngx_http_tokio_module: ngx_module_t = ngx_module_t {
84+
ctx: std::ptr::addr_of!(NGX_HTTP_TOKIO_MODULE_CTX) as _,
85+
commands: unsafe { &NGX_HTTP_TOKIO_COMMANDS[0] as *const _ as *mut _ },
8686
type_: NGX_HTTP_MODULE as _,
8787
..ngx_module_t::default()
8888
};
@@ -96,12 +96,12 @@ impl http::Merge for ModuleConfig {
9696
}
9797
}
9898

99-
unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
99+
unsafe extern "C" fn check_tokio_work_done(event: *mut ngx_event_t) {
100100
let ctx = ngx::ngx_container_of!(event, RequestCTX, event);
101101
let c: *mut ngx_connection_t = (*event).data.cast();
102102

103103
if (*ctx).done.load(Ordering::Relaxed) {
104-
// Triggering async_access_handler again
104+
// Triggering tokio_access_handler again
105105
ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
106106
} else {
107107
// this doesn't have have good performance but works as a simple thread-safe example and
@@ -139,17 +139,17 @@ impl Drop for RequestCTX {
139139
}
140140
}
141141

142-
http_request_handler!(async_access_handler, |request: &mut http::Request| {
142+
http_request_handler!(tokio_access_handler, |request: &mut http::Request| {
143143
let co = Module::location_conf(request).expect("module config is none");
144144

145-
ngx_log_debug_http!(request, "async module enabled: {}", co.enable);
145+
ngx_log_debug_http!(request, "tokio module enabled: {}", co.enable);
146146

147147
if !co.enable {
148148
return core::Status::NGX_DECLINED;
149149
}
150150

151151
if let Some(ctx) =
152-
unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_async_module)) }
152+
unsafe { request.get_module_ctx::<RequestCTX>(&*addr_of!(ngx_http_tokio_module)) }
153153
{
154154
if !ctx.done.load(Ordering::Relaxed) {
155155
return core::Status::NGX_AGAIN;
@@ -162,10 +162,10 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
162162
if ctx.is_null() {
163163
return core::Status::NGX_ERROR;
164164
}
165-
request.set_module_ctx(ctx.cast(), unsafe { &*addr_of!(ngx_http_async_module) });
165+
request.set_module_ctx(ctx.cast(), unsafe { &*addr_of!(ngx_http_tokio_module) });
166166

167167
let ctx = unsafe { &mut *ctx };
168-
ctx.event.handler = Some(check_async_work_done);
168+
ctx.event.handler = Some(check_tokio_work_done);
169169
ctx.event.data = request.connection().cast();
170170
ctx.event.log = unsafe { (*request.connection()).log };
171171
unsafe { ngx_post_event(&mut ctx.event, addr_of_mut!(ngx_posted_next_events)) };
@@ -174,7 +174,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
174174
let req = AtomicPtr::new(request.into());
175175
let done_flag = ctx.done.clone();
176176

177-
let rt = ngx_http_async_runtime();
177+
let rt = ngx_http_tokio_runtime();
178178
ctx.task = Some(rt.spawn(async move {
179179
let start = Instant::now();
180180
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
@@ -183,7 +183,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
183183
// but this is just an example. proper way would be storing these headers in the request ctx
184184
// and apply them when we get back to the nginx thread.
185185
req.add_header_out(
186-
"X-Async-Time",
186+
"X-Tokio-Time",
187187
start.elapsed().as_millis().to_string().as_str(),
188188
);
189189

@@ -197,7 +197,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| {
197197
core::Status::NGX_AGAIN
198198
});
199199

200-
extern "C" fn ngx_http_async_commands_set_enable(
200+
extern "C" fn ngx_http_tokio_commands_set_enable(
201201
cf: *mut ngx_conf_t,
202202
_cmd: *mut ngx_command_t,
203203
conf: *mut c_void,
@@ -208,7 +208,7 @@ extern "C" fn ngx_http_async_commands_set_enable(
208208
let val = match args[1].to_str() {
209209
Ok(s) => s,
210210
Err(_) => {
211-
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`async` argument is not utf-8 encoded");
211+
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "`tokio` argument is not utf-8 encoded");
212212
return ngx::core::NGX_CONF_ERROR;
213213
}
214214
};
@@ -226,7 +226,7 @@ extern "C" fn ngx_http_async_commands_set_enable(
226226
ngx::core::NGX_CONF_OK
227227
}
228228

229-
fn ngx_http_async_runtime() -> &'static Runtime {
229+
fn ngx_http_tokio_runtime() -> &'static Runtime {
230230
// Should not be called from the master process
231231
assert_ne!(
232232
unsafe { ngx::ffi::ngx_process },

0 commit comments

Comments
 (0)